# TikZ: Alternative for use as bounding box? Or a another way to handle it in external pictures?

I've been using TikZ to draw graphs (pgfplots) and pictures for my physics courses etc.

Because I like it when (for example) the box of graph is centered on the page without looking at the stuff that comes with it (ylabel, yticks, etc.), I draw a rectangle on top of like this:

\draw[draw=none, use as bounding box](0,0) rectangle (\figurewidth,\figureheight);


This way only the the box of the graph is considered when the figure is to be placed on the page.

This approach works for me ad I've applied it many times. However, I seem to get in to trouble when using the externalize library. Because the created external figures are cut off at the edges of the bounding box, I lose all that's outside (without externalize this wasn't happening, everything was drawn).

I can see two possible solutions to my problem:

1. Use another command that makes the centering correct, e.g.:

\draw[center on this, draw=none](0,0)rectangle(\figurewidth, \figureheight);

2. Tell the external package not to clip at bounding box

Any ideas?

-

You cannot use "real" bounding boxes with the external library, because of the problems you described. What you have to do instead is use the trim left, trim right and baseline commands to shift the picture according to your requirements.

From the pgfmanual, p. 168:

Note that baseline, trim left and trim right are currently the only supported way of truncated bounding boxes which are compatible with image externalization (see the external library for details).

With pgfplots, you can very comfortably just use [trim axis left,trim axis right] as options to the tikzpicture environment.

Other pictures can be centered by using [trim left,trim right=0pt], for example, to essentially trim the picture down to a vertical line going through the origin.

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\usetikzlibrary{external}
\tikzexternalize
\tikzset{external/force remake}

\begin{document}
\centering
The following 'x' marks the center of the page:

x

\begin{tikzpicture}[trim left,trim right=0pt]
\node (root) {root} child {node {left}} child {node {right}
child {node {child}}
child {node {child}} };

\end{tikzpicture}

% With trim axis
\begin{tikzpicture} [trim axis left,trim axis right]
\begin{axis}[width=5cm,ylabel=looong label,ylabel style={rotate=-90}]
\end{axis}
\end{tikzpicture}

\end{document}


-
Your example works perfect for pgfplots! – romeovs Apr 10 '11 at 7:36

As a generalized version of your approach 1, you could add

\path ($(current bounding box.east)!2!(center |- current bounding box.east)$);
\path ($(current bounding box.west)!2!(center |- current bounding box.west)$);


at the end of your image, provided you specify a point (center) before.

This solution needs \usetikzlibrary{calc}. With this approach, an invisible line is drawn from the current east part to the center, but twice as long, thus generating a point on the left side. Another invisible line is drawn from the west side. Finally, the bounding box is twice as wide as the point that is the furthest from the center, thus creating symmetry.

-