I have a plot consisting of two y-axis. When I'm adding the legendentries in each axis environment, the first legendentry will be plotted "behind" the plot of the second axis. Thus I thought a solution might be the \ref \label to plot both legendentries at the end of the document, that they will appear in the very front of all plots and not hidden. Unfortunately using the tikz externalization gives the error "undefined references ...." and the legendentries are given with "??" only. I checked the manual for using \legend \ref with externalization, but it doesn't help. I tried to compile several times.
\documentclass[fontsize=12pt,openright,oneside,DIV11,a4paper,numbers=noenddot,headsepline,parskip=half]{scrbook}
\usepackage[latin1]{inputenc}
\usepackage[cmex10]{amsmath}
\usepackage{siunitx}
\usepackage{tikz}
\usetikzlibrary[shapes.arrows]
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
% set up externalization
\usetikzlibrary{pgfplots.external}
\tikzset{external/system call={lualatex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\texsource"}}
\tikzexternalize
\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}
\begin{axis}[
legend cell align=left,
bar width=7pt,
xmin=-3, xmax=30,
axis y line*=left,
xlabel=Laser current / \si{\milli\ampere},
ylabel=Tuning range / \si{\nano\metre},
nodes near coords align=left,
nodes near coords,]
\addplot[ybar, ybar legend, draw=gray, fill=gray, color=gray] table[x=current,y=tuning] {Tun_SMSR.txt};
% \addlegendentry{Abstimmbereich};
\label{Abstimmbereich};
\end{axis}
\begin{axis}[
every axis legend/.append style={fill=white, anchor=north west, at={(0.35,0.18)}},
legend cell align=left,
ybar=5pt,
ybar interval=0.6,
bar width=7pt,
xmin=-3, xmax=30,
ymin=30, ymax=60,
axis y line*=right,
axis x line=none,
ylabel=SMSR / \si{\deci\bel},
nodes near coords align=left,
nodes near coords,]
\addplot[ybar, ybar legend, draw=black, color=black] table[x=current,y=SMSR] {Tun_SMSR.txt};
\label{SMSR};
% \addlegendentry{SMSR};
\end{axis}
\node [draw,fill=white] at (rel axis cs: 0.7,0.15) {\shortstack[l]{
\ref{Abstimmbereich} Abstimmbereich\\
\ref{SMSR} SMSR
}};
\end{tikzpicture}
\end{document}
I'm compiling with the command
lualatex -shell-escape "document name".tex
The resulting figure shows the legends but with no symbols and "??" instead.
