# Alignment and scaling of nested subfigures

I originally asked this question to find out how I would show a large image with magnified portions of the same image next to it. I was doing this by manually cropping copies of said image and using the technique discussed here to align them nicely. If this is what you're trying to do, please see this post which discusses how to do this automatically using the spy library of Tikz so you only need to load the one large image.

However, the technique here is still very useful, e.g. if you want to show a large image next to small images that are related but not actually just magnifications of the large image.

I am trying to place one large image on the left next to a group of four small images in a square arrangement on the right. I want it to "look good" where, admittedly, I'm not 100% sure what I'm looking for layout-wise. It seems to me that at least the top of top images and bottom of bottom images on the right hand side should align with the top and bottom of the large image on the left, and overall the whole thing should have a fairly "centered" look on the page.

I've included an example to show what I'm trying to do. How do I achieve a good look with this layout of images, and one that will extend to graphics that are included with the \includegraphics command; the actual images I'm working with are scaled differently from the black boxes in the example and look even worse.

If I were working in something like Powerpoint, I would resize the left image until it was the same height as the combined right images, then align their middles, so I'm curious if we can get a similar effect in Latex.

I'm not 100% committed to the layout I've started with, so if someone has a really different idea, e.g. tabular environment, subfigs, etc, that's fine. Also, all the images I'm working with have the same aspect ratio, though not necessarily the same size.

Thanks.

Example:

\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage[font=small,labelfont=bf,tableposition=top]{caption}
\usepackage{blindtext}
\usepackage{subcaption}

\begin{document}
\blindtext

\newcommand{\localImgScale}{.49}
\newcommand{\localMinipageScale}{.45}
\begin{figure}[!htbp]
\centering
\begin{minipage}[b]{\localImgScale\linewidth}
\centering
\setlength\fboxsep{0pt}
\rule{6.4cm}{3.6cm}%
\subcaption{}
\end{minipage}
\begin{minipage}[b]{.48\linewidth}
\centering
\begin{minipage}[b]{\localMinipageScale\linewidth}
\centering
\rule{3.2cm}{1.8cm}%
\end{minipage}
\begin{minipage}[b]{\localMinipageScale\linewidth}
\centering
\rule{3.2cm}{1.8cm}%
\end{minipage}\\[0.2em]
\begin{minipage}[b]{\localMinipageScale\linewidth}
\centering
\rule{3.2cm}{1.8cm}%
\end{minipage}
\begin{minipage}[b]{\localMinipageScale\linewidth}
\centering
\rule{3.2cm}{1.8cm}%
\end{minipage}
\subcaption{}
\end{minipage}
\caption{Test images. The top and of the top right hand images should align with the top of the left image and the bottom of the bottom right hand images with the bottom of the left image.}
\end{figure}

\blindtext
\end{document}


Update 1: egreg provided a nice answer, and I've just modified it slightly to get it working with subcaptions which I am using. Here is a MWE including hyper-referencing and a new aspectRatio variable that can be renewed in the document body as needed. Not sure how much re-jigging this needs to allow for different aspect ratios between the big and small images within a figure.

\documentclass[a4paper]{article}
\usepackage{graphicx}
\usepackage{caption,subcaption}
\usepackage[pdftex]{hyperref}

\newcommand{\aspectRatio}{2}

\begin{document}

\renewcommand{\aspectRatio}{4/3}

\begin{figure}
\centering
\subcaptionbox{\label{label1}}{\includegraphics[width=\dimexpr(\textwidth-15pt+3pt*\aspectRatio)/2\relax]{img1filename}}%
\hfill
\subcaptionbox{\label{label2}}{\vbox{\offinterlineskip\halign{#\hskip3pt&#\cr
\includegraphics[width=\dimexpr(\textwidth-15pt-3pt*\aspectRatio)/4\relax]{img2filename}&
\includegraphics[width=\dimexpr(\textwidth-15pt-3pt*\aspectRatio)/4\relax]{img3filename}\cr
\noalign{\vskip3pt}
\includegraphics[width=\dimexpr(\textwidth-15pt-3pt*\aspectRatio)/4\relax]{img4filename}&
\includegraphics[width=\dimexpr(\textwidth-15pt-3pt*\aspectRatio)/4\relax]{img5filename}\cr
}}}
\caption{Test images. \subref{label1} The Big image. \subref{label2} The Small Images.}
\end{figure}

\end{document}


Update 2

I recently used this method again, this time for one large and two small images. Here's the code for that layout, where I've also made commands for the big separation widths and small separation widths so they're a bit easier to modify.

\documentclass[a4paper]{article}
\usepackage{graphicx}
\usepackage{caption,subcaption}
\usepackage[pdftex]{hyperref}

\newcommand{\aspectRatio}{2}
\newcommand\bigsep{12pt}
\newcommand\smallsep{5pt}

\begin{document}

% For each figure, set the aspect ratio of the input images (assumes they're all same AR), and desired separation widths
\renewcommand{\aspectRatio}{4/3}
\renewcommand\bigsep{12pt}
\renewcommand\smallsep{3pt}
\begin{figure}
\centering
\subcaptionbox{\label{label1}}{\includegraphics[width=\dimexpr(2\textwidth-\bigsep*2+\smallsep*\aspectRatio)/3\relax]{img1filename}}%
\hfill
\subcaptionbox{\label{label2}}{\vbox{\offinterlineskip\halign{#\hskip3pt&#\cr
\includegraphics[width=\dimexpr(\textwidth-\bigsep-\smallsep*\aspectRatio)/3\relax]{img2filename}\cr
\noalign{\vskip3pt}
\includegraphics[width=\dimexpr(\textwidth-\bigsep-\smallsep*\aspectRatio)/3\relax]{img3filename}\cr
}}}
\caption{Test images. \subref{label1} The Big image. \subref{label2} The Small Images.}
\end{figure}

\end{document}

-
I don't understand fully what you exactly need. Should the four smaller images now be scaled to the same size or not? You can set the height of the included images using \includegraphics[height=...]{filename}. –  Martin Scharrer Jul 16 '11 at 20:57
Well, on the one hand, I was curious to see how other people would lay out these images if the small ones on the right were related to the large one on the left, e.g. imagine they are four magnifications of small portions of the image on the left. How would you choose to lay these out in a pleasing arrangement? And for the specific arrangement I've shown above, since the combined images on the right are taller than the one on the left, I think it looks kinda crappy, so I thought it would be nice if the overall height of the images on the right matched that of the image on the left. –  SSilk Jul 16 '11 at 21:52

If the five images have the same aspect ratio, it's not difficult to solve the problem: let's say that the rectangles have base a and height a/r (with possibly different a each). Let's say we want to leave 12pt of blank space between the two halves and 3pt of space between the small figures.

We have thus to compute dimensions x and y, the bases of the big rectangle and of the small ones respectively, such that

x + 2y = \textwidth-12pt-3pt
x/r = 2y/r + 3pt

A bit of linear algebra will tell that

x = (\textwidth - 15pt + 3pt*r)/2
y = (\textwidth - 15pt - 3pt*r)/4

Let's say that r=2; then

\documentclass[a4paper]{article}
\usepackage{graphicx,subfig}
\begin{document}

\begin{figure}
\centering
\subfloat[]{\includegraphics[width=\dimexpr(\textwidth-15pt+3pt*2)/2\relax]{fig1}}%
\hfill
\subfloat[]{\vbox{\offinterlineskip\halign{#\hskip3pt&#\cr
\includegraphics[width=\dimexpr(\textwidth-15pt-3pt*2)/4\relax]{fig2}&
\includegraphics[width=\dimexpr(\textwidth-15pt-3pt*2)/4\relax]{fig3}\cr
\noalign{\vskip3pt}
\includegraphics[width=\dimexpr(\textwidth-15pt-3pt*2)/4\relax]{fig4}&
\includegraphics[width=\dimexpr(\textwidth-15pt-3pt*2)/4\relax]{fig5}\cr
}}}
\caption{Test images}
\end{figure}

\end{document}


If the package subcaption is used instead of subfig, change \subfloat[] into \subcaptionbox{} (thanks to Axel Sommerfeldt).

I've used \halign to get a more precise control on the spacing.

If the figure on the left has a different aspect ratio than the ones on the right, it's not difficult either.

\halign is the TeX primitive on which tabular is built. There's too much to say about it; the main thing is the structure:

\halign{<template1>&<template2>&...\cr
<body>
}


The tokens up to the first \cr define the templates for the columns; each is separated from the following by &. A template consists of three parts:

<before>#<after>


<before> means tokens that are inserted at the start of each cell, # stands for the actual cell entry in the body, and similarly <after> means tokens that are inserted after the entry. For example a template for "centered cells" is

\hfil#\hfil


In our case the templates are trivial: #\hskip3pt and #; since all images will have the same width it's not necessary to specify anything else.

Notice that \halign, by default, doesn't insert intercolumn space; to be precise it inserts \tabalign glue, but talking about this would take too far away. The default value of the parameter is zero, anyway.

<body> is similar to the body of a tabular environment, but rows must be terminated by \cr and spaces before & and \cr are not ignored; those after them are. Also the final row must be terminated by \cr.

Between rows one can specify spacing (and other things) via \noalign.

To get precise control on spacing, I've said \offinterlineskip before \halign so that TeX will butt all rows together without interline glue. LaTeX does the same, actually. This is done inside a \vbox, so that the setting will be in force only there.

-
That is awesome, and does exactly what I wanted. Thank you very much. Could you briefly clarify what halign does in this code? Thanks. –  SSilk Jul 16 '11 at 22:45
Another more important follow-up question: with your method, subref doesn't work for me. Where exactly do I put the label for the subfigures to get subref to work? Right now it's just finding the labels are undefined. –  SSilk Jul 17 '11 at 0:50
When using the subcaption package please don't use \subfloat but \subcaptionbox instead. The \label must be placed inside the caption text. –  Axel Sommerfeldt Jul 17 '11 at 5:59
@Axel: sorry, I didn't see that the OP uses subcaption. You might add a subcaption version, just to check that the solution works also in that case. –  egreg Jul 17 '11 at 8:28
Thanks, egreg. This one seems to work with subcaption, too, one only need to replace \subfloat[] with \subcaptionbox{}. –  Axel Sommerfeldt Jul 17 '11 at 18:05