TikZ PGF Hyperref Cross-Referencing – Using \ref in tikzpicture Node While Using Externalize

cross-referencinghyperreftikz-pgf

Here's the MWE:

\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{external}
\tikzexternalize[prefix=extfig/]

\begin{document}
\begin{equation}
    a = b
    \label{equation}
\end{equation}

\begin{figure}[h]
    \begin{tikzpicture}
    \node at (0,0) (input) {see equation \ref{equation}};
    \end{tikzpicture}
\end{figure}

\end{document}

The node inside tikzpicture shows see equation ??. How to make the reference work? (BTW: it works well without using the externalize lib.)

Best Answer

The externalization process puts the pictures in a separate PDF file, which is then included as image. However annotations are usually lost, when a PDF page is included.

There are only quite complicated workarounds, e.g., project pax, which uses a PDF library to extract the annotations from the PDF file to reinsert them later at TeX level.

Or reference can be put in a node and its coordinates relative to the bounding box remembered and then the link can be added later on top of the included image (via package overpic or again via tikz).

But I think, it might be much easier just to exclude the pictures with the references from the externalization. Section "50.4.3 Remaking Figures or Skipping Figures" from the PGF manual shows some options to disable the export of a figure, e.g.:

\tikzset{external/export next=false}
\begin{tikzpicture}...\end{tikzpicture}

or

\tikzexternalexportnextfalse
\begin{tikzpicture}...\end{tikzpicture}

or

\tikzexternaldisable
\begin{tikzpicture}...\end{tikzpicture}
\tikzexternalenable