Adam Liter
Adam Liter

Reputation: 925

Incorrect conversion from R Markdown to LaTeX

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.

Upvotes: 5

Views: 5891

Answers (3)

John MacFarlane
John MacFarlane

Reputation: 8947

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}
   ```

Upvotes: 2

dcmorse
dcmorse

Reputation: 1181

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}
---

Upvotes: 2

scoa
scoa

Reputation: 19857

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

Upvotes: 8

Related Questions