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.

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
add comment

1 Answer

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

\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}
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
show 2 more comments

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.