TikZ – How to Annotate a Figure with Lines and Circled Numbers

circlesfloatstikz-pgf

I want to replace static image annotations with annotations in LaTeX to have them be all the same size. I always use figures and sometimes subfigures.

How would I get the same lines as in the image with numbers as in https://tex.stackexchange.com/a/7045/50965 ? Can I resuse that circled code?

annotated image

I tried and searched for some tikz solutions but am not familiar with how I would approach this.

The code of this specific image (in a sub figure) is:

\documentclass{scrbook}
\usepackage{tikz}

\begin{figure}[tph]
\hspace{5mm}\subfloat[figure 1 description]{%
\begin{minipage}[b][1\totalheight][c]{0.4\columnwidth}%
\noindent \begin{center}
\includegraphics[width=1\columnwidth]{path/To/Image}
\par\end{center}%
\end{minipage}



}\hfill{}\subfloat[mentioned figure from question]{\noindent \begin{centering}
\begin{minipage}[b][5.5cm][c]{0.4\columnwidth}%
\noindent \begin{center}
\includegraphics[width=1\columnwidth]{path/To/Image}
\par\end{center}%
\end{minipage}
\par\end{centering}



}\hspace{5mm}

\protect\caption{de}


\end{figure}

Best Answer

This is very easy and may be a duplicate too. Any way, I am posting this and if this is closed a duplicate, I will delete this answer.

\documentclass[tikz,border=2mm]{standalone}
\begin{document}
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0,0) {\includegraphics[width=4in]{example-image-a}};
    \begin{scope}[x={(image.south east)},y={(image.north west)}]
        %% next four lines will help you to locate the point needed by forming a grid. comment these four lines in the final picture.↓
  %      \draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1,1);
%        \draw[help lines,xstep=.05,ystep=.05] (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};}
        %% upto here↑
        \draw[dashed,-latex] (0.8,0.8) -- +(1.1in,0.2in)node[anchor=west] {1};
        \draw[dashed,-latex] (0.6,0.6) -- +(1.9in,0)node[anchor=west] {2};
        \draw[dashed,-latex] (0.4,0.5) -- +(-2in,0)node[anchor=east] {3};
        \draw[dashed,-latex] (0.5,0.4) -- +(-2.4in,-0.5in)node[anchor=east] {4};
    \end{scope}
\end{tikzpicture}

\end{document}

enter image description here

You can use this inside regular fu=igures/subfigures like this. I have modified the minipage dimensions. Roll it back in your case. Also, it is better to use \centering than \begin{center}... \end{center}.

\documentclass{article}
\usepackage{subfig}
\usepackage{tikz}
\tikzset{mynode/.style={draw,solid,circle,inner sep=1pt}}
\begin{document}
\begin{figure}[tph]
\subfloat[figure 1 description]{%
\begin{minipage}{0.4\columnwidth}%
\centering
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0,0) {\includegraphics[width=0.5\columnwidth]{example-image-a}};
    \begin{scope}[x={(image.south east)},y={(image.north west)}]
        \draw[dashed,-latex] (0.8,0.8) -- +(1.1cm,0.2cm)node[mynode,anchor=west] {1};
        \draw[dashed,-latex] (0.6,0.6) -- +(1.9cm,0)node[mynode,anchor=west] {2};
        \draw[dashed,-latex] (0.4,0.5) -- +(-2cm,0)node[mynode,anchor=east] {3};
        \draw[dashed,-latex] (0.5,0.4) -- +(-2.4cm,-0.5cm)node[mynode,anchor=east] {4};
    \end{scope}
\end{tikzpicture}
\end{minipage}
}\hfill{}
\subfloat[mentioned figure from question]{
\centering
\begin{minipage}{0.4\columnwidth}%
\centering
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0,0) {\includegraphics[width=0.5\columnwidth]{example-image-B}};
    \begin{scope}[x={(image.south east)},y={(image.north west)}]
        \draw[dashed,-latex] (0.8,0.8) -- +(1.1cm,0.2cm)node[mynode,anchor=west] {1};
        \draw[dashed,-latex] (0.6,0.6) -- +(1.9cm,0)node[mynode,anchor=west] {2};
        \draw[dashed,-latex] (0.4,0.5) -- +(-2cm,0)node[mynode,anchor=east] {3};
        \draw[dashed,-latex] (0.5,0.4) -- +(-2.4cm,-0.5cm)node[mynode,anchor=east] {4};
    \end{scope}
\end{tikzpicture}
\end{minipage}
}
\caption{de}
\end{figure}
\end{document}

enter image description here

You can define a style for the node like

\tikzset{mynode/.style={draw,solid,circle,inner sep=1pt}}

and use it like

\draw[dashed,-latex] (0.8,0.8) -- +(1.1cm,0.2cm)node[mynode,anchor=west] {1};

addendum from @Hug:

the answer helped very much, but I wanted to also show the complete answer to get a desired result which also has the happy side effects of:

  • image scale agnosticism (through relative positions, not cm)
  • white filled circled numbers, so one can put them inside a figure (achieved through style options)
  • use of styles to easily color the lines according to a color scheme (I used Adobe kuler web app for a custom color scheme for me)
  • no arrow endings of the lines (no -latex option)

this results in the following MWE (with 8 different colors to choose from):

\documentclass{scrbook}
\usepackage{tikz}
%colors
\definecolor{1c1}{RGB}{188,162,6}
\definecolor{1c2}{RGB}{137,129,80}
\definecolor{1c3}{RGB}{239,167,31}
\definecolor{1c4}{RGB}{88,194,241}
\definecolor{1c5}{RGB}{6,180,188}

% stiles used
\tikzset{mynode/.style={draw=black,solid,circle,fill=white,inner sep=2pt, thick, text=black}}
%draw=black to get a black circle, fill=white so it actually has a   
%background and text=black to not get that rendered in the specified color
\tikzset{arrow line/.style={dashed, line width= 2.5pt, color=#1}}
%color is given as a paramter so one can put these two styles in the  
%preamble and easily use throughout the document, line width as it was to small on my images on a page

\begin{figure}[tbph]
\hspace{5mm}\subfloat[figure a]{%
\begin{minipage}[b][1\totalheight][c]{0.4\columnwidth}%
\noindent \begin{center}
\includegraphics[width=1\columnwidth]{path/to/image_a}
\par\end{center}%
\end{minipage}



}\hfill{}\subfloat[description of figure a]{\noindent \centering%
\begin{minipage}[b][5cm][c]{0.4\columnwidth}%
\noindent \begin{center}
\centering
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0] (image) at (0,0,0) {\includegraphics[width=0.7\columnwidth]{path/to/image}};
    \begin{scope}[x={(image.south east)},y={(image.north west)}]
% uncomment the 4 lines for a grid to help with positioning
        % \draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1,1);
        % \draw[help lines,xstep=.05,ystep=.05] (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};}

% uncomment until line above
        \draw[arrow line=1c1] (0.8,0.8) -- +(0.275,0)node[mynode,anchor=west] {\Large 1};
%1c1 is the code for the color, you can enter 1c2 here, too
        \draw[arrow line=1c1] (0.6,0.55) -- +(0.475,0)node[mynode,anchor=west] {\Large 2};
        \draw[arrow line=1c1] (0.35,0.45) -- +(-0.425,0)node[mynode,anchor=east] {\Large 3};
        \draw[arrow line=1c1] (0.375,0.275) -- +(-0.45,-.15)node[mynode,anchor=east] {\Large 4};
    \end{scope}
\end{tikzpicture}
\par\end{center}%
\end{minipage}

}\hspace{5mm}

\protect\caption{outside caption}


\end{figure}

which results in this image:

final resulting image

So, thanks a lot to Harish Kumar, without whose help this hadn't been possible for me.

Related Question