I am trying to have two subfigures with significantly different aspect ratios have the same height by calculating the height for one of them then using that height for the other, following the method of this answer but the length gets set to zero between them. My understanding is that this is tied to the subfigure environment. Is there any way to make this work?
Two pictures for reference:
\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\newlength{\imageh}
\newlength{\imaged}
\newlength{\imagew}
\newcommand{\setimageh}[1]{
\settoheight{\imageh}{\usebox{#1}}
}
\newcommand{\setimagew}[1]{
\settowidth{\imagew}{\usebox{#1}}
}
\newcommand{\setimaged}[1]{
\settodepth{\imaged}{\usebox{#1}}
}
\makeatletter
\newcommand{\ImageDimensions}[2]{
% create and save the box
@ifundefined{Image}{\newsavebox{\Image}}{\usebox{\Image}}
\savebox{\Image}{\includegraphics[width=#1]{#2}}
\centering\usebox{\Image}
\setimageh{\Image}
\setimagew{\Image}
\setimaged{\Image}
}
\makeatother
\begin{document}
\begin{figure}
\centering
\begin{subfigure}[b]{0.45\linewidth}
\ImageDimensions{\linewidth}{smiley.png}
\caption{this picture works\height: \the\imageh}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.45\linewidth}
\includegraphics[height=\imageh]{smiley-narrow.png}
\caption{this picture doesnt show up\
height: \the\imageh}
\end{subfigure}
\end{figure}
\end{document}
There is a workaround, of computing the length and then setting it manually, but that requires compilation which takes time with many figures.
\begin{figure}
\centering
\begin{subfigure}[b]{0.45\linewidth}
\ImageDimensions{\linewidth}{smiley.png}
\caption{this picture works\\height: \the\imageh}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.45\linewidth}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% manual set of height referencing compiled output of height
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\includegraphics[height=156pt]{smiley-narrow.png}
\caption{this is a workaround\\
height set manually}
\end{subfigure}
\end{figure}



\global\imageh=\imageh(and similar) after set\settoheight...statements. – Rmano Nov 01 '23 at 16:42\newsaveboxshould be in the preamble, do not allocate registers in a local group, and do your measuring before the subfigure. Also note your definitions are adding a lot of space tokens, use%to hide the ends of lines – David Carlisle Nov 01 '23 at 17:00\setimagew{\Image}will add three space tokens so push the image off centre – David Carlisle Nov 01 '23 at 17:02