Find the width of a figure in latex, to properbly align multiple minipages within a figure
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}}
\end{minipage}\quad
\begin{minipage}{\dimexpr\textwidth-8cm-1em}
\subtop[duey\strut]{\includegraphics[width=\linewidth]{example-image-duck}}
\subbottom[luey\strut]{\includegraphics[width=\linewidth]{example-image-duck}}
\end{minipage}
\caption{Donalds nephews}\label{fig:nephew}
\end{figure}
\end{document}