# TikZ: using a bitmaps's dimensions as global bounding box

I'm using a tikzpicture environment to draw on a bitmap image in order to add annotations. I'm setting the width of my bitmap to \textwidth. I want to add annotations in the left or right margins of the page. TikZ usually first completes the tikzpicture, then aligns it with the left margin. So what's required here is a manual bounding box to make sure it's the bitmap rather than the whole drawing that's left-aligned.

\documentclass{scrartcl}
\usepackage{tikz,lipsum}
\begin{document}
\raggedright
\begin{tikzpicture}
\useasboundingbox (0,0) rectangle (\textwidth,12em);         % <------- !
\node[anchor=south west,inner sep=0] (image) at (0,0) {%
\rule{\textwidth}{12em}};
\begin{scope}[x={(image.south east)},y={(image.north west)}]
\draw[red,-latex] (-0.05,0.7) node[anchor=east] {abc} -- (0.5,0.9);
\end{scope}
\end{tikzpicture}\par
\lipsum
\end{document}


This is all fine as long as we know the bitmap's dimensions, particulary its height. But -- is there a way to make tikz use the bitmap's dimensions (after my scaling it to width=\textwidth) as a bounding box for the entire drawing without us knowing them in advance?

• \newlength{\bitmapwd}\settowidth{\bitmapwd}{\includegraphics{...}} and then use \bitmapwd – egreg Mar 29 '13 at 16:32
• obviously... how embarassing :/ – Nils L Mar 29 '13 at 16:35

\newlength{\bitmapwd}

\settowidth{\bitmapwd}{\includegraphics[...]{...}}

where the \includegraphics command should be the same you use for printing the image. In the TikZ picture you can use \bitmapwd for a length equal to what you need.