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.

    \useasboundingbox (0,0) rectangle (\textwidth,12em);         % <------- !
    \node[anchor=south west,inner sep=0] (image) at (0,0) {%
    \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);

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?

  • 2
    \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

Add the following code to your preamble


and then before the TikZ picture, type


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.

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.