Incorrect conversion from R Markdown to LaTeX

懵懂的女人 提交于 2019-12-12 11:41:44

问题


Why does the following R Markdown minimal (non)-working example not compile to PDF?

---
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \lhead{}
- \chead{}
- \rhead{The performance of new graduates}
- \lfoot{From: K. Grant}
- \cfoot{To: Dean A. Smith}
output:
  pdf_document:
    keep_tex: yes
    latex_engine: xelatex
---

# Test

In particular, the problematic conversion happens to -\lfoot{From: K. Grant} and -\cfoot{To: Dean A. Smith}, as seen in the output .tex file:

\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{}
\chead{}
\rhead{The performance of new graduates}
true
true

For some reason, both of these lines are converted to true causing

LaTeX error: Missing \begin{document}

thereby preventing the document from compiling to PDF.

Changing \lfoot and \cfoot to just about anything else seems to lead to them being converted correctly. So what's going on here? I take it that there must be a problem with either knitr or pandoc in the conversion process.

NB: I'm not too familiar with R Markdown, and this is a follow-up question to Headers and footers created in Fancyhead not shown in PDF on TeX.SX on Tom's behalf.


回答1:


The : character is the problem. pandoc seems to be trying to parse the header-includes content as if it were variables, and : is used to separate variables and values. It compiles if you quote the lines in question (don't forget, then, to escape the leading backslash)

---
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \lhead{}
- \chead{}
- \rhead{The performance of new graduates}
- "\\lfoot{From: K. Grant}"
- "\\cfoot{To: Dean A. Smith}"
output:
  pdf_document:
    keep_tex: yes
    latex_engine: xelatex
---

# Test



回答2:


I recommend using pandoc's raw_attribute feature (enabled by default) to mark the raw LaTeX section as such. This is the most robust solution.

header-includes: |
   ```{=latex}
   \usepackage{fancyhdr}
   \pagestyle{fancy}
   \fancyhead{}
   \fancyfoot{}
   \lhead{My Title}
   \rhead{My Note}
   \lfoot{\today}\rfoot{Page \thepage}
   ```



回答3:


Stitching the answer here with trial and error, I found that only by deleting the title key and using YAML pipe-denoted multiline string syntax would it compile:

---
output:
  pdf_document: 
    keep_tex: yes
    latex_engine: xelatex
header-includes: |
   \usepackage{fancyhdr}
   \pagestyle{fancy}
   \fancyhead{}
   \fancyfoot{}
   \lhead{My Title}
   \rhead{My Note}
   \lfoot{\today}\rfoot{Page \thepage}
---


来源:https://stackoverflow.com/questions/30009666/incorrect-conversion-from-r-markdown-to-latex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!