2

I would like to draw a frame like the following around a picture without changing the ratio of the picture.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{figure}
\centering
   \begin{tikzpicture}[gray,thin]
        \draw (-6,0) rectangle (6,7);
        %bottom marks
        \foreach \x in {-6,-5.6,..., 6} 
        \draw (\x,0) -- (\x, .1) ;            
        \foreach \x/\num in {-4/-1.0,-2/-0.5,0/0.0,2/0.5,4/1.0}
        \draw (\x,0) node[below]{$\num$}-- (\x,.2);

        %top marks
        \foreach \x in {-6,-5.6,..., 6} 
        \draw (\x,7) -- (\x, 6.9) ;            
        \foreach \x in {-4,-2,...,4}
        \draw (\x,7)-- (\x,6.8);

        %left marks
        \foreach \y in {0,0.35,...,7}
        \draw (-6,\y) -- (-5.9,\y);
        \foreach \y/\num in {1.4/-0.5,2.8/0.0,4.2/0.5,5.6/1.0} 
        {
        \draw (-6,\y) node[left]{$\num$} -- (-5.8,\y);
        }

        %right marks
        \foreach \y in {0,0.35,...,7}
        \draw (6,\y) -- (5.9,\y);
        \foreach \y in {1.4,2.8,...,5.7} 
         {
         \draw (6,\y) -- (5.8,\y);
         }
    \end{tikzpicture}
    \end{figure}
\end{document}

My main problem is that I don't know how to make the frame match my picture without deforming the latter. I started by putting a rectangle around the picture but I couldn't figure out how to put the tick-marks in a convenient way. I guess, I would need the coordinates of the south-west and north-east corner for that. Is there a way to get them?

  • (current bounding box.north east) and (current bounding box.south west)? – cfr May 27 '17 at 12:15
  • Are you aware that there are standard ways to draw axes around plots using either TikZ or pgfplots? – cfr May 27 '17 at 12:19
  • I am not sure how you want the tick marks, but if you import the image in a node, like \node[inner sep=0pt](Img){\includegraphics{example-image}};, you can get the coordinate of the lower left corner with (Img.south west) and the upper right with (Img.north east). – StefanH May 27 '17 at 12:25
  • Should the tick marks be realtive the paper (in real lengths like cm or in) or relative the image (like (-1,-1) in the lower left and (1,1) in the upper right)? – StefanH May 27 '17 at 12:31
  • how can you get only the x (or y) value of let's say south? I thought about using these to calculate the spacing between the ticks.@StefanH The ticks should be relative to the image, if that's possible. – freddy90 May 27 '17 at 17:35
3

Something like this?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{graphicx}
\begin{document}
\begin{figure}
  \centering
  \begin{tikzpicture}[gray,thin,
    Frame/.style={gray},
    Tick/.style={gray},
    SubTick/.style={gray},
    ]
    \node[inner sep=0pt](Img){\includegraphics[scale=0.7]{example-image}};
    \coordinate(LL) at (Img.south west);%% Lower left corner
    \coordinate(LR) at (Img.south east);%% Lower right corner
    \coordinate(UL) at (Img.north west);%% Upper left corner
    \coordinate(UR) at (Img.north east);%% Upper right corner
    %%
    \draw[Frame] (LL) rectangle (UR);
    \foreach \subtick [evaluate=\subtick as \subpos using \subtick/20] in {1,...,20}{%
      \draw[SubTick]($(LL)!\subpos!(UL)$)--+(0.2,0);
      \draw[SubTick]($(LR)!\subpos!(UR)$)--+(-0.2,0);
      \draw[SubTick]($(LL)!\subpos!(LR)$)--+(0,0.2);
      \draw[SubTick]($(UL)!\subpos!(UR)$)--+(0,-0.2);
    }
    \foreach \tick [evaluate=\tick as \pos using \tick/4,evaluate=\tick as \lab using -1+\tick*0.5] in {0,...,4}{%
      \draw[Tick] ($(LL)!\pos!(UL)$)--+(0.4,0)node[pos=0,left]{$\lab$};
      \draw[Tick] ($(LR)!\pos!(UR)$)--+(-0.4,0)node[pos=0,right]{$\lab$};
      \draw[Tick] ($(LL)!\pos!(LR)$)--+(0,0.4)node[pos=0,below]{$\lab$};
      \draw[Tick] ($(UL)!\pos!(UR)$)--+(0,-0.4)node[pos=0,above]{$\lab$};
    }
  \end{tikzpicture}
\end{figure}
\end{document}

enter image description here

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.