1

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}
  • Do you know that it's possible to adjust the bounding box to its "real" size? tex.stackexchange.com/questions/43621/… – Ignasi Oct 2 '15 at 9:48
  • I think that if you can do it, you won't need to include references inside tikz picture and just use a regular caption inside sidewaysfigure. – Ignasi Oct 2 '15 at 9:49
  • The bounding box has the size of the picture. I think the word bounding box is misleading here, I will correct that. The problem is that the picture is squared and fills the whole page so there is no room for a caption below. That is what using a regular caption gives me. I'd like to place the caption in the white space of the picture. – Ghoraab Oct 2 '15 at 10:03
2

If I am not mistaken, you find an undefined reference in the external file which causes the resulting document to contain the usual '??' instead of the correct section number. This appears to be the only problem, and problems with overlays are just attempts to find a solution, right?

In this case, I have two answers.


The first answer is: wait for the next version of the external lib as it comes with a fully automated bugfix. The external lib is shipped with PGF, so if you are a power-user, you can install an unstable version of PGF. A copy of the external lib is also shipped with pgfplots (if you write \usepackage{pgfplots}\usepgfplotslibrary{external} instead of \usetikzlibrary{external} ), and a release of pgfplots is scheduled for the first week of january 2016.


The second answer is: if you open your .log file, you will find some message of sorts

LaTeX Warning: Reference `sec:bluefig' in external picture `PP-figure0' could not be resolved on input line 12.
This is because the PP.aux file is not accessable in this context, you will need to issue the externalize command
   pdflatex -shell-escape -halt-on-error -interaction=batchmode -jobname "PP-figure0" "\def \tikzexternalrealjob {PP}\input {PP}"
manually.

The "PP" is because my main file is called "PP.tex" (it will probably have a different name on your computer).

Thus, the second answer is: execute the suggested command line as it appears in your .log file. In my case, this is

pdflatex -shell-escape -halt-on-error -interaction=batchmode -jobname "PP-figure0" "\def \tikzexternalrealjob {PP}\input {PP}"

Now, if you recompile the main document (PP.tex in my case), the reference is resolved.

This "second answer" is no longer necessary if you have the correct version of the external lib (which will become part of the stable of pgfplots within days after this post).


There is also a third solution, but only if you are a power user under linux. In this case, you can use \tikzexternalize[mode=list and make] and use make -f PP.makefile to compile the external graphics (PP.tex being the name of the main tex file). This does not suffer the problems here and works directly.

0

If you want to write a caption on a current page fixed point, you can also use TikZ for it and its options remember picture and overlay. External tikz figure is still external, but corresponding caption is writen into the actual document.

The main problem seems to be that being a landscaped page all references are wrong, so after some tests I could do:

\documentclass{article}
\usepackage{tikz}

\usetikzlibrary{positioning}
\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;
\end{tikzpicture}
\tikzexternaldisable
\begin{tikzpicture}[overlay, remember picture]
\node[text width = 5cm, draw, below right=1cm and 4cm of current page.south east] {%
    \caption{An ugly blue figure as described in more detail
           in section \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}

enter image description here

  • Thanks for your suggestion. I hope you can understand that positioning the caption manually is not a solution that I would like to use. I'd rather like LaTeX to take care of this. Furthermore I don't think that sidewaysfigure messed up the references. I think that the externalize interferes with the remember picture and overlay. I'll add my next attempt to the question. – Ghoraab Oct 2 '15 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.