1

I am trying to include a background image in a tikz/pgf drawing. I need to be able to draw nodes directly on certain features of the image, it thus needs to be precisely adjusted.

This would be easiest if I could somehow fix it to two nodes defining the corners of the image, like so:

\documentclass{article}
\usepackage{pgf,tikz}
\begin{document}
\begin{tikzpicture}
\node at (0,0) {a};
\includegraphics{something.png}
\node at (2,2) {b};
\end{tikzpicture}
\end{document}

Of course this does not do what I want, i.e. scaling the image so that it its top-right corner is at (2,2) and the bottom-left corner at (0,0):

enter image description here

sheß
  • 3,622
  • please have a look at this canonical question https://tex.stackexchange.com/questions/9559/drawing-on-an-image-with-tikz – percusse Feb 20 '18 at 17:03

2 Answers2

2

The comment by @percusse led me to the right answer. It involves using the scope environment.

The following code is taken from @Caramdir's answer in Drawing on an image with TikZ:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=0.9\textwidth]{some_image.jpg}};
    \begin{scope}[x={(image.south east)},y={(image.north west)}]
        \draw[red,ultra thick,rounded corners] (0.62,0.65) rectangle (0.78,0.75);
    \end{scope}
\end{tikzpicture}
\end{document}
sheß
  • 3,622
1

How about this?

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\node[inner sep=0pt] (img) at (0,0) {\includegraphics{example-image-a}};
\coordinate (a) at (img.south west);
\coordinate (b) at (img.north east);
\draw[blue,thick] (a)--(b);
\end{tikzpicture}
\end{document}

enter image description here

  • That's a good start, but doesn't allow me "to draw nodes directly on certain features of the image". I want to be able to draw a node at (1.5,1) and be certain it appears in the image at that coordinate – sheß Feb 20 '18 at 17:10
  • If you tell me, what (1.5,1) refers to, I'll be happy to adjust the code. Does that mean at (1.5 x width of the image, 1 x height of the image)? See also here. –  Feb 20 '18 at 17:16
  • My background image is a satellite image of which I know the corners' longitudes and latitude. I want to draw nodes on certain POIs based on their long/lat. – sheß Feb 20 '18 at 17:23
  • But since you posted already an answer yourself, I guess my answer is obsolete, right? –  Feb 20 '18 at 17:25
  • The one I posted is a working work-around, but not very elegant and quite hacky in my final implementation. If you have a solution that works better I'd be very grateful – sheß Feb 20 '18 at 17:28
  • 1
    @sheß Well, your question is already marked a duplicate :-( What you might want to add is a transformation that allows you to use the longitude and latitude coordinates directly. This can be done by modifying this (or also that) answer. –  Feb 20 '18 at 17:36