4

I would like to know if it's possible to change the default legend style line legend.

For one of my documents, I'm trying to redefine and harmonize (same width, etc) my legend styles (including line legend) but when I compile it, I get this error message:
Package pgfplots Error: This style is supposed to be constant.
and the change is not taken into account.

Is there a way to modify it?

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}

\pgfplotsset{
compat=newest,
/pgfplots/xbar legend/.style={
    /pgfplots/legend image code/.code={
        \draw [##1,/tikz/.cd,bar width=0.2em,yshift=-0.21em,xshift=-0.1em,bar shift=0pt]
            plot coordinates {(0.6em,0em) (1em,2*\pgfplotbarwidth)
};}},
/pgfplots/ybar legend/.style={
    /pgfplots/legend image code/.code={
        \draw [##1,/tikz/.cd,bar width=0.2em,yshift=-0.31em,bar shift=0pt]
            plot coordinates {(0em,0.3em) (2*\pgfplotbarwidth,0.8em) (4*\pgfplotbarwidth,0.5em)
};}},
/pgfplots/line legend/.style={
    /pgfplots/legend image code/.code={
        \draw [##1, mark repeat=2, mark phase=2, xshift=-0.1em, mark options={scale=0.70}]
            plot coordinates {(0em,0em) (0.5em,0em) (1em,0em)
};}},
}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[legend entries={\strut A,\strut B,\strut C,},legend columns=1]
        \addplot+[ybar, ybar legend, mark=none, fill=red, draw=red!50!black] 
            coordinates {(10,5) (11,6)};
        \addplot+[xbar, xbar legend, mark=none, fill=green, draw=green!50!black] 
            coordinates {(10,5) (11,6)};
        \addplot+[mark=*] 
            coordinates {(10.5,5) (11.5,6)};
    \end{axis}
\end{tikzpicture}
\end{document}

1 Answer 1

2

Of course there is a way to modify it, but apparently the pgfplots authors don't want us to. This is a peculiar decision (and as far as I can tell line legend is the only key that is protected in this way) but let's respect it. Unfortunately, the error message is not very helpful.

What you should do is define a legend style with a different name and activate it. Since line legend sets up the default settings, this overwrites those defaults. (I don't know if the settings are reset anywhere using line legend but I don't think so.)

\pgfplotsset{
  harmonized line legend/.style={
    <your settings>,
  },
  harmonized line legend,
}

Ok, so that's what you should do. If you don't care and just want to overwrite line legend you can remove its protection like this:

\makeatletter
  \expandafter\let\csname pgfk@/pgfplots/line legend/.style/.@cmd\endcsname\undefined
  \expandafter\let\csname pgfk@/pgfplots/line legend/.append style/.@cmd\endcsname\undefined
\makeatother

Now you can redefine the style as any other. Note that you still have to set up your new defaults by executing line legend after you have re-defined it, as above.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .