3

img

Hi. I would like to create this graphic inside of LaTex, instead of what I had to do (bring both images into powerpoint and add text boxes, save all that as a tiff file, and then convert to eps file) in order to use in LaTex. The current version is a little blurry, in the text boxes in particular.

For clarity, I have the 2 images as eps files (and matlab figures). I would like to overlay the zoomed image over the right quadrant of the non-zoomed image. I would like to add arrows or black lines to demarcate the zoom.

I tried to play around with TIKZ, but could not figure out how to replicate that image.

Any thoughts?

Thanks!!

David Carlisle
  • 757,742
Greg
  • 101

2 Answers2

5

In my opinion, preparing two images is not ideal: if you want to change the zoomed area, you have to go prepare a separate image in some other program, carefully calculating the required crop.

The power of TikZ can do this for you through the spy library. Then, outside of the spy scope, the annotations can be added, using the spy node name to help locate them. The zoomed image looks pixelated here, but if you have sufficient resolution in your base image, this will not be a problem.

\documentclass[tikz]{standalone}
\usetikzlibrary{spy}
\tikzset{annot/.style={draw=black,fill=white,text=black}}

\begin{document}
\begin{tikzpicture}
  \begin{scope}[spy using outlines={rectangle,magnification=3,connect spies,size=1.5cm}]
    \node[inner sep=0,outer sep=0,anchor=south west] (image) at (0,0) 
      {\includegraphics[width=5cm]{example-image-a}};
    \spy on (2.125,1.57) in node (zoom) at (4,2.75);
  \end{scope}
  \draw[blue] (zoom.south west) ++(0.4,0.2) -- ++(0.2,-0.8) node[annot,below] {Black};
  \draw[blue] (zoom.north west) ++(0.25,-0.2) -- ++(-1,-0.1) node[annot,left] {Grey};
\end{tikzpicture}
\end{document}

enter image description here

The library has a gold-mine worth of options for styling the insets just the way you like. Check out Section 68 of the pgf manual for the full gritty detail. :-)

Paul Gessler
  • 29,607
2

Here's one possibility. The grid lines shown are, of course, only for use in the drafting stage where you are figuring out the coordinates needed to place the second image, box and arrow.

\documentclass[tikz,border=5pt]{standalone}
\usepackage{graphicx}
\begin{document}

  \begin{tikzpicture}
    % http://tex.stackexchange.com/a/9561/ - Caramdir
    \node (img1) [anchor=south west, inner sep=0pt] at (0,0) {\includegraphics{example-image-a}};
    \begin{scope} [x={(img1.south east)}, y={(img1.north west)}]
        % http://tex.stackexchange.com/a/9562/ - jake
        \draw [help lines, step=.1] (0,0) grid (1,1);
        \foreach \x in {0,1,...,9} { \node [anchor=north] at (\x/10,0) {0.\x};}
        \foreach \y in {0,1,...,9} { \node [anchor=east] at (0,\y/10) {0.\y};}
        \draw [red, thick] (.7,.3) rectangle +(.1,.1);
        \node (img2) [anchor=north west, inner sep=0pt] at (.9,.2) {\includegraphics{example-image-b}};
        \draw [ultra thick, ->] (.8,.3) -- (img2.north west);
    \end{scope}
  \end{tikzpicture}

\end{document}

image on image

To place annotations on the overlaid image, you can use a variation on the same trick:

\begin{scope}[shift=(img2.south west), x={(img2.south east)}, y={(img2.north west)}]
  \draw [help lines, step=.1] (0,0) grid (1,1);
  \foreach \x in {0,1,...,9} {
    \node [anchor=north] at (\x/10,0) {0.\x};
    \node [anchor=east] at (0,\x/10) {0.\x};
  }
  \node at (.7,.8) [anchor=north west, fill=white, align=center] {Here is an\\annotation};
\end{scope}

Again, grid lines are just to help during construction.

grid overlaid on grid

Complete code:

\documentclass[tikz,border=5pt]{standalone}
\usepackage{graphicx}
\begin{document}

  \begin{tikzpicture}
    % http://tex.stackexchange.com/a/9561/ - Caramdir
    \node (img1) [anchor=south west, inner sep=0pt] at (0,0) {\includegraphics{example-image-a}};
    \begin{scope} [x={(img1.south east)}, y={(img1.north west)}]
        % http://tex.stackexchange.com/a/9562/ - jake
        \draw [help lines, step=.1] (0,0) grid (1,1);
        \foreach \x in {0,1,...,9} { \node [anchor=north] at (\x/10,0) {0.\x};}
        \foreach \y in {0,1,...,9} { \node [anchor=east] at (0,\y/10) {0.\y};}
        \draw [red, thick] (.7,.3) rectangle +(.1,.1);
        \node (img2) [anchor=north west, inner sep=0pt] at (.9,.2) {\includegraphics{example-image-b}};
        \draw [ultra thick, ->] (.8,.3) -- (img2.north west);
    \end{scope}
    \begin{scope}[shift=(img2.south west), x={(img2.south east)}, y={(img2.north west)}]
        \draw [help lines, step=.1] (0,0) grid (1,1);
        \foreach \x in {0,1,...,9} {
            \node [anchor=north] at (\x/10,0) {0.\x};
            \node [anchor=east] at (0,\x/10) {0.\x};
        }
        \node at (.7,.8) [anchor=north west, fill=white, align=center] {Here is an\\annotation};
    \end{scope}
  \end{tikzpicture}
\end{document}
cfr
  • 198,882