# Determine if within a PGF axis environment

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.

-
interesting, is there a concrete example where one may need this? –  alfC May 6 '11 at 5:01
I have defined a macro to label certain points in my graph. Since the sytnax changes whether I am within the axis environment or not, I want to use the same macro in my code and have it expand to different code. Is this not a good approach? –  Peter Grill May 6 '11 at 6:08
@alfC: One concrete example now at new command, pfgkeys within pfgplots axis environment. –  Peter Grill Mar 13 at 18:49

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}

-
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 5 '11 at 5:29
@Unfortunately no, I think, there's no manual for the implementation side of things. The code is quite well commented, though. –  Jake May 5 '11 at 5: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 5 '11 at 21:31
@Peter: It should be used as \pgfplotsifinaxis{code if inside}{code if outside}. –  Caramdir May 5 '11 at 22:54
Yes, that works much better. –  Peter Grill May 5 '11 at 23:34