Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In the file producing my tikz-graphic, I define a new command that creates a tikzpicture using particular arguments (left out in MWE below because the problem reproduces without those). I use the standalone class for the figure, so that I can more quickly develop the figure without recompiling my entire article, and without cluttering my directory with small files \input-ing the different figures. However, when I \input the standalone-file in my main document, I cannot execute the new command defined inside the standalone-file.

My standalone figure MWE file (called stafig.tex):

\documentclass{standalone}
\usepackage{tikz}
\begin{document}

\newcommand{\examplefig}[0]{%
\begin{tikzpicture}
 \draw (0,0) rectangle (2, 1) node [midway] {Example};
\end{tikzpicture}%
}

\onlyifstandalone{
 \examplefig
 \examplefig
}

\end{document}

I include it in my MWE main article like this:

\documentclass{article}
\usepackage{standalone}
\usepackage{tikz}
\begin{document}

\begin{figure}
 \input{stafig}
 \examplefig
 \caption{Yes, a test}
\end{figure}

\end{document}

The standalone-file compiles fine and has the expected result. However, compiling the main file fails with:

! Undefined control sequence. l.8  \examplefig

In my real case, I want to use this macro to create four plots in different subfloat-environments with different parameters.

My questions:

  • Why is compilation of the main file failing? Is this expected?
  • How could I approach this instead?
  • How would I do if I additionally use pgfkeys?
share|improve this question
2  
BTW, you should avoid empty lines inside the document environment of the standalone file. They create a paragraph break, which will set the width to \linewidth, which is not wanted. –  Martin Scharrer Nov 28 '11 at 9:38

1 Answer 1

up vote 4 down vote accepted

Update 2011/12/21

I now release standalone v1.0 which has a new package option group (=true|false) which can be used to switch of the normally added group using group=false. The patches below are therefore not necessary any longer and should not be used with v1.0. (This can be tested using \makeatletter\@ifpackagelater{2011/12/21}{<v1.0>}{<older>}\makeatother.)


Original answer:

If a standalone file is included using the standalone package and \input its content is placed inside a group, therefore the macro definition is only done local and does no longer exists afterwards. This is by design, but I might add a package option to suppress it in the next version.

You could work around this by defining the macro globally instead. For this use \gdef\examplefig instead of \newcommand{\examplefig} (or \newcommand{\examplefig}[0]).


You can also use the following patch for the standalone package (v0.4a) which removes the group around the file. However, this might break one the package gets updated and is not tested with nested standalone files (i.e. standalone files which \input other standalone files).

\documentclass{article}
\usepackage{standalone}
\usepackage{tikz}

\makeatletter
\let\orig@document\document
\let\orig@enddocument\enddocument
\def\sa@document{%
  \endgroup
  \global\let\enddocument\sa@enddocument
  \sa@atbegindocument
}
\def\sa@enddocument{%
  \sa@atenddocument
  \global\let\document\orig@document
  \global\let\enddocument\orig@enddocument
  \begingroup
  \@ignoretrue
  \def\@currenvir{document}%
  \aftergroup\endinput
}
\makeatother

\begin{document}

\begin{figure}
 \input{stafig}
 \centering
 \examplefig
 \caption{Yes, a test}
\end{figure}

\end{document}
share|improve this answer
    
I understand and your solution works... for the MWE, that is. Not shown in my MWE, I'm using pgfkeys in my real situation, and those keys are also local (that's at least my conclusion from Package pgfkeys Error: I do not know the key '/photontest' and I am going to ignore it) :(. Can I somehow make global what pgfkeys defines? (Meta-Q: Should I open a new question for this, alter the current one, or leave this Q as a comment?) –  gerrit Nov 28 '11 at 10:35
    
@gerrit: The comment is fine at the moment. You could edit the question as well if you want. Also, not need to have MWE always in bold. It is complicated to make PGF keys global. I would rather remove the group instead. See my updated answer for a patch. I note this down as a feature request for the package. –  Martin Scharrer Nov 28 '11 at 10:51
    
Thanks! Works like a charm :) –  gerrit Nov 28 '11 at 11:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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