[Tex/LaTex] How does TiKZ calculate positioning of picture on page

positioningtikz-pgf

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.

Best Answer

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}
Related Question