# Cross-referencing within tikzpicture mixes with frontespizio package

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}
\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
\nextgroupplot
\nextgroupplot
\nextgroupplot
\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.

This is how it looks like using the code in egreg's answer:

• A solution could be based on tex.stackexchange.com/questions/39001/… : perhaps you can move \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 9:14
• Thanks, this works! I have to use mode=list and make and compile many times to get the labels right, but at least I have a correct output. – yellowquark Jun 24 '17 at 12:29

I get only one frontispiece, if I use the package according to the documentation.

\documentclass[a4paper]{article}
\usepackage{frontespizio}
\usepackage{pgfplots}

\pgfplotsset{%
compat = 1.14,
}

\usepgfplotslibrary{groupplots}

\usetikzlibrary{external}
\usepgfplotslibrary{external}
\tikzexternalize

\begin{document}

\begin{frontespizio}
\Universita{X}
\Facolta{prova}
\Corso{x}
\Titoletto{tesi}
\Titolo{titolo}
\Candidato{Io}
\Relatore{Un altro}
\Correlatore{Qualcun altro}
\end{frontespizio}

\begin{figure}[htp]
\centering
\tikzsetnextfilename{test-figure}
\begin{tikzpicture}
\begin{groupplot}[
group style={
group name=prova,
rows=2,
columns=2
}
]
\nextgroupplot
\nextgroupplot
\nextgroupplot
\nextgroupplot
\end{groupplot}
\end{tikzpicture}
\end{figure}
\begin{figure}[htp]
\centering
\tikzsetnextfilename{test-figure}
\begin{tikzpicture}
\begin{groupplot}[
group style={
group name=prova,
rows=2,
columns=2
}
]
\nextgroupplot
\nextgroupplot
\nextgroupplot