[Tex/LaTex] Alignment and scaling of nested subfigures

graphicspositioningscalingsubfloats

Update: Read this first

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.

Original Post (updates below)

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[includeheadfoot,margin=3cm]{geometry}
\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}

Example Output

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}
\hypersetup{colorlinks=true}

\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}
\hypersetup{colorlinks=true}

\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}

Best Answer

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 + 3ptr)/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.

enter image description here

\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.