1

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.

1 Answer 1

1

After some trial and error, I have found a solution to both my problems.

It seems that it is necessary to have a matching invoke after crossref tikzpicture to the invoke before crossref tikzpicture command.

But I can't find where that would be stated in the manual, so take it with a grain of salt.

Additionally, only when using the mode=list and make option for externalization is the output generated as (somewhat) expected. This will get rid of the error.

Following is an example of what the full code looks like:

\documentclass{article}
\usepackage{lipsum}

\usepackage{pgfplots}

\usetikzlibrary{external}
\tikzexternalize[
    prefix=TikzCompiledFigures/,
    mode=list and make,
]
\pgfplotsset{
    compat=1.18,
    samples=10,
    invoke before crossref tikzpicture={
        \tikzset{%
            every node/.append style={%
                font=\Huge
            },%
        }%
    },
    invoke after crossref tikzpicture={
        \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=\tiny,
},
]
\addplot {x};
\addplot {x+1};
\end{axis}
\end{tikzpicture}%
\caption{\footnotesize Test: $x$ %
\ref{named}
.
}
\end{figure}

\end{document}

BUT this still does not help in typesetting the external legend in the desired font size.

For that, I used the package relsize as shown in this answer.

\documentclass{article}
\usepackage{lipsum}

\usepackage{pgfplots}

\usetikzlibrary{external}
\tikzexternalize[
    prefix=TikzCompiledFigures/,
    mode=list and make,
]
\pgfplotsset{
    compat=1.18,
    samples=10,
}
% This is what was propsed
\usepackage{relsize}
\newcommand\scaleInNode[1][1]{\tikzset{execute at begin node={\normalsize\larger[#1]}}}
% to here


\begin{document}
\lipsum[1] %

$x$ %
% And here is one example of how it can be used
{\scaleInNode[0]\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=\scriptsize,
},
]
\addplot {x};
\addplot {x+1};
\end{axis}
\end{tikzpicture}%
\caption{%
    Test: $x$ %
    % Here, two legends with different font sizes are added:
    {\ref{named}}
    {\scaleInNode[0]\ref{named}}
    .
}
\end{figure}

\end{document}

Now I can scale every single "legend to name"-reference to the desired font size.

Result of "name to legend" in caption w/ different font sizes.

I hope this will help someone with their problem in the future.

You must log in to answer this question.

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