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.stack.imgur.com/DdGhE.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 ?

Simon Dispa
  • 39,141
  • 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 '21 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 '21 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. – Torbjørn T. Jul 28 '21 at 20:53
  • @TorbjørnT. thanks, indeed your x,y values work! I probably assumed an incorrect meaning for those x,y parameters, will need to find it in the doc. – PlasmaBinturong Jul 28 '21 at 21:14
  • 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. – Torbjørn T. Jul 28 '21 at 21:26
  • @TorbjørnT. this is definitely the simplest way (no extra package/library, simple syntax), if you put these comments into an answer I will accept it. – PlasmaBinturong Jul 29 '21 at 09:50
  • 1
    It's really a duplicate of the previously mentioned question, I think .. – Torbjørn T. Jul 29 '21 at 09:51
  • This mentioned question does not ask specifically for relative coordinates, but it might be implied anyway. So I agree, since the accepted answer is what I am looking for. – PlasmaBinturong Jul 29 '21 at 10:27

1 Answers1

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

John Kormylo
  • 79,712
  • 3
  • 50
  • 120