# subfigures using subfloatrow, overpic, tikz to add rows to annotate imported images/figures

I did my diligent search for a combination of subfloatrow, overpic, and adding arrows for annotation of imported images using tikz, but couldn't find one. These kind of images are typically found in the biology field, they could be for western blots or gel electrophoresis, etc...The idea is always the same; is to get multiple images annotated with text (courier fits well here), and frequently, with elegant, yet simple, arrows to point at a specific band or spot of interest in the image.

Recommended unless you report a better one!

• overpic: a very nice solution to these kind of text annotations
• floatrow: AFIK is the best one when it comes to handling subfloats (figures and tables) as well as subcaptions and many other features
• mdframed: a feature-rich package to control frames
• tikz: a no-comment-package for drawing arrows and figures (a tikz fan!)

• subfig: obsolete
• subfigure: supported not maintained
• subcaption: fine, but its manual directs you to the better one, which is the subfloatrow environment of floatrow for more complex subfloats

What is needed now?

• adding arrows to at least one these subfigures in the Minimal Working Example (MWE), horizontal ones, left to right and in opposite direction as well. Please provide relative measurements to fine-tune the arrow positioning. Different, elegant arrows of tikz are welcomed, did I forget to mention elegant here?

• Controlling the space between these images (in this case increasing it)

• Referring to the subfigures in the main figure caption

The code of MWE

\documentclass[12pt]{scrartcl} % KOMA script
\usepackage{courier} % courier font for text annotation
\usepackage[labelsep=period]{caption} % for appearance of captions not placings
\usepackage{subcaption} % incompatible with subfig, or subfigure (obsolete)
\usepackage[demo]{graphicx} % for graphcis, demo to provide image placeholders
\usepackage{xcolor} % colors
\usepackage[framemethod=tikz]{mdframed} % for frames, load xcolor before!
\usepackage{tikz} % drawing arrows and graphics
\usetikzlibrary{arrows} % arrow library for tikz
\usepackage{floatrow} % has the subfloatrow environment for subfloats
\usepackage[percent]{overpic} % for text annotation over imported pictures
\begin{document}
\begin{figure}
\begin{mdframed}[align=center,linecolor=black,fontcolor=black,backgroundcolor=white,userdefinedwidth=0.9\textwidth,roundcorner=5pt,skipabove=10pt,skipbelow=10pt,leftmargin=0pt,innertopmargin=100pt,innerbottommargin=0pt, innerleftmargin=50pt,innerrightmargin=20pt, innerlinewidth=0pt, middlelinewidth=0pt,outerlinewidth=1pt] % change the innertopmargin for labels
\floatsetup[subfigure]{captionskip=10pt}
\ffigbox { %
\begin{subfloatrow}[3]% number of subfigures are 3, 2 by default
\ffigbox{
\begin{overpic}[grid,tics=15]{image1} % remove the grid, tics argument later on
\put (-30,0) {$\beta$-actin}
\put(1,67){\rotatebox{90}{\texttt{Lane1: geneA}}}
\put(15,67){\rotatebox{90}{\texttt{Lane2: geneB}}}
\put(30,67){\rotatebox{90}{\texttt{Lane3: geneC}}}
\end{overpic}
}{\subcaption{subcap1}}
\ffigbox{\includegraphics{image2}}{\subcaption{subcap2}} % you can do the same as image 1 with overpic
\ffigbox{\includegraphics{image3}}{\subcaption{subcap3}} % you can do the same as image 1 with overpic
\end{subfloatrow}
}{\caption{A Minimal Working Example}}
\end{mdframed}
\end{figure}


Which gives

The basic idea is to use TikZ to place the images inside nodes with names; this will give you control over the positioning and will also allow the placement of arrows.

The requirement about cross-referencing can be fulfilled simply by using the \label, \ref mechanism.

## Some explanations

I decided not to use overpic at all. The grid and placement of labels can be done entirely through TikZ; in the following example I used Caramdir's and Jake's answers to Drawing on an image with TikZ and defined a \mygrid command to place a labelled grid on each image; the mandatory argument of \mygrid is the name of the node used to hold the image:

\documentclass[12pt]{scrartcl}
\usepackage{courier}
\usepackage[labelsep=period]{caption}
\usepackage{subcaption}
\usepackage{graphicx}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}
\usepackage{floatrow}

\newcommand\mygrid[1]{%
\begin{scope}[x={(#1.south east)},y={(#1.north west)}]
\draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1,1);
\foreach \x in {0,1,...,10} { \node [anchor=north,font=\tiny] at (\x/10,0) {\x}; }
\foreach \y in {0,1,...,10} { \node [anchor=east,font=\tiny] at (0,\y/10) {\y}; }
\end{scope}%
}

\begin{document}

\begin{figure}
\begin{mdframed}[
align=center,
linecolor=black,
fontcolor=black,
backgroundcolor=white,
%userdefinedwidth=0.9\textwidth,
roundcorner=5pt,
skipabove=10pt,
skipbelow=10pt,
leftmargin=0pt,
innertopmargin=100pt,
innerbottommargin=0pt,
innerlinewidth=0pt,
middlelinewidth=0pt,
outerlinewidth=1pt
]
\floatsetup[subfigure]{captionskip=10pt}
\ffigbox{%
\begin{subfloatrow}[3]
\ffigbox{%
\begin{tikzpicture}[remember picture]
\node[anchor=south west,inner sep=0] (image1)
{\includegraphics[width=3.8cm]{ctanlion}};
\mygrid{image1}
\end{tikzpicture}
}%
{\subcaption{subcap1}\label{sfig:a}}%
\ffigbox{%
\begin{tikzpicture}[remember picture]
\node[anchor=south west,inner sep=0] (image2)
{\includegraphics[width=3.8cm]{ctanlion}};
\mygrid{image2}
\end{tikzpicture}
}%
{\subcaption{subcap2}\label{sfig:b}}
\ffigbox{%
\begin{tikzpicture}[remember picture]
\node[anchor=south west,inner sep=0] (image3)
{\includegraphics[width=3.8cm,height=2cm]{ctanlion}};
\mygrid{image3}
\end{tikzpicture}
}%
{\subcaption{subcap3}\label{sfig:c}}
\end{subfloatrow}
}{\caption{A Minimal Working Example with three subfigures:~\ref{sfig:a}, \ref{sfig:b} and~\ref{sfig:c}}}
\end{mdframed}
\end{figure}

\end{document}


This produces:

Now, with the help of the grids, we can draw some arrows and place the labels:

\documentclass[12pt]{scrartcl}
\usepackage{courier}
\usepackage[labelsep=period]{caption}
\usepackage{subcaption}
\usepackage{graphicx}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}
\usepackage{floatrow}

\newcommand\mygrid[1]{%
\begin{scope}[x={(#1.south east)},y={(#1.north west)}]
\draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1,1);
\foreach \x in {0,1,...,10} { \node [anchor=north,font=\tiny] at (\x/10,0) {\x}; }
\foreach \y in {0,1,...,10} { \node [anchor=east,font=\tiny] at (0,\y/10) {\y}; }
\end{scope}%
}

\begin{document}

\begin{figure}
\begin{mdframed}[
align=center,
linecolor=black,
fontcolor=black,
backgroundcolor=white,
%userdefinedwidth=0.9\textwidth,
roundcorner=5pt,
skipabove=10pt,
skipbelow=10pt,
leftmargin=0pt,
innertopmargin=100pt,
innerbottommargin=0pt,
innerlinewidth=0pt,
middlelinewidth=0pt,
outerlinewidth=1pt
]
\floatsetup[subfigure]{captionskip=10pt}
\ffigbox{%
\begin{subfloatrow}[3]
\ffigbox{%
\begin{tikzpicture}[remember picture]
\node[anchor=south west,inner sep=0] (image1)
{\includegraphics[width=3.8cm]{ctanlion}};
\mygrid{image1}
\end{tikzpicture}
}%
{\subcaption{subcap1}\label{sfig:a}}%
\ffigbox{%
\begin{tikzpicture}[remember picture]
\node[anchor=south west,inner sep=0] (image2)
{\includegraphics[width=3.8cm]{ctanlion}};
\mygrid{image2}
\end{tikzpicture}
}%
{\subcaption{subcap2}\label{sfig:b}}
\ffigbox{%
\begin{tikzpicture}[remember picture]
\node[anchor=south west,inner sep=0] (image3)
{\includegraphics[width=3.8cm,height=2cm]{ctanlion}};
\mygrid{image3}
\end{tikzpicture}
}%
{\subcaption{subcap3}\label{sfig:c}}
\end{subfloatrow}
}{\caption{A Minimal Working Example with three subfigures:~\ref{sfig:a}, \ref{sfig:b} and~\ref{sfig:c}}}
\end{mdframed}
\end{figure}

\begin{tikzpicture}[remember picture,overlay]
% Some arrows
\draw[->,line width=1.5pt,cyan!80!black,dashed]
([yshift=-40pt,xshift=20pt]image2.north) |- +(0,80pt) -|
([yshift=-40pt,xshift=20pt]image3.north);
\draw[->,line width=1.5pt,orange!80!black]
([yshift=-10pt,xshift=20pt]image2.west) |-
([yshift=-10pt,xshift=20pt]image3.west);
\draw[->,line width=1.5pt,red!80!black]
([xshift=20pt]image1.west) |- +(0,70pt) -|
([yshift=20pt,xshift=30pt]image3.west);
\draw[->,line width=1.5pt,green!80!black,dashed]
([yshift=20pt,xshift=-43pt]image1.east) |- +(0,-73pt) -|
([yshift=-15pt,xshift=-15pt]image3.east);
% Some labels
\node[anchor=north west,rotate=90,font=\ttfamily]
at (image1.north west) {Lane1: geneA};
\node[anchor=north west,rotate=90,font=\ttfamily,yshift=-15pt]
at (image1.north west) {Lane2: geneB};
\node[anchor=north west,rotate=90,font=\ttfamily,yshift=-30pt]
at (image1.north west) {Lane3: geneC};\end{tikzpicture}

\end{document}


After commenting out the three \mygrid{...} lines, one gets:

If the general caption must appear outside the frame, one can leave empty the corresponding argument (in this case, the second argument of the outer \ffigbox), and then use \RawCaption outside the mdframed environment. To keep synchronicity, however, the figure counter will have to be manually stepped at the beginning (this is not necessary if the caption goes before the object):

\documentclass[12pt]{scrartcl}
\usepackage{courier}
\usepackage[labelsep=period]{caption}
\usepackage{subcaption}
\usepackage{graphicx}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}
\usepackage{floatrow}

\newcommand\mygrid[1]{%
\begin{scope}[x={(#1.south east)},y={(#1.north west)}]
\draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1,1);
\foreach \x in {0,1,...,10} { \node [inner ysep=0pt,anchor=north,font=\tiny] at (\x/10,0) {\x}; }
\foreach \y in {0,1,...,10} { \node [inner ysep=0pt,anchor=east,font=\tiny] at (0,\y/10) {\y}; }
\end{scope}%
}

\begin{document}
\floatsetup[figure]{style=plain,capposition=bottom}
\begin{figure}
\begin{mdframed}[
align=center,
linecolor=black,
fontcolor=black,
backgroundcolor=white,
%userdefinedwidth=0.9\textwidth,
roundcorner=5pt,
skipabove=10pt,
skipbelow=10pt,
leftmargin=0pt,
innertopmargin=100pt,
innerbottommargin=0pt,
innerlinewidth=0pt,
middlelinewidth=0pt,
outerlinewidth=1pt
]
\floatsetup[subfigure]{captionskip=10pt}
\ffigbox{%
\stepcounter{figure}
\begin{subfloatrow}[3]
\ffigbox{%
\begin{tikzpicture}[remember picture]
\node[anchor=south west,inner sep=0] (image1)
{\includegraphics[width=3.8cm]{ctanlion}};
%\mygrid{image1}
\end{tikzpicture}
}%
{\subcaption{subcap1}\label{sfig:a}}%
\ffigbox{%
\begin{tikzpicture}[remember picture]
\node[anchor=south west,inner sep=0] (image2)
{\includegraphics[width=3.8cm]{ctanlion}};
%\mygrid{image2}
\end{tikzpicture}
}%
{\subcaption{subcap2}\label{sfig:b}}
\ffigbox{%
\begin{tikzpicture}[remember picture]
\node[anchor=south west,inner sep=0] (image3)
{\includegraphics[width=3.8cm,height=2cm]{ctanlion}};
%\mygrid{image3}
\end{tikzpicture}
}%
{\subcaption{subcap3}\label{sfig:c}}
\end{subfloatrow}
}{}% empty second argument; the caption goes outside the frame
\end{mdframed}
\RawCaption{\caption{A Minimal Working Example with three subfigures:~\ref{sfig:a}, \ref{sfig:b} and~\ref{sfig:c}}\label{test}}
\end{figure}

\begin{tikzpicture}[remember picture,overlay]
% Some arrows
\draw[->,line width=1.5pt,cyan!80!black,dashed]
([yshift=-40pt,xshift=20pt]image2.north) |- +(0,80pt) -|
([yshift=-40pt,xshift=20pt]image3.north);
\draw[->,line width=1.5pt,orange!80!black]
([yshift=-10pt,xshift=20pt]image2.west) |-
([yshift=-10pt,xshift=20pt]image3.west);
\draw[->,line width=1.5pt,red!80!black]
([xshift=20pt]image1.west) |- +(0,70pt) -|
([yshift=20pt,xshift=30pt]image3.west);
\draw[->,line width=1.5pt,green!80!black,dashed]
([yshift=20pt,xshift=-43pt]image1.east) |- +(0,-73pt) -|
([yshift=-15pt,xshift=-15pt]image3.east);
% Some labels
\node[anchor=north west,rotate=90,font=\ttfamily]
at (image1.north west) {Lane1: geneA};
\node[anchor=north west,rotate=90,font=\ttfamily,yshift=-15pt]
at (image1.north west) {Lane2: geneB};
\node[anchor=north west,rotate=90,font=\ttfamily,yshift=-30pt]
at (image1.north west) {Lane3: geneC};
\end{tikzpicture}

\end{document}


CTAN lion drawing by Duane Bibby.

• excellent answer +1 for skipping the need to a package and the neat example, of course. But can you please include text labels rotated like in the MWE? that would be great. Apr 8, 2013 at 1:30
• @doctorate Of course, give me some minutes and I'll add the text. Apr 8, 2013 at 1:44
• @doctorate please see my updated answer. Apr 8, 2013 at 2:13
• that was great and very thorough indeed. Thanks a lot. Apr 8, 2013 at 7:35
• @doctorate please see my updated answer. Apr 8, 2013 at 13:55

With PSTricks and without subfigure (or its variants). I use two figures just for the sake of simplicity so you can extend it to 3 or more!

Note that there are many connecting lines you can choose in PSTricks such as straight line, arc, curve, etc.

% filename is main.tex
% compile it with pdflatex -shell-escape main.tex
% ===============================================
\documentclass[pdf]{article}
\usepackage{pst-node}

\usepackage{graphicx}
\def\Scale{1.25}
\def\ShowGrid{top}

\newsavebox\IBox
\sbox\IBox{\includegraphics[scale=\Scale]{baby}}

\psset
{
xunit=\dimexpr\wd\IBox/10,
yunit=\dimexpr\ht\IBox/10,
}

{
griddots=0,
subgriddiv=2,
gridwidth=0.2pt,
subgridwidth=0.1pt,
gridcolor=white,
subgridcolor=cyan!50,
gridlabels=4pt,
}

\def\SubFigLeft{%
\begin{pspicture}[showgrid=\ShowGrid](0,-1)(10,10)
\rput[bl](0,0){\usebox\IBox}
\pnode(4.2,4.8){Left}
\end{pspicture}}

\def\SubFigRight{%
\begin{pspicture}[showgrid=\ShowGrid](0,-1)(10,10)
\pnode(5.2,4.8){Right}
\end{pspicture}}

\begin{document}
Karl's student do not care about dashing patterns. Karl's students do not care about arrow tips. Karl' students, by the way, do not know what a transformation matrix is.
\begin{figure}[hbtp]
\centering
\begin{pspicture}[showgrid=false](0,-1)(21,10)
\psframe[framearc=0.1,linecolor=orange,linewidth=2pt](-1,-2)(22,11)
\rput[bl](0,-1){\SubFigLeft }
\rput[bl](11,-1){\SubFigRight}
\pcbar[linestyle=dashed,linecolor=magenta,linewidth=2pt,angle=90,arm=1]{->}(Left)(Right)
\end{pspicture}
\caption{The difference between angry and happy.}
\label{fig:Angry-Happy}
\end{figure}

Karl's student do not care about dashing patterns. Karl's students do not care about arrow tips. Karl' students, by the way, do not know what a transformation matrix is.
\begin{figure}[hbtp]
\centering
\def\ShowGrid{false}
\begin{pspicture}[showgrid=false](0,-1)(21,10)
\psframe[framearc=0.1,linecolor=orange,linewidth=2pt](-1,-2)(22,11)
\rput[bl](0,-1){\SubFigLeft}
\rput[bl](11,-1){\SubFigRight}
\rput(5,-1){(a) Angry}
\rput(16,-1){(b) Happy}
\pcarc[linestyle=dashed,linecolor=magenta,linewidth=2pt,angle=30]{->}(Left)(Right)
\end{pspicture}
\caption{The difference between angry and happy.}
\label{fig:Angry-Happy}
\end{figure}
\end{document}

• Please concentrate on the code rather than on the right figure. Apr 8, 2013 at 8:22
• But manually placing the labels for the subfigures prevents from cross-referencing them; can your code be modified so that subfigures are automatically labelled? Apr 8, 2013 at 16:30
• @GonzaloMedina: Yes it is possible but I have to use xelatex (or latex->dvips->ps2pdf if EPS images are available) as the nodes have to be in the same physical page. Apr 8, 2013 at 16:36