2

I need to draw two rectangles (or this could apply to any shape in general) of different colors on top of a diagram. The MWE that I have this is. The code works, but I am unable to change the coordinates of the squares to accurately achieve my desired result. I have been drawing using the tikzpicture.

My MWE is the following:

\documentclass[10pt, compress]{beamer}
\usepackage{tikz}

     \begin{frame}

    \begin{tikzpicture}
        \node[anchor=south west,inner sep=0] at (0,0) {\includegraphics[width=\textwidth]{images/sample.png}};
        \draw[red,ultra thick,rounded corners] (5.3,5.3) rectangle (8.4,8.4);
        \draw[green,ultra thick,rounded corners] (3.4,3.4) rectangle (8.3,8.3);
    \end{tikzpicture}

    \end{frame}
\end{document}

This MWE correctly draws the rectangles on top of the image, but they are incorrectly placed over it.

The desired result is along this line:

desired result However, at the moment, I am achieving the following result:

Actual Result

Can someone provide insight on how to best estimate the coordinates that will help me get to my desired output? This problem has been recurrent for me for several tasks - so I would like to understand how to calculate them. Thank you!

  • 1
    possible duplicate of Drawing on an image with TikZ – Tobi Mar 3 '15 at 17:34
  • @Tobi I don't think it is a duplicate, as my question is not in regards to drawing. I already have the code for that. My question is regarding to how to accurately calculate the coordinates. Similar, but they are, in fact, different questions. – owwoow14 Mar 3 '15 at 17:36
  • 1
    @owwoow14 You can use the second part of the accepted answer and the answer of Jake on that same question to normalize the coordinates of your image, such that the bottom left is (0,0) and the upper right is (1,1). Then you can draw a grid on top of your image and easily align the proper coordinates – darthbith Mar 3 '15 at 17:58
  • 2
    @owwoow14 Alternatively, you can draw your graph with pgfplots and use the axis cs coordinate system to accurately draw on your graph in the "natural" coordinates of the graph, i.e. whatever your axes are labeled with. As such, I think it is a duplicate, unless you can be more specific about what your problem is :-) – darthbith Mar 3 '15 at 17:59
6

when I want to draw over an image, I first draw a grid with a proportional scale the image with foreach and the calc library.

Then I use these coordinates to draw

    \begin{tikzpicture}
        \node[anchor=south west,inner sep=0] (img)at (0,0) {\includegraphics[width=\textwidth]{sample.png}};
        \foreach \xx in {0,1,2,...,9}{% 
        \draw[dashed] ($(img.north)!0.\xx!(img.north east)$)node[above]{0.\xx} --  ($(img.south)!0.\xx!(img.south east)$);
                \draw[dashed] ($(img.east)!0.\xx!(img.north east)$)node[right]{0.\xx} --  ($(img.west)!0.\xx!(img.north west)$);
        }
        \path ($(img.north)!0.1!(img.north east)$)|-coordinate(c1)($(img.east)!0.3!(img.north east)$);% c1: first corner
         \path ($(img.north)!0.82!(img.north east)$)|-coordinate(c2)($(img.east)!0.77!(img.north east)$);   %c2 : second corner
        \draw[red,ultra thick,rounded corners] (c1)         rectangle (c2);
%        \draw[green,ultra thick,rounded corners] (3.4,3.4) rectangle (8.3,8.3);
    \end{tikzpicture}

use proportional coordinates used to change the scale of the image and maintain proper drawing

  \begin{tikzpicture}[scale=0.5,transform shape]
    \node[anchor=south west,inner sep=0] (img)at (0,0) {\includegraphics[width=\textwidth]{sample.png}};
    \foreach \xx in {0,1,2,...,9}{
    \draw[dashed] ($(img.north)!0.\xx!(img.north east)$)node[above]{0.\xx} --  ($(img.south)!0.\xx!(img.south east)$);
            \draw[dashed] ($(img.east)!0.\xx!(img.north east)$)node[right]{0.\xx} --  ($(img.west)!0.\xx!(img.north west)$);
    }
    \path ($(img.north)!0.1!(img.north east)$)|-coordinate(c1)($(img.east)!0.3!(img.north east)$);
     \path ($(img.north)!0.82!(img.north east)$)|-coordinate(c2)($(img.east)!0.77!(img.north east)$);
    \draw[red,ultra thick,rounded corners] (c1)         rectangle (c2);
\end{tikzpicture}  

enter image description here

  • Adding the grid really helps to visualize how the drawn figures move around the image. – owwoow14 Mar 4 '15 at 10:17

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.