# How does TiKZ calculate positioning of picture on page?

I'm trying to use \beginpgfgraphicnamed to externalise some complex figures in a large document, using the latest released version of TiKZ (2.00). This produces standalone PDF images but for me the graphic is shifted up: the top part of the image is cut off and the bottom part has unnecessary whitespace. The overall PDF that is generated by pdflatex when these externalised images are used has the same visual problem, so I'm having to drop the idea of externalising graphics for now.

As far as I can tell the overall size of the pictures is correct (when comparing to the version without externalisation). I am using a custom thesis style file that changes margins, which is probably breaking several of the l2tabu no-no's.

How does PGF/TiKZ calculate the positioning of pictures on the page, at least as this affects positioning of externalised pictures?

In particular, are there specific things to tweak or to avoid tweaking to ensure this problem does not happen? I'm trying to not embark on a big LaTeX hacking project like redoing the style based on a more modern documentclass than report, so ways to steer clear of this would be especially welcome.

-
Have you ever heard of the package standalone? Have a look at this answer: tex.stackexchange.com/questions/452/… – Vivi Aug 15 '10 at 21:40

You can set the bounding box of a tikzpicture explicitly by using \useasboundingbox or more TikZ'ish:

\path[use as bounding box] (0,0) rectangle (10,10); % adjust to fit


in your tikzpicture.

You will have to provide some more information if the above is not what you want. As stated above, the way to control the size of a tikzpicture is to set the bounding box manually if you are not happy with what TikZ is calculating. To quote the pgfmanual (section Establishing a bounding Box):

pgf is reasonably good at keeping track of the size of your picture and reserving just the right amount of space for it in the main document. However, in some cases you may want to say things like "do not count this for the picture size" or "the picture is actually a little large." For this you can use the option use as bounding box or the command \useasboundingbox, which is just a shorthand for \path[use as bounding box].

You might also try using the external library

\usetikzlibrary{external}
\tikzexternalize


this offers some additional functionality on top of the

\beginpgfgraphicnamed ...\endpgfgraphicnamed.


Alternatively, the preview package offers a way to extract tightly cropped parts of a document

\usepackage[active,pdftex,tightpage]{preview}
\PreviewEnvironment{tikzpicture}

-
If your external figures were created in LaTeX, then I second the recommendation for using the preview package in those figures. Preview will automatically crop the PDF to the exact size of the LaTeX environment you are using to draw the figure, without having to guess at bounding boxes. – ESultanik Aug 17 '10 at 14:00
I spent so long looking for just this thing. Thanks so much! – Richard Jun 14 '12 at 17:19

To position a picture on the page you could use the current page node. Here is an example doing it that way, placing a blue square in the top left corner of the page:

\documentclass[svgnames]{report}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[remember picture,overlay]
\node[yshift=-1cm] at (current page.north west) {%
\begin{tikzpicture}[remember picture, overlay]
\draw[fill=LightSkyBlue] (0,0) rectangle (1cm,1cm);
\end{tikzpicture}
};
\end{tikzpicture}
\end{document}

-