[Tex/LaTex] pgfplots: change legend text color according to plot fill color

colorlegendpgfplots

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}
\pgfplotsset{compat=newest}
\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\\
                        };

            \addplot[pattern=dots, pattern color=blue]
                fill between[ of = T11 and T12 ];
            \addlegendentry{Test 1}

            \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\\
                        };

            \addplot[pattern=north east lines, pattern color=red]
                fill between[ of = T21 and T22 ];
            \addlegendentry{Test 2}

            \addlegendimage{line legend,black,line width=1pt}
            \addlegendentry{Special 1}

            \addlegendimage{line legend,black,line width=3pt}
            \addlegendentry{Special 2}

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

enter image description here

Best Answer

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}
\pgfplotsset{compat=newest}
\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\\
                        };

            \addplot[pattern=dots,my pattern color=blue]
                fill between[ of = T11 and T12 ];
            \addlegendentry[color=\mycolor]{Test 1}

            \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\\
                        };

            \addplot[pattern=north east lines,my pattern color=red]
                fill between[ of = T21 and T22 ];
            \addlegendentry[color=\mycolor]{Test 2}

            \addlegendimage{line legend,my color=black,line width=1pt}
            \addlegendentry[color=\mycolor]{Special 1}

            \addlegendimage{line legend,my color=black,line width=3pt}
            \addlegendentry[color=\mycolor]{Special 2}

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

enter image description here