# pgfplots: change legend text color according to plot fill color

I have a (large) set of graphs with (multiple) lines/fills in a large document. The style generally corresponds to the MWE picture below. Many relevant style parameters like colors and line sizes etc. are controlled via a centralized format file.

Now I'm forced to align the legend text color of each fill-entry to the corresponding fill color. Thus, in the legend below, Test 1 should be blue, Test 2 should be red.

An (at least partially) automatic solution is highly appreciated since changing dozens of manually generated legend entries would most likely drive me mad... if possible at all

\documentclass[tikz]{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}

\begin{document}
\begin{tikzpicture}
\begin{axis}[%
legend style={
at={(1.05,0.5)},
anchor=west,
% text = blue,
},
legend cell align={left},
reverse legend,
]

\addplot [name path=T11, color=blue, line width=1pt, forget plot]
table[row sep=crcr]{%
1   1\\
2   2\\
3   3\\
};

\addplot [name path=T12, color=blue, line width=3pt, forget plot]
table[row sep=crcr]{%
2   3\\
3   4\\
4   5\\
};

fill between[ of = T11 and T12 ];

\addplot [name path=T21, color=red, line width=1pt, forget plot]
table[row sep=crcr]{%
1   4\\
2   5\\
3   6\\
};

\addplot [name path=T22, color=red, line width=3pt, forget plot]
table[row sep=crcr]{%
2   6\\
3   7\\
4   8\\
};

fill between[ of = T21 and T22 ];

\end{axis}
\end{tikzpicture}
\end{document}


• You probably know that you could just do \addlegendentry[red]{Test 2}, but you are asking about a solution that automatically adds the correct color, do you? Add a given point you have to define the color of the plot is. You could just store it in a macro and use it in the legend, e.g. \def\mycolor{blue} \addplot[pattern=dots, pattern color=\mycolor] fill between[ of = T11 and T12 ]; \addlegendentry[\mycolor]{Test 1}. – user121799 Sep 14 '18 at 14:43
• thx marmot! Since the colors are already predefined in the real code it would be easy to adopt what you wrote. However, it means manually tweaking many, many code lines. Therefore, I was hoping for something like a legend style in the axis options, like legend style={text=/fillcolor}, to set this for all legend items and then manually color the special lines (which is already implemented in the code) – hanshansen Sep 14 '18 at 15:07
• I really don't know. Here is an answer that shows what you may do to retrieve the last color. This shows that it is possible in principle. But do you really think it is worth the effort? Your problem seems to be easily solvable with a simple regex search and replace. – user121799 Sep 14 '18 at 16:21

Here is a very simple-minded proposal. Why using something simple-minded here? This is because retrieving the color used in the previous plot/path is notoriously difficult, though not impossible (see also this answer). I'd like to argue that one may want to refrain from injecting code at such a deep level. My proposal is far less sophisticated, but of course also less powerful. All you need to do in you code is to replace all instances of color=... by my color=..., and likewise for pattern color. And you have to replace \addlegendentry{Test 1} by \addlegendentry[\mycolor]{Test 1}, and of course add my shabby tikz styles in the beginning.

\documentclass[tikz]{standalone}
\tikzset{mycolor/.code=\xdef\mycolor{#1},my color/.style={mycolor=#1,color=#1},
my pattern color/.style={mycolor=#1,pattern color=#1}}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}

\begin{document}
\begin{tikzpicture}
\begin{axis}[%
legend style={
at={(1.05,0.5)},
anchor=west,
% text = blue,
},
legend cell align={left},
reverse legend,
]

\addplot [name path=T11,my color=blue, line width=1pt, forget plot]
table[row sep=crcr]{%
1   1\\
2   2\\
3   3\\
};

\addplot [name path=T12,my color=blue, line width=3pt, forget plot]
table[row sep=crcr]{%
2   3\\
3   4\\
4   5\\
};

fill between[ of = T11 and T12 ];

\addplot [name path=T21, color=red, line width=1pt, forget plot]
table[row sep=crcr]{%
1   4\\
2   5\\
3   6\\
};

\addplot [name path=T22, color=red, line width=3pt, forget plot]
table[row sep=crcr]{%
2   6\\
3   7\\
4   8\\
};