# Generated Figure seems cut when using tikzexternalize

When I use the following code, the Figure seems cut, as from a too tight bounding box.

    \documentclass{article}

\usepackage{pgf}
\usepackage{tikz}
\usepackage{pgfplots}

\usetikzlibrary{shapes,arrows}
\usetikzlibrary{calc}
\usetikzlibrary{external}
\tikzexternalize

\usetikzlibrary{positioning}
\tikzstyle{seed}=[-latex,red, thick]
\tikzstyle{ray}=[green!50!black,dotted,thick,-latex]

\begin{document}
\newcommand{\door}[1] {
\begin{scope}[shift={#1}]
\draw[red] (-0.5,0) arc (180:270:1cm);
\draw[black] (0.5,-1) -- (0.5,-0) node[right,  near start]{};
\draw[black,dotted] (-0.5,0) -- (0.5,-0)node(doorstepcenter)[pos=0.5]{} ;
\end{scope}
}

\newcommand{\room}{
\coordinate (a1) at  (4.5,0.5);
\coordinate (a2) at  (4.5,4);
\coordinate (a3) at  (0,4);
\coordinate (a4) at  (1,3);
\coordinate (a5) at  (1.4,3)  ;
\coordinate (a6) at  (1.4,2.6)  ;
\coordinate (a7) at  (1,2.6)  ;
\coordinate (a8) at  (1,3);
\coordinate (a9) at  (0,4);
\coordinate (a10) at  (0,0.5) ;
\coordinate (a11) at  (4.5,0.5);
}

\begin{tikzpicture}%[node distance =4.5cm,thick]
\room; %just get the door
\door{ ($(a10)!0.5! (a1) -(0,0.1)$) };

\coordinate (r1) at (a6);
\coordinate (r2) at (a7);
\coordinate (ray) at  ($(r1)!.3!(r2)$);
\draw[ray] (doorstepcenter.center) -- (ray)  node [below=10pt, midway]{ray};
\path (a1)--(a2) node[pos=0.5,left,text width = 3cm, text centered]{Polygon cannot be closed };
\draw[seed] (r1) -- (r2);
\draw[thick] (a4) -- (a5);
\draw[thick] (a5) -- (a6);
\draw[seed] (a7) -- (a8);
\draw[seed] (a8) -- (a9);
\draw[seed] (a9) -- (a10);
\draw[seed] (a10) -- (a11);
\draw[seed] (a11) -- (a2);
\draw[seed] (a2) -- (a3);
\end{tikzpicture}
\end{document}


The arrows left and right seem cut. Where does this come from and how can it be avoided ?

-
I can't reproduce the problem. Are you sure it's not just a viewer problem? –  Gonzalo Medina May 6 '12 at 0:49
Good question. Tested it with an internal eclipse viewer and okular. When I placed some phantom lines (paths) around the figure the arrows were neatly drawn ... –  Martin May 6 '12 at 1:01
And without those paths? I tested your code as it is and opened it with Okular, Adobe Reader and the TeXworks viewer, and the problem mentioned wasn't there... –  Gonzalo Medina May 6 '12 at 1:04
Strange. So all arrows were fully drawn on your machine ? The image I pasted was what I saw in my viewers without the paths (hence the code i posted). What system are you running ? I am on ubuntu lucid. Perhaps I should just give it a try on windows acrobat and see what comes out. –  Martin May 6 '12 at 1:08
Yes, all arrows fully drawn. I use TeX Live2011 last updated yesterday, on Ubuntu 12.04 (Oneiric). –  Gonzalo Medina May 6 '12 at 1:11

This happens because you are using the external library (a fact you should have made more clear, btw.) which produces a PDF image of the picture and includes this in order to reduce compile time for further compiler runs (the picture is only processed once). The arrow heads are not taken as part of the official path and so PGF/TikZ does not extend the official bounding box to hold them. Instead it limits the size to the lines. Maybe this is fixed in future versions of PGF/TikZ or maybe there is a reason why it is not done.

You forget the \tikzexternalize macro in your preamble of your example code. Without this no externalization is done and therefore your picture isn't cut. This is the reason why other users could recreate you issue at first. While the bounding box is still the same in this case the arrow heads simply look out from the box but are still displayed.

One way to fix this is to extend the bounding box a little bit. I couldn't find a margin or border option for external which does this. You can do this yourself by adding a dummy path at the end of the picture which uses the special current bounding box node. There might be also a different, better way for this.

\documentclass{article}

\usepackage{pgf}
\usepackage{tikz}
\usepackage{pgfplots}

\usetikzlibrary{shapes,arrows}
\usetikzlibrary{calc}
\usetikzlibrary{external}
\usetikzlibrary{positioning}
\tikzstyle{seed}=[-latex,red, thick]
\tikzstyle{ray}=[green!50!black,dotted,thick,-latex]
\tikzexternalize

\begin{document}
\newcommand{\door}[1] {
\begin{scope}[shift={#1}]
\draw[red] (-0.5,0) arc (180:270:1cm);
\draw[black] (0.5,-1) -- (0.5,-0) node[right,  near start]{};
\draw[black,dotted] (-0.5,0) -- (0.5,-0)node(doorstepcenter)[pos=0.5]{} ;
\end{scope}
}

\newcommand{\room}{
\coordinate (a1) at  (4.5,0.5);
\coordinate (a2) at  (4.5,4);
\coordinate (a3) at  (0,4);
\coordinate (a4) at  (1,3);
\coordinate (a5) at  (1.4,3)  ;
\coordinate (a6) at  (1.4,2.6)  ;
\coordinate (a7) at  (1,2.6)  ;
\coordinate (a8) at  (1,3);
\coordinate (a9) at  (0,4);
\coordinate (a10) at  (0,0.5) ;
\coordinate (a11) at  (4.5,0.5);
}

\begin{tikzpicture}%[node distance =4.5cm,thick]
\room; %just get the door
\door{ ($(a10)!0.5! (a1) -(0,0.1)$) };

\coordinate (r1) at (a6);
\coordinate (r2) at (a7);
\coordinate (ray) at  ($(r1)!.3!(r2)$);
\draw[ray] (doorstepcenter.center) -- (ray)  node [below=10pt, midway]{ray};
\path (a1)--(a2) node[pos=0.5,left,text width = 3cm, text centered]{Polygon cannot be closed };
\draw[seed] (r1) -- (r2);
\draw[thick] (a4) -- (a5);
\draw[thick] (a5) -- (a6);
\draw[seed] (a7) -- (a8);
\draw[seed] (a8) -- (a9);
\draw[seed] (a9) -- (a10);
\draw[seed] (a10) -- (a11);
\draw[seed] (a11) -- (a2);
\draw[seed] (a2) -- (a3);
\path
([shift={(-5\pgflinewidth,-5\pgflinewidth)}]current bounding box.south west)
([shift={( 5\pgflinewidth, 5\pgflinewidth)}]current bounding box.north east);
\end{tikzpicture}
\end{document}


Note that an alternative to external is the standalone class and package. The standalone class provides a border option which adds some margin for you. This would look like follows and also requires the -shell-escape aka --enabled-write18 compiler option.

% Main document
\documentclass{article}

\usepackage[subpreambles,mode=buildnew]{standalone}
\begin{document}
\includestandalone{mytikzpic} % or \input, but here you can use [<options>]
\end{document}

\documentclass[border=1pt]{standalone}

\usepackage{pgf}
\usepackage{tikz}
\usepackage{pgfplots}

\usetikzlibrary{shapes,arrows}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\tikzstyle{seed}=[-latex,red, thick]
\tikzstyle{ray}=[green!50!black,dotted,thick,-latex]

\begin{document}
\newcommand{\door}[1] {
\begin{scope}[shift={#1}]
\draw[red] (-0.5,0) arc (180:270:1cm);
\draw[black] (0.5,-1) -- (0.5,-0) node[right,  near start]{};
\draw[black,dotted] (-0.5,0) -- (0.5,-0)node(doorstepcenter)[pos=0.5]{} ;
\end{scope}
}

\newcommand{\room}{
\coordinate (a1) at  (4.5,0.5);
\coordinate (a2) at  (4.5,4);
\coordinate (a3) at  (0,4);
\coordinate (a4) at  (1,3);
\coordinate (a5) at  (1.4,3)  ;
\coordinate (a6) at  (1.4,2.6)  ;
\coordinate (a7) at  (1,2.6)  ;
\coordinate (a8) at  (1,3);
\coordinate (a9) at  (0,4);
\coordinate (a10) at  (0,0.5) ;
\coordinate (a11) at  (4.5,0.5);
}

\begin{tikzpicture}%[node distance =4.5cm,thick]
\room; %just get the door
\door{ ($(a10)!0.5! (a1) -(0,0.1)$) };

\coordinate (r1) at (a6);
\coordinate (r2) at (a7);
\coordinate (ray) at  ($(r1)!.3!(r2)$);
\draw[ray] (doorstepcenter.center) -- (ray)  node [below=10pt, midway]{ray};
\path (a1)--(a2) node[pos=0.5,left,text width = 3cm, text centered]{Polygon cannot be closed };
\draw[seed] (r1) -- (r2);
\draw[thick] (a4) -- (a5);
\draw[thick] (a5) -- (a6);
\draw[seed] (a7) -- (a8);
\draw[seed] (a8) -- (a9);
\draw[seed] (a9) -- (a10);
\draw[seed] (a10) -- (a11);
\draw[seed] (a11) -- (a2);
\draw[seed] (a2) -- (a3);
\path
([shift={(-5\pgflinewidth,-5\pgflinewidth)}]current bounding box.south west)
([shift={( 5\pgflinewidth, 5\pgflinewidth)}]current bounding box.north east);
\end{tikzpicture}
\end{document}

-