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

时间:2015-03-24 20:45:08

标签: r latex knitr r-markdown

我想在Rmd / Knitr文档中创建threeparttable并在表格的底部添加注释。该表由具有results = "asis"的块内的R函数创建。我没有将这个函数添加到工作示例中,因为它非常详细,纯粹的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}

enter image description here

不幸的是,表格标题中的引用不起作用。如果我把它从LaTeX环境中拿出来,它可以正常工作,但不在里面。有没有办法在LaTeX环境中解析Markdown?

2 个答案:

答案 0 :(得分:4)

这类问题基本上是一个逃避问题,或者更确切地说是pandoc自动乳胶块开始/结束识别的避免问题。

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

\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块中进行选择性降价,那将是很好的。有点像...

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

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

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

答案 1 :(得分:0)

我发现如果你愿意使用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)")
```