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?
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.
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}}
```
---
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With