【问题标题】:How does pandoc parse latex code in a .md file?pandoc 如何解析 .md 文件中的乳胶代码?
【发布时间】:2015-11-03 01:43:53
【问题描述】:

我正在使用带有 knitr/rmarkdown/pandoc/latex 的 Rstudio 将 .Rmd 代码呈现为 pdf。我一直在努力解决某些乳胶代码完全按预期呈现的问题,而最终无法正确解析非常不同的代码,这导致我的 .tex 文件包含像 "\textbackslash{}begin{table}" 这样的行而不是 "\begin {表}”。

在处理 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}

但是另外一点降价,除了缺少 \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 直到它到达 \begin{tabular} 之前的左大括号内的 \small 才意识到它正在解析乳胶。使用 parbox,它知道它在 \begin{table} 的第一个反斜杠处是乳胶。

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

【问题讨论】:

  • 这个问题可能和你的类似:github.com/jgm/pandoc/issues/2493
  • 谢谢一辉。我不确定它是否直接相关,但是如果我不知道 Latex 解析器失败,您的链接让我想到了这种无声回退到纯文本的问题。原来是标题内的 \vspace 。如果我删除它,我会在 .tex 文件中得到正确解析的 Latex 代码。
  • 是的,我的意思是后备。请随意接受您自己的答案。

标签: r knitr r-markdown pandoc xtable


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多