[Tex/LaTex] TikZ/PGF bounding box too large

bounding boxtikz-pgf

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

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

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?

Best Answer

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.

\begin{tikzpicture} 
\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);
\end{tikzpicture}

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