Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 10 down vote accepted

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}]
\addplot {x^2};
\end{axis}
\end{tikzpicture}

\end{document}

tree and plot, centered around useful points

share|improve this answer
    
Your example works perfect for pgfplots! –  romeovs Apr 10 '11 at 7:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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