This is a problem I encounter frequently, and for which I don't have a good (i.e., scalable and automatic) solution.
Suppose I have a main document MWE.tex
, a macro file with custom commands macros.tex
, and a standalone file with a Tikz figure tikz_external.tex
. In macros.tex
there are commands that I want to use in both the main document, as well as in the external standalone file. Moreover, the Tikz figure requires some additional tikz library. The examples are shown below.
% MWE.tex
\documentclass{article}
\usepackage{tikz}
\usepackage[subpreambles]{standalone}
\input{macros}
\begin{document}
\includestandalone[width=0.75\linewidth]{tikz_external}
\end{document}
.
% macros.tex
\newcommand{\foo}{FOO}
.
% tikz_external.tex
\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta} % needed for Latex arrowhead style
\input{macros}
\begin{document}
\begin{tikzpicture}
\draw[->,>=Latex] (0,0) -- node[above] {\foo} (1,0);
\end{tikzpicture}
\end{document}
Now in the main file (MWE.tex
) this fails to compile, giving the error:
Command \foo already defined. \newcommand{\foo}{FOO}
However, if I remove the subpreambles
option in MWE.tex
I instead get the following error:
Package pgf Error: Unknown arrow tip kind 'Latex'. \draw[->,>=Latex]
To solve this, I can include the Tikz library arrows.meta
into MWE.tex
.
However, I don't like this because now MWE.tex
has a dependency on the internals of tikz_external.tikz
, and you could imagine there being hundreds of external Tikz files, with a varying number of Tikz libraries needed. In order to compile, MWE.tex
would have to import all of these libraries.
I guess a third option could be to include the external Tikz figures as PDFs in the main document. But then I have to recompile the external Tikz files every time I make a change to them, which loses the benefit of building everything from within the main file.
My question: How should I do this properly? I.e., ideally, I want to be able to compile the external Tikz figures separately so I can work on them on their own if I want to. But I also want the ability to compile the entire document, without having to re-compile the external files. And I don't want to important all the Tikz library dependencies into the main file. Is this possible?