2015-11-03 29 views
2

我正在使用Rstudio與knitr/rmarkdown/pandoc/latex將.Rmd代碼呈現爲pdf。我一直在努力處理某些與預期完全一樣的乳膠代碼,而稍微不同的代碼最終沒有正確解析,這導致我的.tex文件包含諸如「\ textbackslash {begin} {}}」之類的行,而不是「\ begin {表}」。pandoc如何解析.md文件中的latex代碼?

谷歌搜索在處理HTML時揭示了類似的錯誤解析,但我直接從.Rmd到.md到.tex到.pdf。

這一切都取決於我使用的Rstudio的特定版本/平臺,以及R包knitr,xtable,rmarkdown,rmarkdown模板等,所以我一直在努力想出來MWE。

(我做了檢查男,pandoc Y的版本> = 1.13,因爲谷歌搜索建議有早期版本中可能涉及的錯誤。)

不過,我現在已經有了一個有點MWE是我至少可以分離出pandoc如何解析它的臨時.utf8.md文件來創建.tex文件。

下降價是從.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} 

但降價的另一位,這是多方上面有什麼,除了缺乏對周圍的\ parbox相同\ caption(這是R xtable包如何實現自己的caption.width選項)完全被破壞。相關的備用線:

\caption{\small Record Count of Things Summarized in this Table.\label{MyRef}\vspace{4pt}} 

這兩個降價塊被根據下面的命令由Rstudio到相應.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}之前的開口大括號內的\小。使用parbox,它知道它是在\ begin {table}的第一個反斜槓處的乳膠。

所以我的問題是:這是怎麼回事?我該如何解決它?

+1

此問題可能與您的相似:https://github.com/jgm/pandoc/issues/2493 –

+0

謝謝,Yihui。我不確定它是直接相關的,但是如果Latex解析器失敗,我的連接讓我想到了這種沉默的回退,但我沒有意識到這一點。原來是標題內的\ vspace。如果我刪除它,我會在.tex文件中正確解析Latex代碼。 –

+1

是的,我的意思是後備。請隨時接受你自己的答案。 –

回答

2

結果是\ vSpace裏面的標題,或至少刪除,導致正確的解析。必須不夠標準,LaTeX閱讀器纔會失敗。

查看Yihui對原始問題的評論。他的鏈接(https://github.com/jgm/pandoc/issues/2493)表明,pandoc的LaTeX解析器默默地回退到將有問題的LaTeX解釋爲純文本,我想這解釋了發生了什麼。