5

Using PGFplots, I would like to apply a decoration to a plot but not to the corresponding legend entry. Is there a way to do this?

Here is an example: I'd like the legend image to be just the line, without the decoration "test".

example of decoration appearing in legend

\documentclass{standalone}

\usepackage{tikz,pgfplots}
\usetikzlibrary{decorations.markings}
\pgfplotsset{compat=1.12}

\makeatletter
\tikzset{reset postactions/.code=\let\tikz@postactions\pgfutil@empty}
\makeatother
\tikzset{test decoration/.style={
 postaction={
  decorate,
  decoration={
   markings,
   mark=between positions 0.5 and 1 step 10mm with {\node {test};}
  }
 }
}}

\begin{document}
 \begin{tikzpicture}
  \begin{axis}[legend image post style={reset postactions}]
   \addplot[blue,test decoration] {-x};
   \legend{$-x$};
  \end{axis}
 \end{tikzpicture}
\end{document}

The reset postactions style (which is ineffective) comes from this answer. I've also tried resetting the test decoration style to empty inside legend image post style, with no effect.

David Z
  • 12,084
  • 7
  • 48
  • 65

1 Answers1

3

A workaround could be to use forget plot and \addlegendimage.

\documentclass{standalone}

\usepackage{tikz,pgfplots}
\usetikzlibrary{decorations.markings}
\pgfplotsset{compat=1.12}

\tikzset{test decoration/.style={
 postaction={
  decorate,
  decoration={
   markings,
   mark=between positions 0.5 and 1 step 10mm with {\node {test};}
  }
 }
}}

\begin{document}
 \begin{tikzpicture}
  \begin{axis}[myplot/.style={blue}]
   \addplot[myplot,test decoration,forget plot] {-x};
   \addlegendimage{myplot}

   \legend{$-x$};
  \end{axis}
 \end{tikzpicture}
\end{document}

enter image description here

Torbjørn T.
  • 206,688
  • Not quite the elegant solution I was hoping for, but that will do the trick. Thanks! I'll see if anyone else comes up with something. – David Z Apr 25 '16 at 11:58