I have a pdf graphic that needs to be inserted into a TikZ environment (where I can annotate it). However, the bounding box that is placed around the image includes unnecessary whitespace. The command is simply

  \node at (0,0) {\includegraphics[width=1.0\linewidth]{figure.pdf}};
  \draw (current bounding box.south west) rectangle (current bounding box.north east);

The bounding box for the actual pdf is tight. However, the result, outputted by tikz is not. There is significant whitespace on all sides.

How do you tighten the bounding box?

  • The overpic package seems to be the better alternative here. – Thorsten Donig Jul 13 '11 at 21:25
  • @T.D: overpic uses the outdated picture environment. See the link in my answer for a TikZ based replacement. We are planning to publish that as package. – Martin Scharrer Jul 13 '11 at 22:26
  • @Martin: For which reasons should this environment be outdated? – Thorsten Donig Jul 17 '11 at 13:16
  • @Thorsten: Because there are now much better alternatives available which do not suffer the limitations and provide much more functionality (nodes, shadings, fadings, patterns, etc.). I myself wouldn't use picture for new code. – Martin Scharrer Jul 17 '11 at 13:19
  • @Martin: That's true. But from this point of view you should consider the whole LaTeX kernel as outdated since it is enhanced by many, many packages which provide better alternatives for a lot of predefined stuff. – Thorsten Donig Jul 18 '11 at 17:29

You need to set the inner sep to zero. It's the separation between the inner content and the outer frame (which is only visible if you use draw). There is also outer sep which is by default half of the drawing line width (.5\pgflinewidth) and places the anchors at the outer side of the frame lines, not it the middle. You might want to set it also to zero to get exactly the border of the image.

\node [inner sep=0pt,outer sep=0pt] at (0,0) {\includegraphics[width=1.0\linewidth]{figure.pdf}};
\draw (current bounding box.south west) rectangle (current bounding box.north east);

See also the related question Drawing on an image with TikZ.

  • This is strange. With the sep commands (both of them) the picture is now flush against the bounding box. However, when I output the picture outside of the document using \beginpgfgraphicnamed{jobtitle} ... \end... and then compile, I get a pdf graphic, jobtitle.pdf, which has space before the left of the `bounding box'. Any idea what's going on? – TSGM Jul 13 '11 at 22:40
  • @TSGM: I don't have not much ideas. Maybe you have some spurious spaces somewhere. Try to place a % direct after { and } which are located at the end of lines. – Martin Scharrer Jul 14 '11 at 7:02
  • same experience as @TSGM, after adding % everywhere: are there other sep options out there to be set to 0pt? – PatrickT Jan 24 '14 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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