1

What concise recipee is there to overlay objects on top of images, using relative coordinates (such as fraction of image width/height)?

For example trying to draw a red rectangle to highlight a specific region of this image:

https://i.imgur.com/a4P5Y5j.png

\documentclass[a4paper]{beamer}

\usepackage{tikz}

\begin{document}

\begin{frame}{}

\begin{tikzpicture}
\node[inner sep=0, anchor=south west] (img) at (0,0) {
    \includegraphics[keepaspectratio,height=.9\textheight,width=\linewidth]{refgrid_crop}%
};
    \begin{scope}[x={(img.south west)},y={(img.south west)},local bounding box=img]
    \draw[thick, rounded corners, color=red!80!black, anchor=south west] (0.5, 0.3)
        rectangle (0.75, 0.5);
    \end{scope}
\end{tikzpicture}

\end{frame}
\end{document}

The above code using scope does not display at all a rectangle at the wanted fractions 0.5, 0.3. Also, I don't understand why, but a rectangle only appears in the lower right region when I set the scope parameters x and y to {(img.south east)}, while expected this parameter to indicate the origin of the coordinate system...

How to switch the units to image fraction and origin to south-west ?

8
  • 1
    x=1mm, y=1in is a way to rescale the x-axis and y-axis, not a way to shift things. Shifting is done by shift={(x, y)}.
    – Symbol 1
    Jul 28 at 16:12
  • 1
    A very interesting thread about this, in which you can leran that a specific package, called callouts may help you out (but other solutions presented are very nice too).
    – SebGlav
    Jul 28 at 17:06
  • 1
    As you can see from the question linked above, you need \begin{scope}[x={(img.south east)},y={(img.north west)}], not what you currently have. Jul 28 at 20:53
  • 2
    As mentioned above, the x and y parameters rescale the axes, specifically they define the unit vectors. You've placed the image with the bottom left corner at (0,0), so if you set x=(img.south east), the x unit vector points from bottom left to the bottom right corner of the image. Similarly, with y=(img.north west), the y unit vector goes from the bottom left to the top left of the image. Jul 28 at 21:26
  • 1
    It's really a duplicate of the previously mentioned question, I think .. Jul 29 at 9:51
1

Easier to show than explain. The calc tikzlibrary can locate fractional distance between two points. The following locates (0.5,0.3) relative to the (img) node.

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
\node[inner sep=0] (img) {\includegraphics{example-image}};
\coordinate (A) at ($(img.west)!0.5!(img.east)$);% x location
\coordinate (B) at ($(img.south)!0.3!(img.north)$);% y location
\node[red,draw] at (A|-B) {here};
    
\end{tikzpicture}
\end{document}

demo

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.