Why does YAML code work in one RMD not work in another?

I suspect I have a fundamental comprehension gap in how R Markdown works under the hood.

This code, to simply make the YAML title bold & blue --

title: **"**\\textcolor{blue}{Font Test}**"**
author: "Office of Institutional Research & Assessment"
header-includes:
  - \usepackage{xcolor}
  - \usepackage[lf]{electrum} 
  - \usepackage[T1]{fontenc}
  - \newcommand*\fontenc{\setmainfont{fontenc}}
  - \newcommand{\mytitle}{\fontec\textcolor{blue}}
  - \pretitle{\vspace{\droptitle}\centering\huge\fontenc}
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: 
  pdf_document:
    includes:
      in_header: mystyle.sty

---

Will work in one RMD file but not in another. The non-working RMD is more complex, with tables (kable, kableExtra) does contain one option for knitr to format tables for latex, but that's it. The non-working RMD uses the same mystyle.sty for the font choice, etc. Hangs up the PDF creation when I use the code for the YMAL title.

Any thoughts? Reading recs?

David Thomas

Silly as it seems, do you have the requisite --- both above and below the code? YAML is whitespace-sensitive, too, so you might try rebuilding it up piece by piece.

Other than the pandoc documentation itself, my best recs would be:


https://rmarkdown.rstudio.com/authoring_pandoc_markdown.html#pandoc_markdown
or the "Common problems when compiling a .pdf" section here:
https://ourcodingclub.github.io/2016/11/24/rmarkdown-1.html

3 Likes

Yes the 3 dashes are both above and below. Thanks for the links!

Total shot in the dark but I have had a few times where I have copied something with a quotation mark from somewhere else and pasted it into my code and it turned out to be a stylized quotation mark, not your run of the mill " or ' that Rmd recognizes.

Any chance one of the parts in quotes of your non-working YAML arent turning yellow (or whatever color your highlighter makes strings)?

Yes, quotes are same --typed, not copied. Here is the error message:

! Undefined control sequence.
\textcolor
{blue}{2017 CDS Calculations}
l.76 ...
\textcolor{blue}{2017 CDS Calculations}}

Try to merge the list of latex packages on this SO answer into your header-includes and the error will be gone.

Alas, yes, I read through the kableExtra PDF and tried the package load method, which continues to throw the same error message -- and rejects LaTex color assignments further down the document between chunks. I don't know enough LaTex to fix this.

I just noticed that you have - \usepackage{xcolor} on your list. Can you try to remove that? For kableExtra, xcolor is loaded with the [table] option.

So your header-includes should look like

header-includes:
  - \usepackage{booktabs}
  - \usepackage{longtable}
  - \usepackage{array}
  - \usepackage{multirow}
  - \usepackage[table]{xcolor} 
  - \usepackage{wrapfig}
  - \usepackage{float}
  - \usepackage{colortbl}
  - \usepackage{pdflscape}
  - \usepackage{tabu}
  - \usepackage{threeparttable}
  - \usepackage[normalem]{ulem}
  - \usepackage[lf]{electrum} 
  - \usepackage[T1]{fontenc}
  - \newcommand*\fontenc{\setmainfont{fontenc}}
  - \newcommand{\mytitle}{\fontec\textcolor{blue}}
  - \pretitle{\vspace{\droptitle}\centering\huge\fontenc}

I get the error even if I eliminate the entire 6 lines devoted to the font selection and title formatting. If its a choice between kableExtra table formats and color headings, I'll choose kableExra.

:joy:LaTeX is always difficult to deal with...

1 Like