# TikZ: Crop images after placing them

I have a ton of microscopy images to include in a report, some of these images have rather uninteresting information I want to crop. Since I add scale bars using TikZ and therefore require all image dimensions to be precisely known I have used TikZ to crop the images.

In memoir this leads to subfigures being slightly askew, see attached image. I have noticed the same behavior for figures displaying two or more subfigures of different bulk.

Here is the code:

\documentclass{memoir}
\usepackage{mwe}
\usepackage{siunitx}
\usepackage{graphicx}
\usepackage{tikz}
\newsubfloat{figure}
\begin{document}

\begin{figure}
\subbottom[]{
\tikz{
\draw (0,0) node[name=micrograph]{\includegraphics[width=.45\textwidth]{example-image}};
\draw[ultra thick, white] (micrograph.south west)++(0.05*0.45\textwidth,0.05*0.45\textwidth)--++(0.45*0.54177\textwidth,0)node[above,white,midway]{100 \si{\micro\meter}};}
}
\subbottom[]{
\tikz{
\clip (0,0)++(-.3\textwidth,-.3\textwidth) rectangle ++(.45\textwidth,.45\textwidth);
\draw (0,0) node[name=micrograph]{\includegraphics[width=.6\textwidth]{example-image}};
\draw[ultra thick, white] (micrograph.south west)++(0.05*0.5\textwidth,0.05*0.5\textwidth)--++(0.5*0.25806\textwidth,0)node[above,white,midway]{100 \si{\micro\meter}};}
}
\end{figure}
\end{document}


How can I ensure the two images end up aligned?

• What does this have to do with the memoir tag? BTW: why are you using tikz to clip the image? The trim=... plus clip options to \includegraphics can do the same thing. – daleif Nov 20 '13 at 13:40
• Your MWE does not compile for me. – Steven B. Segletes Nov 20 '13 at 13:46
• @StevenB.Segletes, is minimal a bit too minimal in this case? (untested) – daleif Nov 20 '13 at 14:01
• Sorry guys, I made some mistakes when typing up the MWE. Such as using the minimal document class. It is fixed now, and compiles (you need to supply jpgs yourself I fear) @daleif : I use tikz because of the scale bars. I know the size of the original images, and by using this method I can draw accurate scale bars. I fear using graphicx to clip the images will render the scale bars I have added useless. Which in turn renders the image useless. – The V Nov 20 '13 at 14:39
• @Mr.V You can \usepackage{mwe} and \includegraphics{example-image} so no external figures are required for the example. – JLDiaz Nov 20 '13 at 15:12

The problem is the clipping rectangle you are specifying, which starts at -.3\textwidth units below (0,0).

This can be easily detected if you put a \fbox around each tikz picture, to reveal the actual extension of the picture:

\begin{figure}
\subbottom[]{
\fbox{\tikz{
\draw (0,0) node[name=micrograph]{\includegraphics[width=.45\textwidth]{example-image}};
\draw[ultra thick, white] (micrograph.south west)++(0.05*0.45\textwidth,0.05*0.45\textwidth)--++(0.45*0.54177\textwidth, 0)node[above,white,midway]{100 \si{\micro\meter}};}
}}
\subbottom[]{
\fbox{\tikz{
\clip[use as bounding box] (0,0)++(-.3\textwidth,-.3\textwidth) rectangle ++(.45\textwidth,.45\textwidth);
\draw (0,0) node[inner sep=0pt,name=micrograph]{\includegraphics[width=.6\textwidth]{example-image}};
\draw[ultra thick, white] (micrograph.south west)++(0.05*0.5\textwidth,0.05*0.5\textwidth)--++(0.5*0.25806\textwidth,     0)node[above,white,midway]{100 \si{\micro\meter}};}
}}
\end{figure}


I'm not sure of the result you expected. Here is another example in which the second figure is greatly magnified, slightly rotated, and arbitrarly cropped to a "region of interest". In this case, the cropping rectangle has no negative origen, so the unwanted white space do not apear:

\begin{figure}
\subbottom[]{
\fbox{\tikz{
\draw (0,0) node[name=micrograph]{\includegraphics[width=.45\textwidth]{example-image}};
\draw[ultra thick, white] (micrograph.south west)++(0.05*0.45\textwidth,0.05*0.45\textwidth)--++(0.45*0.54177\textwidth,  0)node[above,white,midway]{100 \si{\micro\meter}};}
}}
\subbottom[]{
\fbox{\tikz{
\clip[use as bounding box] (0,0)++(.01\textwidth,.01\textwidth) rectangle ++(.45\textwidth,.45\textwidth);
\draw (0,0) node[inner sep=0pt,name=micrograph]{\includegraphics[angle=10,width=1.6\textwidth]{example-image}};
\draw[ultra thick, white] (micrograph.south west)++(0.05*0.5\textwidth,0.05*0.5\textwidth)--++(0.5*0.25806\textwidth,     0)node[above,white,midway]{100 \si{\micro\meter}};}
}}
\end{figure}


• Thanks! Excellent solution. Just a note for posterity: Using the angle argument for the includegraphics command may alter the image size. In my experience, if you want to add a scale bar with tikz, let tikz do the rotation. Otherwise the image width will not be as finely controlled as you expect, want and need for the scale bar to be accurate. – The V Nov 21 '13 at 8:27