# \label and \ref do not work correctly using pgfplots, hyperref and tikz externalization

I have the following MWE:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{external}
\tikzset{external/mode=list and make}
\tikzexternalize
\usepackage{blindtext}
\usepackage{hyperref}

\begin{document}

\Blindtext
\begin{tikzpicture}
\begin{axis}
(1, 9) (2, 6) (3, 1) (4, 4)
}; \label{important}
\end{axis}
\end{tikzpicture}
\Blindtext
Here I want to reference the plot \ref{important}.

\end{document}


If I comment out \tikzexternalize to deactivate externalization, the link generated by \ref points to page 2. With \tikzexternalize in place, it incorrectly links to page 1. I use the latex command that calls pdfTeX 3.14159265-2.6-1.40.18 (TeX Live 2017/Debian). I have pgfplots 1.16 installed in ~/texmf. Note that I go through to the make -f test.makefile sequence required by TikZ's list and make externalization mode.

• I think your question is the same as this one. – user121799 Jul 5 at 7:13

there is no destination from the label if you include an externally generated pdf. You could try something like this:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{external}
\tikzset{external/mode=list and make}
\tikzexternalize
\usepackage{blindtext}
\usepackage{hyperref}

\begin{document}

\Blindtext
\hypertarget{importantdest}{}\begin{tikzpicture}
\begin{axis}

• I suppose I can just use \ref*{important} for the reference without a link? (I actually mostly need the plot symbol to create a custom legend in my actual document) – apriori Jul 5 at 3:06
• please note that \ref behaves somewhat differently in pgfplots. It produces a plot symbol, exactly like the legend image. – user121799 Jul 5 at 7:09