1

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?

  • 1
    ProvideCommand? – Johannes_B Sep 4 '18 at 9:31
1

You can declare your macro using \providecommand instead of \newcommand which will simply do nothing if the respective macro has been created already.

If you encounter clashes of loaded packages (for instance because you load them in macros.tex and in MWE.tex - for whatever reason) you can use \RequirePackage instead of \usepackage and make macros.tex a package instead of a file that gets included via \input. See here for further information on that.

  • Please note that this would throw errors. TeX is case sensitive. – TeXnician Sep 4 '18 at 13:00
  • 1
    @Raven if you do not place an at sign before the nickname of your interlocutor (as I just did for yours) he does not receive it. So TeXnician didn't get your answer. See: meta.stackexchange.com/a/43020 – AndréC Sep 4 '18 at 20:12
  • I've actually been using both of your suggestions previously. But for some reason I thought using ``\providecommand'' was considered bad-practice? – hakoja Sep 16 '18 at 18:27
  • @hakoja The problem is that you can never be sure whether the respective command is defined the way you want it to or whether it has been defined by someone else in a completely different manner. But in this case you'd have to rely that no one else (in the loaded packages) defines the same macro and therefore you are the only one defining it. Therefore it shouldn't be that much of a problem – Raven Sep 16 '18 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.