Rmd/Kntir: LaTeX 环境中的 Markdown 引用

Rmd/Kntir: Markdown citations in LaTeX environments

我想在 Rmd/Knitr-document 中创建一个 threeparttable 并在 table 的底部添加注释。 table 是由 R-function 在 results = "asis" 块中创建的。我没有将函数添加到工作示例中,因为它非常冗长,而且问题在纯 LaTeX 代码中很明显。

这有效,结果符合预期。

---
title: "Untitled"
output: pdf_document
header-includes:
- \usepackage{threeparttable}
- \usepackage{booktabs}
- \usepackage{longtable}
references:
- id: rao2001basic
  title: Basic Research in Parapsychology
  author:
  - family: Rao
    given: K.R.
  issued:
    year: 2001
  publisher: McFarland
  type: book
---

\begin{table}[h]
\centering
\begin{threeparttable}
\caption{A summary table of the cars dataset.}
\begin{tabular}{lrr}
\toprule
Descriptives & speed & dist\
\midrule
Mean & 15.4 & 42.98\
SD & 5.29 & 25.77\
Min & 4 & 2\
Max & 25 & 120\
\bottomrule
\end{tabular}
\tablenotes{\item\textit{Note.} This table was created by @rao2001basic. }
\end{threeparttable}
\end{table}

不幸的是,table 标题中的引用无效。如果我把它从 LaTeX 环境中取出来,它工作正常,但不是在里面。有没有办法在 LaTeX 环境下解析 Markdown?

这类问题本质上是一个转义问题,或者说是pandoc自动乳胶块begin/end识别的回避问题。

这个特殊情况可以直接用环境命令写成

\table[h]
\centering
\threeparttable
\caption{A summary table of the cars dataset.}
\begin{tabular}{lrr}
\toprule
Descriptives & speed & dist\
\midrule
Mean & 15.4 & 42.98\
SD & 5.29 & 25.77\
Min & 4 & 2\
Max & 25 & 120\
\bottomrule
\end{tabular}
\tablenotes[flushleft]
\item\textit{Note.} This table was created by @rao2001basic.
\endtablenotes
\endthreeparttable
\endtable

但如果确实需要 begin{env}/end{env} 则可以像这样使用宏

\def \btable{\begin{table}}
\def \etable{\end{table}}
\def \bthreeparttable{\begin{threeparttable}}
\def \ethreeparttable{\end{threeparttable}}
\def \btablenotes{\begin{tablenotes}}
\def \etablenotes{\end{tablenotes}}

如果存在用于重命名 begin{env}/end{env} 并允许在 tex 块中 selective markdown 的强大通用解决方案,那就太好了。像...

\newcommand\mdbegin[2]{%
  \ifstrempty{#1}{%
    \begin{#2}
  }{%
    \begin{#1}[#2]
  }%
}

\newcommand\mdend[1]{%
  \end{#1}
}

这适用于此,使用 etoolbox 包,但我认为这不是推荐的解决方案。

我发现,如果你愿意使用bookdown::pdf_document2()格式,你可以使用text references来解决这个问题,而不必乱用LaTeX:

---
title: "Untitled"
output: bookdown::pdf_document2
header-includes:
- \usepackage{threeparttable}
- \usepackage{booktabs}
- \usepackage{longtable}
references:
- id: rao2001basic
  title: Basic Research in Parapsychology
  author:
  - family: Rao
    given: K.R.
  issued:
    year: 2001
  publisher: McFarland
  type: book
---

(ref:tablenote)
This table was created by @rao2001basic.

\begin{table}[h]
\centering
\begin{threeparttable}
\caption{A summary table of the cars dataset.}
\begin{tabular}{lrr}
\toprule
Descriptives & speed & dist\
\midrule
Mean & 15.4 & 42.98\
SD & 5.29 & 25.77\
Min & 4 & 2\
Max & 25 & 120\
\bottomrule
\end{tabular}
\tablenotes{\item\textit{Note.} (ref:tablenote)}
\end{threeparttable}
\end{table}

这甚至适用于在 R:

中创建表的情况
```{r results = "asis"}
knitr::kable(mtcars[1:3, ], caption = "(ref:tablenote)")
```