I would like to use the "legend to name" feature of pgfplots with externalize. This works fine if I don't use the "invoke before crossref tikzpicture" which I use to set the desired fontsize (per crossreference, I specifically want to be able to change this independently from what is set for the respective pgfplot picture itself i.e. what is set via "every axis legend/.append style").
When using the combination of externalize and "invoke before" I get the following error:
! Extra \endgroup.
\pgfplots@legend@to@name@named ...i \fi \endgroup
l.26 \ref{named}
Using externalize without using "invoke before.." yields the expected (but not desired) result.
Following is a MWE, where the first 4 commented out lines have to be commented in to produce the error (both with or without externalizing to a sub-folder). Alternatively, the "invoke before" section can be commented out, then externalize works perfectly.
\documentclass{article}
\usepackage{lipsum}
\usepackage{pgfplots}
% \usetikzlibrary{external}
% \tikzexternalize[
% %prefix=TikzCompiledFigures/,
% ]
\pgfplotsset{
compat=1.18,
samples=10,
invoke before crossref tikzpicture={% <--- "invoke before" is used to change the fontsize according to the surrounding text
\tikzset{%
every node/.append style={%
font=\normalsize
}
}
}
}
\begin{document}
\lipsum[1] %
$x$ %
\ref{named}
\lipsum[2]
\begin{figure}[t]
\centering
%
\begin{tikzpicture}
\begin{axis}[
width=7cm,
legend columns=-1,
legend entries={$(x+0)^k$;,$(x+1)^k$},
legend to name=named,
every axis legend/.append style={
draw=none,
font=\Huge,
},
]
\addplot {x};
\addplot {x+1};
\end{axis}
\end{tikzpicture}
%
\caption{\footnotesize Test: $x$ %
\ref{named}
.
}
\end{figure}
\end{document}
I know that sometimes disabling externalize helps, as shown in this question, but in this case it does not mitigate the problem. In fact, one of the solutions using "invoke before" to disable externalize, as suggested in one of the comments, produces the same error I am facing.
I hope someone can help me here. Every hint is greatly appreciated.
For completeness sake: This question is a followup question to this one: Alignment of pgfplot inline legend in body text and caption.