Margaret
Margaret

Reputation: 5919

Pulling YAML parameters into a LaTeX preamble for a RMarkdwon document

I freely admit I know pretty much no LaTeX, so this may be a very basic question.

My company produces reports using RMarkdown. To apply styling, including a title page, there is a separate preamble.tex file which defines them.

However, a number of values in the preamble.tex file are hard coded. Here's an example of the result:

enter image description here

However, what I would like to do is pass these "from tex" values in as parameters from the RMarkdown code (presumably as YAML values?). Is this possible? If so, how?

If it helps, I've uploaded the code used to run the above (along with a copy of the PDF it generates) here: http://mm-c.me/work/RMarkdown-LaTeX/

Upvotes: 2

Views: 886

Answers (1)

user2554330
user2554330

Reputation: 44788

That's not very easy, but you can do it by modifying the Pandoc template.

The default templates for LaTeX output are in system.file("rmd/latex", package = "rmarkdown"). Unless you have quite an old version of Pandoc, you probably want the one named default-1.17.0.2.tex. (The number is the Pandoc version.)

As described on this page: https://bookdown.org/yihui/rmarkdown/template-pandoc.html, you can modify this template to include additional parameters.

Normally, your

includes:
  in_header: preamble.tex

would put the preamble.tex file just before \begin{document}, so what you can do is put a copy of it there yourself, replacing each of the arguments like title from tex with Pandoc variables like $titlefromtex$, e.g.

\fancyhead[L]{\color{clientdark}\small $titlefromtex$ \textbar{ } $periodfromtex$}

Save the new file in template.tex, then in your YAML, put in top level arguments with matching names and a pdf_document argument naming the template, e.g.

output:
  pdf_document:
    latex_engine: xelatex
    fig_caption: yes
    keep_tex: true
    template: template.tex
  html_document:
    df_print: paged
titlefromtex: The Title
periodfromtex: The Period
versionfromtex: The Version
descriptionfromtex: The Description

Upvotes: 2

Related Questions