# Find the width of a figure in latex, to properbly align multiple minipages within a figure

I want something like this: made with this MWE:

\documentclass{memoir}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{duckuments}
\newsubfloat{figure}
\begin{document}
\begin{figure}
\centerfloat
\begin{minipage}{10cm}
\subbottom[huey]{\includegraphics[width=10cm]{example-image-duck}}
\end{minipage}
\begin{minipage}{0.5\textwidth}
\subtop[duey]{\includegraphics[width=5cm]{example-image-duck}}
\subbottom[luey]{\includegraphics[width=5cm]{example-image-duck}}
\end{minipage}
\caption{Donalds nephews}
\label{fig:nephew}
\end{figure}
\end{document}


But where the width of the first figure is unknown, so I cannot make a minipage, then I get:

\documentclass{memoir}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{duckuments}
\newsubfloat{figure}
\begin{document}
\begin{figure}
\centerfloat
%   \begin{minipage}{10cm}
\subbottom[huey]{\includegraphics[width=10cm]{example-image-duck}} %The 10 cm is unknown
%   \end{minipage}
\begin{minipage}{0.5\textwidth}
\subtop[duey]{\includegraphics[width=5cm]{example-image-duck}}
\subbottom[luey]{\includegraphics[width=5cm]{example-image-duck}}
\end{minipage}
\caption{Donalds nephews}
\label{fig:nephew}
\end{figure}
\end{document}


Is there a way to either find the width from the \includegraphics command, or align the figures without the minipage?

• \includegraphics produces a box, therefore you can always get the dimensions of an included image using \settowidth, \settoheight and \settodepth (use a search engine). – frougon May 9 at 12:26
• your question isn't so clear,you set the minipage to .5\textwidth, so don't you want the left larger figure to be .5\textwidth (or a bit less if you want a gap) why do you need to know 10cm? (similarly why force the smaller figures to 5cm rather than use \linewidth (teh width of the minipage) – David Carlisle May 9 at 12:35
• @DavidCarlisle I don't really wan't to force the width of any of the figures, but example images are all (unless forced) the same size. I wanted to simulate having a large (tall), and two small (short) figures, and the wish to arrange them as shown in the first picture (without knowing the width of any one figure). – Thorbjørn E. K. Christensen May 9 at 12:41
• @DavidCarlisle I need to know the width of the first figure, so I don't make the minipage too shallow or wide (which would push/pull the small figures to the right). I wan't the left figure to have it's "natural" size – Thorbjørn E. K. Christensen May 9 at 12:45
• You presumably want to align the center of the minipage with the center of the \subbottom. The baseline of a minipage is the center by default, but \subbotom appears to be using the baseline of the image. \raisebox{\dimexpr 0.5\depth-0.5\height}{...} will center anything. – John Kormylo May 9 at 13:07

Here is a proposal for the computation of the desired width. I am using pgf here to make the computation more comprehensible, in principle it can be stripped off. And one can modify things to respect the heights of the captions, too, but this is the basic computation.

\documentclass{memoir}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{duckuments}
\newsubfloat{figure}
\usepackage{pgf}
\begin{document}
We are given the aspect ratios $r_i$ of three figures, $r_i$. Let's call their
final heights $y_i$ and widths $x_i$, i.e.\ $r_i=y_i/x_i$. We want the sum
of heights of figures 2 and 3 to coincide with the height of figure 1,
$y_2+y_3=r_2x_2+r_3x_3=x_2(r_2+r_3)\stackrel{!}{=}y_1=r_1x_1\;,$
where we have used that figures 2 and 3 should be equally wide, $x_2=x_3$. We
also want the width of figures 1 and 2 (or, equivalently, 1 and 3) to add up to
some target width $t$, $x_1+x_2=t$. Therefore
$x_1=\frac{r_2+r_3}{r_1+r_2+r_3}t\;.$
The following example uses $t=0.95$\textbackslash\texttt{textwidth}. In
this version, the heights of the captions have not been taken into account.

% in general the ratios may not coincide
\pgfmathsetmacro{\rOne}{height("\includegraphics{example-image-duck}")/width("\includegraphics{example-image-duck}")}
\pgfmathsetmacro{\rTwo}{height("\includegraphics{example-image-duck}")/width("\includegraphics{example-image-duck}")}
\pgfmathsetmacro{\rThree}{height("\includegraphics{example-image-duck}")/width("\includegraphics{example-image-duck}")}
\pgfmathsetmacro{\xOne}{((\rOne+\rTwo)/(\rOne+\rTwo+\rThree))*0.95*\textwidth}
\pgfmathsetmacro{\xTwo}{0.95*\textwidth-\xOne}

\begin{figure}
\centerfloat
\begin{minipage}{\xOne pt}
\subbottom[huey]{\includegraphics[width=\xOne pt]{example-image-duck}}
\end{minipage}
\begin{minipage}{\xTwo pt}
\subtop[duey]{\includegraphics[width=\xTwo pt]{example-image-duck}}
\subbottom[luey]{\includegraphics[width=\xTwo pt]{example-image-duck}}
\end{minipage}
\caption{Donalds nephews}
\label{fig:nephew}
\end{figure}

\clearpage
Assume now that we want to take into account the caption heights. For
simplicity, assume they are universal and denote them by $h$. More precisely, at
this level $h$ is a tuning parameter that can be adjusted to account for the
captions.  Then
$2h+y_2+y_3=2h+r_2x_2+r_3x_3=2h+x_2(r_2+r_3)\stackrel{!}{=}h+y_1=h+r_1x_1$
and thus
$x_1=\frac{(r_2+r_3)t+h}{r_1+r_2+r_3}\;.$

\pgfmathsetmacro{\rOne}{height("\includegraphics{example-image-duck}")/width("\includegraphics{example-image-duck}")}
\pgfmathsetmacro{\rTwo}{height("\includegraphics{example-image-duck}")/width("\includegraphics{example-image-duck}")}
\pgfmathsetmacro{\rThree}{height("\includegraphics{example-image-duck}")/width("\includegraphics{example-image-duck}")}
\pgfmathsetmacro{\hCaption}{17.5} %<-just a guess
\pgfmathsetmacro{\xOne}{(((\rOne+\rTwo)*0.95*\textwidth+\hCaption)/(\rOne+\rTwo+\rThree))}
\pgfmathsetmacro{\xTwo}{0.95*\textwidth-\xOne}

\begin{figure}
\centerfloat
\begin{minipage}{\xOne pt}
\subbottom[huey]{\includegraphics[width=\xOne pt]{example-image-duck}}
\end{minipage}
\begin{minipage}{\xTwo pt}
\subtop[duey]{\includegraphics[width=\xTwo pt]{example-image-duck}}
\subbottom[luey]{\includegraphics[width=\xTwo pt]{example-image-duck}}
\end{minipage}
\caption{Donalds nephews}
\label{fig:nephewtuned}
\end{figure}

\end{document}


I also added a way to take into account the captions (but this requires tuning since I do not know what \subbottom and \subtop precisely do, i.e. which dimensions they use. If this is known, the following discussion may allow one to compute h rather than guessing it.

\documentclass{memoir}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{duckuments}
\newsubfloat{figure}
\usepackage{showframe}
\begin{document}
\begin{figure}
\centerfloat
\begin{minipage}{8cm}
\strut\\
\subbottom[huey]{\includegraphics[width=\linewidth]{example-image-duck}}

• Sorry to ask but where does the length 8cm come from? What happens if the user uses a different page geometry and/or different figures with different aspect ratios? – user121799 May 9 at 19:07