Original Problem
I have some big "banana shaped" pictures in a document. Thus the picture fills the whole page, the "actual picture" i.e. the non-white space, however, does not. I place a caption in the white space of the picture using tikz similar to this solution.
The picture itself is a tikz picture and it is quite big, so I'm using externalization to reduce the compilation time.
Everything worked fine until I placed a reference in the caption. Here is a MWE:
\documentclass{article}
\usepackage{tikz}
\usepackage{rotating}
\usetikzlibrary{external}
\tikzexternalize[up to date check=md5]
\begin{document}
\begin{sidewaysfigure}
\tikzexternalenable
\begin{tikzpicture}
\filldraw [blue] (0,0) -- +(0,15) -- (21,15) -- (21,5) -- (14,5) -- (12,0) -- cycle;
\draw (current bounding box.south east) node[anchor=south east, text width = 5cm]{\caption{An ugly blue figure as described in more detail in section \ref{sec:bluefig}. \label{pic:bluefig}}};
\end{tikzpicture}
\tikzexternaldisable
\end{sidewaysfigure}
\section{Bluefigures} \label{sec:bluefig}
Here is the description of the blue figure. The picture is in figure \ref{pic:bluefig}.
\end{document}
The picture is generated and externalized correctly but obviously it is not possible anymore to put a correct reference into the picture as the caption is part of a pdf file and not of the tex file anymore.
I found some other threads: here and here on Stackexchange but in these cases the caption is under the picture as usual, so they did not help.
I tried to nest tikzpicture environments to have just the inner tikzpicture externalized but besides this being bad practice it did not work and I ran into various "undefined control sequences". Furthermore I tried to include the graphic with
\includegraphics{filename.tikz}
but this does not work within a tikzpicture environment. The compiler complains about an unknown file extension .tikz in this case.
Now I'm out of ideas and I'd be glad if someone could help me. (Should I post MWEs for the not working solutions as well?)
1st try to solve
Based on the proposition of Ignasi I have tried to figure out what messes up the coordinates in the overlay picture. I think that externalize somehow prohibits that tikz remembers the coordinates. The following MWE works flawless without externalize, using overlay and remember picture. As soon as you un-comment the externalize lines things get messed up and I end up with an error "No shape named corner known".
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{rotating}
\usetikzlibrary{external}
%\tikzexternalize[up to date check=md5]
\begin{document}
\begin{sidewaysfigure}
%\tikzexternalenable
\begin{tikzpicture}[remember picture]
\filldraw [blue] (0,0) -- +(0,15) -- (21,15) -- (21,5) -- (14,5) -- (12,0) -- cycle;
\draw [thick,black] (0,0) -- (21,0) -- (21,15);
\node (corner) at (current bounding box.south east){};
\end{tikzpicture}
%\tikzexternaldisable
\begin{tikzpicture}[overlay, remember picture]
\node [draw] at (corner) [anchor=south east,text width = 4cm, align=justify]%
{\caption{The caption with reference to \ref{sec:bluefig} \label{pic:bluefig}}};
\end{tikzpicture}
\end{sidewaysfigure}
\section{Bluefigures} \label{sec:bluefig}
Here is the description of the blue figure. The picture is in
figure \ref{pic:bluefig}.
\end{document}
caption
insidesidewaysfigure
. – Ignasi Oct 2 '15 at 9:49