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 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.
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.