5

Is there an easy way to test whether a macro is called from within an PGF \begin{axis}...\end{axis} environment? One solution would be to define a new environment myself that sets a flag at \begin{axis} and clears it as \end{axis}, but I'd prefer not to have to do that.

Caramdir
  • 89,023
  • 26
  • 255
  • 291
Peter Grill
  • 223,288

1 Answers1

5

pgfplots provides a command \pgfplotsifinaxis that tests whether the key pgfplots@is@in@axis is defined and invokes code according to the outcome.

It is defined in pgfplots.code.tex as

\long\def\pgfplotsifinaxis#1#2{%
    \pgfutil@ifundefined{pgfplots@is@in@axis}{#2}{#1}%
}%

It is used as

\pgfplotsifinaxis{code if in axis}{code if outside axis}
Jake
  • 232,450
  • Works great. Is there a way to look this kind of thing up without looking through the code? I can't find any mention of it the 2.10 TikZ-PGF manual. – Peter Grill May 05 '11 at 05:29
  • @Unfortunately no, I think, there's no manual for the implementation side of things. The code is quite well commented, though. – Jake May 05 '11 at 05:31
  • I think I was too quick to accept this solution. This seems be defined in an {axis} environment, but is also defined outside? Am I using it incorrectly? See MWE added to original question. – Peter Grill May 05 '11 at 21:31
  • @Peter: It should be used as \pgfplotsifinaxis{code if inside}{code if outside}. – Caramdir May 05 '11 at 22:54
  • Yes, that works much better. – Peter Grill May 05 '11 at 23:34
  • @Peter: I removed your MWE, in order to avoid confusing people who have the same problem and look for a solution here. @Jake: Maybe you can add how the macro is used to your answer. – Caramdir May 06 '11 at 01:19
  • @Caramdir: Done! – Jake May 06 '11 at 07:41