Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 645
Adam Liter Avatar asked May 03 '15 02:05

Adam Liter


People also ask

Why is my RMarkdown file not knitting?

No Knit HTML button This means that RStudio doesn't understand your document is supposed to be an RMarkdown document, often because your file extension is . txt . To fix this, go to the Files tab (lower right corner, same pane as Plots and Help) and select the checkbox next to your document's name.

Can you use LaTeX in R Markdown?

In our experience, it is also mostly workable to include LATEX code in R markdown documents. Recall that documents in R markdown are converted from Rmd to md by the knitting process, and then into LATEX by pandoc, and then into PDF by a LATEX compiler (pdflatex, xetex or similar).

What can R Markdown be converted into?

The rmarkdown package will use the pandoc program to transform the file into a new format. For example, you can convert your . Rmd file into an HTML, PDF, or Microsoft Word file. You can even turn the file into an HTML5 or PDF slideshow.


2 Answers

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
like image 136
scoa Avatar answered Sep 21 '22 18:09

scoa


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}
   ```
like image 20
John MacFarlane Avatar answered Sep 23 '22 18:09

John MacFarlane