Incorrect conversion from R Markdown to LaTeX - 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.

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

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

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

Related

RMarkdown ! Undefined control sequence

I am writing my PhD thesis in RMarkdown. Everything was rendering fine yesterday, today I added a few pieces of text with one reference (deleting it does not change the error message) and now, I get this error message:
! Undefined control sequence. ...9}\selectfont {\global
\setmainfont
{DejaVu Sans}{}} l.3593 ...nt{\global\setmainfont{DejaVu Sans}{}}}}
& \multicolumn{3}{!{\colo...
The document consists of several child documents. I managed to track the error to one of the child documents by switching the individual child chunks off and on in the main document. BUT the line the error refers to seems to be from A DIFFERENT child document. All of the child documents knit OK when I try them separately.
I can get rid of the error when I use xelatex as the pdf engine but I would prefer to stick with pdflatex because of compatibility with some R packages.
My YAML:
title: ''
output:
pdf_document:
latex_engine: pdflatex
number_sections: TRUE
pandoc_args: ['--lua-filter=section-bibliographies.lua']
always_allow_html: true
geometry: "left = 4cm, right = 2cm, top = 2cm, bottom = 2cm"
fontsize: 11pt
header-includes:
- \usepackage{float}
- \usepackage{sectsty}
- \usepackage{paralist}
- \usepackage{setspace}\spacing{1.5}
- \usepackage{fancyhdr}
- \usepackage{lastpage}
- \usepackage{dcolumn}
- \usepackage{natbib}
- \usepackage[nottoc, numbib]{tocbibind}
- \usepackage{ragged2e}
- \usepackage{pdflscape}
- \usepackage{textgreek}
- \usepackage{graphicx}
- \usepackage{titlesec}
classoption: twoside
bibliography: library.bib
reference-section-title: References
section-bibs-level: 2
citation_package: biblatex
csl: journal-of-ecology.csl
Any suggestion will be much appreciated!

Latex in document referencing via Varioref is not working in Rmarkdown

I've been using vanilla latex documents for a long time and have recently switched over to Rmarkdown. I'm struggling to get varioref to work, or any in-document referencing for that matter. See below example Rmarkdown file contents. I've tried to make it a minimally reproducible example.
---
title: "vref"
output:
pdf_document:
keep_tex: true
latex_engine: pdflatex
header-includes: \usepackage{varioref}
---
Please see \vref{section}
\newpage
\section{This is a section} \label{section}
It seems like the reference is being created with a clickable link, but there is no reference to the section being created..
Rmarkdown producing unnumbered sections has nothing to do with varioref. You can make them numbered like this:
---
title: "vref"
output:
pdf_document:
keep_tex: true
latex_engine: pdflatex
header-includes:
- \usepackage{varioref}
- \setcounter{secnumdepth}{4}
---
Please see \vref{section}
\newpage
\section{This is a section} \label{section}

Fancyhead for unnumbered appendix in R Markdown knit to pdf

A question about fancyhdr in R Markdown knitting to pdf.
Can anyone help me to fix the header for the appendix? I have numbered sections in the document, and the fancyhead for pages with the numbered sections is oke. But for the unnumbered appendix, it keeps showing the last numbered section, where I just want the title of the current unnumbered appendix to be shown. Code example below. Thanks!
---
title: "Example"
output:
pdf_document:
number_sections: true
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
---
\newpage
# Chapter
\newpage
# Appendix {-}
You can adjust the header like this:
---
title: "Example"
output:
pdf_document:
number_sections: true
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
---
\newpage
# Chapter
\newpage
# Appendix {-}
\markboth{something here}{something there}

Include after_body in toc with bookdown

I am creating a pdf book, and want to include the after_body header in the toc. I use the following yaml:
---
author: "name"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
geometry: "left=4cm,right=3cm,top=3cm,bottom=3cm"
subparagraph: true
output:
bookdown::pdf_book:
toc: false
citation_package: natbib
includes:
before_body: frontpage.tex
after_body: after_body.tex
in_header: preamble.tex
fontsize: 11pt
linestretch: 1.2
documentclass: book
bibliography: [packages.bib, library.bib]
biblio-style: apalike
link-citations: yes
---
In the after_body.tex i have the following:
\backmatter
\begin{titlepage}
\LARGE
\textbf{Enclosed articles I - III}
\end{titlepage}
How do I include the "Enclosed articles I - III" in the toc? I create the toc with \tableofcontents before \mainmatter in the index.Rmd file.
Edit: I also noticed that bookdown creates an empty page with a page number after the \titlepage latex environment. This also happens in the frontpage.tex. Is there any way to remove the numbers on these pages?
I managed to do this by changing the \textbf to \chapter, and include this in the preamble.tex:
\usepackage{fancyhdr}
\pagestyle{fancy}
\setlength{\headheight}{13.6pt} % as requested by fancyhdr's warning
\renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}}
\makeatletter
\renewcommand{\chaptermark}[1]{%
\if#mainmatter
\markboth{Chapter \thechapter{}: #1}{}%
\else
\markboth{#1}{}%
\fi
}
As taken from this post

Modify footline in Beamer - R Markdown

I'm actually using the CambridgeUS theme for my Beamer using RMarkdown.
output:
beamer_presentation:
theme: "CambridgeUS"
header-includes:
- \usepackage{color}
- \usepackage{amsmath}
- \setbeamertemplate{navigation symbols}{}
I wanna know how to delete the date in the footline and how to change the value taken as the full name (author) to only take last name.
It's possible using only tex as here but I don't succeed using RMarkdown.
Do you already face this issue ?
To remove the date from the footline, you could specify en empty optional argument in \date[]{\today}.
To show only the last name in the footline, define a short version for the author: \author[Smith]{Jack Smith}
\documentclass{beamer}
\usetheme{CambridgeUS}
\date[]{\today}
\author[Smith]{Jack Smith}
\begin{document}
\begin{frame}
abc
\end{frame}
\end{document}
The identical code also works in rmarkdown:
---
output:
beamer_presentation:
theme: "CambridgeUS"
header-includes:
- \setbeamertemplate{navigation symbols}{}
- \date[]{\today}
- \author[Smith]{Jack Smith}
---
test

Resources