【问题标题】:Rmd/Kntir: Markdown citations in LaTeX environmentsRmd/Kntir:LaTeX 环境中的 Markdown 引用
【发布时间】:2015-05-28 08:21:41
【问题描述】:

我想在 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}

很遗憾,表格标题中的引用无效。如果我把它从 LaTeX 环境中拿出来,它工作得很好,但不是在里面。有没有办法在 LaTeX 环境中解析 Markdown?

【问题讨论】:

  • 我认为这是不可能的。 Pandoc 不解析原始 tex 块内的降价。是否可以让您的代码输出降价表而不是乳胶?
  • 我认为这种类型的表格格式目前在 pandoc 中是不可能的。
  • 由于您使用函数来生成表格,您可以将其分成两部分,在其中发出引用并写入两个文件,然后使用 `\input{part1.txt} [@ rao2001basic] \输入{part2.txt}。尽管使用外部 bib 文件可能更容易且可重现。
  • 最近有人建议添加此功能github.com/jgm/pandoc/issues/2453
  • 使用knitcitations 会是一个解决方案吗?

标签: r latex knitr r-markdown


【解决方案1】:

这类问题本质上是一个逃避问题,或者说是 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 包,但我认为这不是推荐的解决方案。

【讨论】:

  • 非常感谢,这看起来可能只适合我的目的。我会仔细看看。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    • 2013-06-07
    • 1970-01-01
    • 2021-05-01
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    相关资源
    最近更新 更多