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 option in pgfplots at the axis level or a setting of legend style option, that would effectively ignore the subsequent legend entries in the plot?

\documentclass{article} \usepackage{pgfplots} 
\begin{document} \begin{tikzpicture}
  \begin{axis}[legend style = {at={(1.03,-0.03)}},
        %ignore legend %hypothetical option
    ]
    \addlegendentry{graph 1} %ignored
    \addplot+[mark=none,fill=yellow,draw=none,forget plot] {0.1*x^2};
    \addlegendentry{graph 2} %ignored
    \addplot[mark=none,draw=red,line legend] {0.1*x^2};
    \addlegendentry{graph 3} %ignored
    \addplot[mark=none,draw=blue,line legend] {0.0*x^2};
  \end{axis}
\end{tikzpicture} \end{document}

I have a large plot (many curves) and I want to remove all the legends at the same time, without modifying the code too much.

share|improve this question
    
As a quick hack you can skip all the \addlegendentry commands by assigning it to \relax. So put every axis legend/.code={\let\addlegendentry\relax} to the hypothetical line and it will locally cancel all the legend entries. –  percusse Oct 20 '12 at 21:06
    
@percusse, yes, I was looking for a hack like this. However when I add your option, the whole text for legends appears in text mode before a plot with no legends. –  alfC Oct 20 '12 at 21:26
    
Strange I don't see anything. I'm using v1.6. –  percusse Oct 20 '12 at 21:34
    
@percusse: Your suggestion seems to work just fine for me as well. I am not able to reproduce the problem that alfC experiences with your approach. –  Peter Grill Oct 20 '12 at 21:37
    
@PeterGrill Maybe it's a better idea to gobble the argument too to avoid those cases (though I can't replicate it) but I'll wait in case Christian chips in. –  percusse Oct 21 '12 at 4:12

1 Answer 1

up vote 8 down vote accepted

Here is what can be done for your problem.

Option 1

As suggested by percusse every axis legend/.code={\let\addlegendentry\relax} can be used to suppress legends. Following code shows their use both locally and globally.

Code:

\documentclass{article} 
\usepackage{pgfplots}
%\pgfplotsset{every axis legend/.code={\let\addlegendentry\relax}} %%%if you don't want them globally, uncomment this line

\begin{document} 
\begin{tikzpicture}
  \begin{axis}[legend style = {at={(1.03,-0.03)}},
     every axis legend/.code={\let\addlegendentry\relax}   %ignore legend locally 
    ]
    \addlegendentry{graph 1} %ignored
    \addplot+[mark=none,fill=yellow,draw=none,forget plot] {0.1*x^2};
    \addlegendentry{graph 2} %ignored
    \addplot[mark=none,draw=red,line legend] {0.1*x^2};
    \addlegendentry{graph 3} %ignored
    \addplot[mark=none,draw=blue,line legend] {0.0*x^2};
  \end{axis}
\end{tikzpicture} 
\end{document}

enter image description here

Option 2

As suggested by Qrrbrbirlbel, you can define a new style by

\pgfplotsset{ignore legend/.style={every axis legend/.code={\let\addlegendentry\relax}}}

in the preamble and now you can use your hypothetical ignore legend as you wished.

Code:

\documentclass{article} 
\usepackage{pgfplots}
\pgfplotsset{ignore legend/.style={every axis legend/.code={\let\addlegendentry\relax}}}
\begin{document} 
\begin{tikzpicture}
  \begin{axis}[legend style = {at={(1.03,-0.03)}},
        ignore legend %hypothetical option! NOT ANYMORE
    ]
    \addlegendentry{graph 1} %ignored
    \addplot+[mark=none,fill=yellow,draw=none,forget plot] {0.1*x^2};
    \addlegendentry{graph 2} %ignored
    \addplot[mark=none,draw=red,line legend] {0.1*x^2};
    \addlegendentry{graph 3} %ignored
    \addplot[mark=none,draw=blue,line legend] {0.0*x^2};
  \end{axis}
\end{tikzpicture} 
\end{document}

As Christian Feuersänger writes in his comment,

It is save to overwrite this macro to no-operation. However, your solution does not really overwrite it to no-operation, because it results in \relax{graph 1} - and {graph 1} is still processed by TeX. In your case, the characters are silently ignored... but if you write complicated things in the argument, it might fail.

and his suggestion is to use

\pgfplotsset{ignore legend/.style={every axis legend/.code={\renewcommand\addlegendentry[2][]{}}}}

instead of

\pgfplotsset{ignore legend/.style={every axis legend/.code={\let\addlegendentry\relax}}}

since \renewcommand takes care of both the mandatory and the optional argument of \addlegendentry.

Option 3

If you are freshly writing the code, then simply omit the command \addlegendentry{graph 3} etc.

share|improve this answer
    
I'd go the whole nine yards and use \pgfplotsset{ignore legend/.style={every axis legend/.code={\let\addlegendentry\relax}}} in the preamble so that the OP can use their “hypothetical” option ignore legend. –  Qrrbrbirlbel Oct 20 '12 at 23:53
    
Note that this is a very questionable hack and might be breaking stuff :). I'd rather wait for Christian to approve since he knows it inside out. –  percusse Oct 21 '12 at 0:34
    
@HarishKumar It is save to overwrite this macro to no-operation. However, your solution does not really overwrite it to no-operation, because it results in \relax{graph 1} - and {graph 1} is still processed by TeX. In your case, the characters are silently ignored... but if you write complicated things in the argument, it might fail. –  Christian Feuersänger Oct 25 '12 at 8:55
1  
@HarishKumar In your case, it would work if you write \renewcommand\addlegendentry[2][]{} instead of \let...\relax because \renewcommand takes care of both the mandatory and the optional argument of \addlegendentry –  Christian Feuersänger Oct 25 '12 at 14:30
    
As pointed out by @ChristianFeuersänger, the \relax can lead to unintentional stuff. I had probelms, just by having math symbols in my legend (which I consider pretty standard and nothing exotic), e.g., $\gamma=0.5$. The \renewcommand option is definitely the better one. I think you should remove the \relax solution entirely. –  Clausen Jun 14 at 0:09

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.