pandoc如何解析.md文件中的LaTeX代码?

4
我正在使用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版本>=1.13,因为搜索引擎提示早期版本中可能存在相关错误。)
然而,我现在至少得到了一个可以隔离Pandoc如何解析其临时.utf8.md文件以创建.tex文件的MWE。
以下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}}

以下命令将这两个Markdown代码块解析为相应的.tex代码块。在Pandoc处理期间,我已经确认了这一点,因为我可以看到带有\parbox和没有\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}

Bad:

\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,它就知道它是在第一个反斜杠处解析 LaTeX 的 \begin{table}。
所以我的问题是:这是怎么回事?我该如何解决?

2
这个问题可能与你的类似:https://github.com/jgm/pandoc/issues/2493 - Yihui Xie
谢谢,Yihui。我不确定它是否直接相关,但是你的链接让我思考了一下当Latex解析器失败时,自动回退到纯文本的情况,这是我之前不知道的。结果发现是在标题中的\vspace引起的问题。如果我将其移除,.tex文件中的Latex代码就能正确解析了。 - Paul Obrecht
1
是的,我指的是备用方案。请随意接受您自己的答案。 - Yihui Xie
1个回答

2
原来问题出在标题内的 \vspace 命令,或者说将其移除后可以正确解析。可能是因为这个命令不够标准,导致 LaTeX 解析器失败。
可以参考 Yihui 在原问题中的评论。他提供的链接(https://github.com/jgm/pandoc/issues/2493)显示,pandoc 的 LaTeX 解析器会悄悄地将有问题的 LaTeX 代码解释成纯文本,这应该可以解释这里发生的情况。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接