TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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 '13 at 18:49
up vote 4 down vote accepted

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


It is used as

\pgfplotsifinaxis{code if in axis}{code if outside axis}
share|improve this answer
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

Your Answer


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.