r - pandoc 如何解析 .md 文件中的 latex 代码?

标签 r knitr r-markdown pandoc xtable

我正在使用 Rstudio 和 knitr/rmarkdown/pandoc/latex 将 .Rmd 代码呈现为 pdf。我一直在努力使某些 latex 代码完全按预期呈现,而非常细微不同的代码最终没有被正确解析,这导致我的 .tex 文件包含诸如“\textbackslash{}begin{table}”而不是“\begin”之类的行{ table }”。

在处理 HTML 时,谷歌搜索揭示了类似的错误解析,但我直接从 .Rmd 到 .md 到 .tex 到 .pdf。

这完全取决于我使用的 Rstudio 的特定版本/平台,以及 R 包 knitr、xtable、rmarkdown、rmarkdown 模板等,所以我一直在努力想出一个 MWE。

(我确实检查过 pandoc 的 m,y 版本是否 >= 1.13,因为谷歌搜索表明早期版本中存在可能相关的错误。)

但是,我现在有一种 MWE,我至少可以将其与 pandoc 如何解析其临时 .utf8.md 文件以创建 .tex 文件隔离开来。

以下 Markdown 从 .md 到 .tex 再到 .pdf 被正确解析:

# Data Profile
\begin{table}[htbp]
\centering
\parbox{12cm}{\caption{\small Record Count of Things Summarized in this Table.\label{MyRef}\vspace{4pt}}} 
{\small
\begin{tabular}{llrrr}
Thing & Characteristic & Aspect 1 & Aspect 2 & Aspect 3 \\ 
\hline
Some & data & rows & go & here \\ 
more & data & rows & go & here \\ 
\end{tabular}
}
\end{table}

但是另一点 Markdown ,除了在\caption 周围缺少\parbox(这是 R xtable 包实现其自己的 caption.width 选项的方式)之外,在各方面都与上面的相同,完全被破坏了。相关的备用线路:
\caption{\small Record Count of Things Summarized in this Table.\label{MyRef}\vspace{4pt}}

Rstudio 根据下面的命令将这两个 markdown 块解析为各自的 .tex 块。我对在 pandoc 处理过程中发生这种情况感到满意,因为我可以看到带有和不带有\parbox 的 .utf8.md 文件在其他方面是相同的,但是生成的 .tex 文件不同,其他所有内容(rmarkdown 模板、 pandoc 选项等)保持完全相同。
/usr/local/rstudio-0.98.1103/bin/pandoc/pandoc +RTS -K512m -RTS MyDoc.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output MyDoc.tex --filter /usr/local/rstudio-0.98.1103/bin/pandoc/pandoc-citeproc --template /home/user/R/x86_64-unknown-linux-gnu-library/3.2/MyRmarkdownTemplate/rmarkdown/templates/report/resources/template.tex --highlight-style tango --latex-engine pdflatex --bibliography bibliography.bib

好:
\begin{table}[htbp]
\centering
\parbox{12cm}{\caption{\small Record Count of Things Summarized in This table.\label{MyRef}\vspace{4pt}}} 
{\small
\begin{tabular}{llrrr}
Thing & Characteristic & Aspect 1 & Aspect 2 & Aspect 3 \\ 
\hline
Some & data & rows & go & here \\ 
more & data & rows & go & here \\ 
\end{tabular}
}
\end{table}

坏:
\textbackslash{}begin\{table\}{[}htbp{]} \centering
\textbackslash{}caption\{\small Record Count of Things Summarized in This Table.\label{MyRef}\vspace{4pt}\} \{\small

\begin{tabular}{llrrr}
Thing & Characteristic & Aspect 1 & Aspect 2 & Aspect 3 \\ 
\hline
Some & data & rows & go & here \\ 
more & data & rows & go & here \\ 
\end{tabular}
\} \textbackslash{}end\{table\}

换句话说,出于某种原因,如果没有那个\parbox,pandoc 不会意识到它正在解析 latex ,直到它在\begin{tabular} 之前到达左括号内的\small。使用 parbox,它知道它是\begin{table} 中第一个反斜杠处的 latex 。

所以我的问题是:这是怎么回事?我该如何解决?

最佳答案

原来是标题中的\vspace ,或者至少删除它会导致正确的解析。必须足够非标准以至于 LaTeX 阅读器失败。

请参阅 Yihui 对原始问题的评论。他的链接( https://github.com/jgm/pandoc/issues/2493 )表明 pandoc 的 LaTeX 解析器默默地回退到将有问题的 LaTeX 解释为纯文本,我认为这解释了这里发生的事情。

关于r - pandoc 如何解析 .md 文件中的 latex 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33490078/

相关文章:

r - 将一些函数应用于多个对象

r - 将 R 代码的输出插入到 Rnw 文件中

r - 缩小绘图宽度以为ggrepel标签腾出更多空间

r - 使用 knitr 对 block 的动态调用次数

r - 包含 Markdown 时 Shiny 的主面板宽度

python - 在 python pandas 数据框中将十六进制转换为十进制

R 模式来修改传递给泛型函数的参数

r - 用 R 中的 NA 值聚类

r - 如何重新缩放 bookdown/rmarkdown 网站的本地镜像?

r - 如何在 Rmarkdown 包小插图中嵌入 rgl 快照