MichaelChirico
MichaelChirico

Reputation: 34703

Stop knitr "sanitizing" characters in header-includes

I am creating a document where some tables have some columns that benefit from this solution to forcing right-alignment of wrapped-text entries.

To implement that solution I include the following in my preamble:

\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1\linewidth}}

To implement this with knitr, I tried:

---
output:
  pdf_document:
    keep_tex: yes
header-includes:
  - \usepackage{array}
  - \newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1\linewidth}}
---

But this fails with error:

! LaTeX Error: Missing \begin{document}.

...

l.83 \newcolumntype{R}{[}1

pandoc: Error producing PDF

Error: pandoc document conversion failed with error 43

Examining the .tex output, it's clear what's gone wrong:

\usepackage{array}
\newcolumntype{R}{[}1{]}\{\textgreater{}\{\raggedleft\arraybackslash\}p\{\#1\linewidth\}\}

knitr has converted > to \textgreater{}, { to \{, }' to \}, [ to {[}, ] to {]}, and # to \#. I of course want none of these automatic conversions.

This question addressed the same issue, but in that case there was a workaround that solved the issue (quoting in the YAML) -- quoting and escaping \ produces the same output, quoting without escaping \ produces an "unrecognized control sequence" error.

How can I get knitr not to manipulate my input?

Upvotes: 7

Views: 623

Answers (2)

jarauh
jarauh

Reputation: 2006

An alternative, motivated by https://github.com/jgm/pandoc/issues/4473#issuecomment-419667477, is to escape LaTeX code from pandoc (note that I also changed the yaml multiline style to '|' for convenience):

---
output:
  pdf_document:
    keep_tex: yes
header-includes: |
  \usepackage{array}
  ```{=latex}
  \newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1\linewidth}}
  ```
---

Upvotes: 1

Yihui Xie
Yihui Xie

Reputation: 30104

You can (and should) use the includes option (see documentation). An extra benefit is that your LaTeX code will only be applied to the LaTeX output. If you use header-includes in the YAML metadata, it will be applied to all possible output formats, e.g. even when you compile Rmd to HTML.

BTW, knitr does not sanitize any characters in YAML. Pandoc does.

Upvotes: 5

Related Questions