Loading macros from external file into tikz chunks

How do I load LaTeX macros from an external file into my TikZ code chunks? I can load them into my main document file in the YAML header, but they are not passed through to the TikZ chunks.

Thanks for any help! I'm just getting started with bookdown / R markdown and I'm still learning the ropes.

You should be able to provide some option to the chunk.
One of them is changing the template used - see this knitr example knitr-examples/058-engine-tikz.Rmd at master · yihui/knitr-examples · GitHub

You can pass some options to the engine by defining engine.opts, e.g. use your own template instead of the default one to include the tikz code: engine.opts = list(template = "mytemplate.tex"). The default template can be found under system.file('misc', 'tikz2pdf.tex', package = 'knitr').

Providing your own template would allow you to customize the content based on your external file.

Some chunk options are passed to the template and you may not it to provided your own:

  • classoption passed to %% TIKZ_CLASSOPTION %%: use that for passing class option to standalone document class
  • extra.preamble passed to %% EXTRA_TIKZ_PREAMBLE_CODE %%: use to add any content in preamble. You can do extra.preamble = xfun::read_utf8("external.tex") to read from an external file. Maybe that is what you look for.

Hope it helps

1 Like

Great, thank you, extra.preamble was what I needed. For anyone who finds this thread in future, I had to pass this in engine.opts, not in the chunk header.

Oh you're right. It is option to pass to engine.opts. I forgot to mention.

Glad it is working !

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.