1

I'm struggling with getting a custom legend order in my pgfplots groupplot. I tried this (esdd's answer) but I get the error Undefined control sequence. {\patchFailedError}

Stefan Pinnow says one answer later, that it should work the following way:

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{
    matrix,
    pgfplots.external,
    pgfplots.groupplots,
}
\pgfplotsset{compat=1.3}
\tikzexternalize

\begin{document}
\begin{figure} \tikzset{external/force remake} \tikzsetnextfilename{ext} \begin{tikzpicture} \begin{groupplot}[ group style={ group size= 2 by 1, xlabels at=edge bottom, ylabels at=edge left, x descriptions at=edge bottom, }, ] \nextgroupplot[] \addplot [color=black] coordinates {(0,0) (1,0)}; \addplot [color=red] coordinates {(0,1) (1,1)};

        \nextgroupplot[name=aa]
        \addplot [color=black] coordinates {(0,0) (1,0)}; \label{leg:a}
        \addplot [color=red] coordinates {(0,1) (1,1)}; \label{leg:b}

    \end{groupplot}

\matrix [ matrix of nodes, nodes={anchor=west}, anchor=north east, at={([shift={(-3pt,-3pt)}]aa.north east)}, fill=white, draw, inner sep=2pt, row sep=2pt ] { \ref{leg:b} $b$ \ \ref{leg:a} $a$ \ };

\end{tikzpicture}

\end{figure}

\end{document}

But I get this output:

enter image description here

Hence, the plot marks are missing. How can I achieve a custom legend order in my case? Disable externalization is no option (also not for this one plot).

  • off-topic: why you use ancient pgfplots (1.3)? Recent one is now 1.17 ... – Zarko Mar 27 '21 at 10:02
  • I copy and pasted the header from the answer. I updated the version to 1.17 but it doesn't work either – Steradiant Mar 27 '21 at 10:05
  • 1
    There fore I said that the comment is "off-topic" :-) – Zarko Mar 27 '21 at 10:15
  • When I compile your code I do not get your stated error. But I also get the ??s instead of the lines. This is because of the \tikzset{external/force remake}. Remove this for the last compilation run and the lines appear. – Stefan Pinnow Mar 29 '21 at 18:35
  • Thanks, yeah, I already figured that out too with the help of Excelsior's answer. Nevertheless it's annoying that \tikzset{external/force remake} doesn't work properly then. – Steradiant Mar 30 '21 at 06:51

1 Answers1

1

you have to define the position outside the node environment and then it works like expected.

\documentclass{article}

\usepackage{pgfplots}

\usetikzlibrary{matrix} \usepgfplotslibrary{groupplots,external}

\pgfplotsset{compat=newest}

\tikzexternalize

\begin{document}
\begin{figure} \tikzset{external/force remake} \tikzsetnextfilename{ext} \begin{tikzpicture} \begin{groupplot}[ group style={ group size= 2 by 1, xlabels at=edge bottom, ylabels at=edge left, x descriptions at=edge bottom, }, ] \nextgroupplot[] \addplot [color=black] coordinates {(0,0) (1,0)}; \addplot [color=red] coordinates {(0,1) (1,1)};

            \nextgroupplot[name=aa]
            \addplot [color=black] coordinates {(0,0) (1,0)}; \label{leg:a}
            \addplot [color=red] coordinates {(0,1) (1,1)}; \label{leg:b}

        \end{groupplot}

        \matrix [
        matrix of nodes,
        anchor=north east,
        fill=white,
        draw,
        inner sep=2pt,
        row sep=2pt
        ] at {([shift={(-3pt,-3pt)}]aa.north east)} {
            \ref{leg:b} & $b$ \\
            \ref{leg:a} & $a$ \\
        };

    \end{tikzpicture}
\end{figure}

\end{document}

enter image description here

Excelsior
  • 2,322
  • I copied your code to a new file but I get the error Package tikz Error: Cannot parse this coordinate. \end{tikzpicture} – Steradiant Mar 27 '21 at 13:01
  • @Steradiant Strange. Did you also copied the preamble? For me it is working as expected. – Excelsior Mar 27 '21 at 14:34
  • Yes, I copied everything. I figured out, that \tikzset{external/force remake} makes problems. Without this command it works. The drawback is, that I always have to find the correct pdf-file to delete it if I want a remake. – Steradiant Mar 27 '21 at 15:57
  • @Steradiant maybe you can try \documentclass{standalone} (then you have to remove the figure environment) and have a look here (https://tex.stackexchange.com/questions/55760/override-tikz-externalize-up-to-date-flag) – Excelsior Mar 27 '21 at 16:02
  • With your code I cant reproduce showed result. MWE compile fine, but in result is no legend ... – Zarko Mar 27 '21 at 16:56
  • That's no option for me. I have one TeX file which generates all graphics for a bigger main file. I don't want to make a full (or minimal) TeX file with preamble for every graphic. – Steradiant Mar 27 '21 at 16:56