# How to refer to pgfplot legend labels with hyperref?

I would like to \refer to a pgf legend label in the caption of a figure (to explain symbols). I create my figures with externalization. Without the hyperref package, everything works fine. But as soon as I include hyperref, errors emerge.

First of all, using \caption{\ref{figurelegend}} gives me an error. Paragraph endet before \Hy@setref@link was complete... Apparently, hyperlinking does not work. But I have already expected this behavior since the aux file created by the externalization does not include hyperref information.

As a next approach I tried disabling the hyperref for this link using \caption{\ref*{figurelegend}}. However, this gives me an error. Exta \else ... which I can not understand at all.

So I thought, maybe \protecting the reference would help. Unfortunately, \caption{\protect\ref*{figurelegend}} gives me the same error.

I also tried \pgfplotslegendfromname{figurelegend}. It compiles without errors but I only produces "??". Somehow the aux file from the externalization seems to be invisible for this command.

Again I emphasize that without hyperref, everything works fine. So how can I properly refer to pgf legend labels from externalizeded figures when hyperref is activated?

Here is a minimal working example:

main.tex:

\documentclass{article}

\usepackage{cleveref,graphicx,tikz,pgfplots}
%\usepackage{hyperref} % remove comment to break everything

\makeatletter\write\@auxout{\string\@input{figure.aux}}\makeatother % include the externalized references

\begin{document}
\section{Figures}
\begin{figure}
\centering
\includegraphics[width=3cm]{fig.pdf}
\caption{Here is a red line: \ref{figurelegend}}
\end{figure}
Some text.
\end{document}


figure.tex:

\documentclass{article}
\usepackage{tikz,pgfplots}
\usetikzlibrary{external}
\tikzexternalize[force remake=true,figure list=true,mode=list and make]

\begin{document}
\tikzsetnextfilename{fig}
\begin{tikzpicture}[scale=1]
\begin{axis}[legend pos=outer north east]
\addplot[red,domain=1:2] {x^2}; \addlegendentry{$x^2$}; \label{figurelegend}
\end{axis}
\end{tikzpicture}
\end{document}


To compile, evoke

pdflatex figure
make -f figure.makefile
pdflatex main


Related links which might prove useful for someone else:

• Would you be so kind to add an MWE. – Richard Jul 10 '15 at 18:10
• Its kind of difficult, but I can try. – JanPeterson Jul 10 '15 at 18:15

One solution is to use standalone instead of externalize (which has the advantage of being far more efficient as well).

\documentclass[multi={tikzpicture}]{standalone}
\usepackage{tikz,pgfplots}

\begin{document}
\begin{tikzpicture}[scale=1]
\begin{axis}[legend pos=outer north east]
\addplot[red,domain=1:2] {x^2}; \addlegendentry{$x^2$}; \label{figurelegend}
\end{axis}
\end{tikzpicture}

\ref{figurelegend}
\end{document}


which in this case produces file test.pdf

\documentclass{article}
\usepackage{graphicx}

\begin{document}
\listoffigures

\section{Figures}
\begin{figure}[h]
\centering
\includegraphics[width=3cm,page=1]{test.pdf}
\caption{Here is a red line: \protect\includegraphics[page=2]{test.pdf}}
\end{figure}
Some text.
\end{document}


• This works fine, although, it somehow seems like a cumbersome workaround. – JanPeterson Jul 13 '15 at 8:55