I'm trying to create a plot, using pgfplots and the externalize library, which contains a \label to a plot and a corresponding \ref in a legend.
I'm also using the frontespizio package to create the first page.
When I run pdflatex the first time, the reference in the plot is not resolved and I get the usual ?? where the \ref should be.
After the second run (plus a pdflatex test-frn to create the PDF of the frontispice), that terminates without any error, the label gets placed, but the plot appears with the frontispice (which then appears two times in the output) in the background.
Here's a MWE:
\documentclass{article}
\usepackage{frontespizio}
\usepackage{pgfplots}
\pgfplotsset{%
compat = 1.14,
}
\usepgfplotslibrary{groupplots}
\usetikzlibrary{external}
\usepgfplotslibrary{external}
\tikzexternalize
\begin{filecontents}{frontespizio-test.tex}
\begin{frontespizio}
\Universita{X}
\Facolta{prova}
\Corso{x}
\Titoletto{tesi}
\Titolo{titolo}
\Candidato{Io}
\Relatore{Un altro}
\Correlatore{Qualcun altro}
\Annoaccademico{2016-2017}
\end{frontespizio}
\end{filecontents}
\begin{document}
\input{frontespizio-test}
\begin{figure}
\centering
\tikzsetnextfilename{test-figure}
\begin{tikzpicture}
\begin{groupplot}[
group style={
group name=prova,
rows=2,
columns=2
}
]
\nextgroupplot
\addplot coordinates {(0,0) (1,1)}; \label{x}
\nextgroupplot
\addplot coordinates {(0,0) (1,1)};
\nextgroupplot
\addplot coordinates {(0,0) (1,1)};
\nextgroupplot
\addplot coordinates {(0,0) (1,1)};
\addlegendentry{\ref{x}}
\end{groupplot}
\end{tikzpicture}
\end{figure}
\end{document}
Unfortunately I really need to externalize the plots, since I'm working with many "heavy" plots, and I need the \label-\ref mechanism to manually create a legend for the groupplot, as in this answer.
Update: the frontispice appears in every plot that is externalised, also those without inner references, without a groupplot environment, even in a drawing of a molecule created with chemfig.


\input{frontespizio-test}into\tikzifexternalizing{}{\input...}such that it is only done for the main file, not for the externalized images – Christian Feuersänger Jun 24 '17 at 09:14mode=list and makeand compile many times to get the labels right, but at least I have a correct output. – yellon Jun 24 '17 at 12:29