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 create plots with pgf/TikZ, externalize them and then they will be included into another predefined LaTeX-document (which someone else will do, so I can't change anything there). That final document has a two-column layout and all images will be scaled to a width of 80mm (= 1 column width).

When I create a TikZ-picture and externalize it, the resulting pdf is nicely cropped. However, in my case I need all my pictures to have a width of exactly 80mm. I do not want to scale the pictures, but center each of them on a 80mm-wide canvas.

I came up with a solution using the bounding box of the current picture and enlarging it to my needs. Here's an example:

\documentclass{article}
\usepackage{color,pgf,tikz}
\usetikzlibrary{calc,external}
\tikzexternalize

% define column size (= image width)
\newlength{\singlecol}
\setlength{\singlecol}{80mm}

% set bounding box for each picture, i.e. expand to image width/column size
\tikzset{singpic/.append style={
   execute at end picture={
    \coordinate (BBCorner1) at ($ (current bounding box.south) + (-0.5*\singlecol,0) $);
    \coordinate (BBCorner4) at ($ (current bounding box.north) + (0.5*\singlecol,0) $);
    \path[use as bounding box, draw, blue] (BBCorner1) rectangle (BBCorner4);
    }
   }
}

\begin{document}
   \begin{tikzpicture}[singpic]
      \filldraw[fill=yellow!20] (0,0) rectangle (5,5) node[midway] {The Picture};
      \draw[red, thick] (current bounding box.south west) rectangle (current bounding box.north east);
   \end{tikzpicture}
\end{document}

enter image description here

So this works as it should, but is there a more elegant way to do this?

share|improve this question
2  
Welcome to TeX.SE. Thanks so much for posting a compilable example. –  Peter Grill Nov 30 '11 at 23:16
    
Have you considered leaving the images as small as they are and just center them within the column by using \centering or a center environment? –  Daniel Nov 30 '11 at 23:18
    
Well, I would love to do just that, but that means I would need to change the include-picture-environment in the final two-column LaTeX-document, right? Unfortunately I don't have any influence on that part, so I need a solution for my pictures to have the correct size. –  Kristin Nov 30 '11 at 23:29
add comment

1 Answer

up vote 7 down vote accepted

I think that's quite a good approach. Minor suggestions for simplifying the code:

The effect of [use as bounding box] is to fix the bounding box at the size it has after the path you supply the option to, so any paths specified later on will not increase the bounding box further. Since you specify the path at the very end of the picture, the option has no effect in this case. Note that [use as bounding box] does not decrease the bounding box size. In case you want the bounding box of the picture to match the bounding box of the path in all cases, you have to issue \pgfresetboundingbox first.

Also, you don't have to define the coordinates first and then define a rectangle between them to get the correct bounding box. Instead, you can just specify a path between the two points directly:

\documentclass{article}
\usepackage{tikz} % TikZ loads PGF and color automatically
\usetikzlibrary{calc,external}

% define column size (= image width)
\newlength{\singlecol}
\setlength{\singlecol}{80mm}

% set bounding box for each picture, i.e. expand to image width/column size
\tikzset{
    singpic/.append style={
        execute at end picture={
            \path [draw] ($ (current bounding box.south) + (-0.5*\singlecol,0) $) -- +(\singlecol,0);
        }
    }
}

\begin{document}
   \begin{tikzpicture}[singpic]
      \filldraw[fill=yellow!20] (0,0) rectangle (5,5) node[midway] {The Picture};
      \draw[red, thick] (current bounding box.south west) rectangle (current bounding box.north east);
   \end{tikzpicture}
\end{document}
share|improve this answer
    
Thanks a lot for your explanations! The height of each picture shall not be fixed, but adjusted to its contents. Therefore I've put the [use as bounding box] path at the very end, so I can use and keep the current height of each individual picture. True, I can't decrease the bounding box (which would mean to crop away some of the picture's contents), so I probably should think about some way to ensure that my pictures are never to big (or trigger a warning in such a case), but I guess that's a different question ... –  Kristin Dec 1 '11 at 19:50
    
Ah, just understood now - since I draw the bounding box path at the end, I can omit the option [use as bounding box]. The bounding box will be adjusted automatically to that path anyway (if the path is the largest object). –  Kristin Dec 2 '11 at 9:34
    
Yup, that's what I meant. Sorry for not being clearer. You might want to edit your question (or ask a new one) about generating a warning if your picture is too big, that's definitely an interesting question. –  Jake Dec 2 '11 at 12:32
    
No problem, I'm still quite new to TikZ/pgf, so I need some time to understand. I'll do some research on the picture-too-big-problem before posting a new question. –  Kristin Dec 2 '11 at 21:47
1  
Just realized that life is much simpler: using the desired width and relative coordinates for the 2. point does the trick already: \path[draw,blue] ($ (current bounding box.center) + (-0.5*\singlecol,0) $) -- +(\singlecol,0); –  Kristin Dec 2 '11 at 22:45
show 2 more comments

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.