1

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:

smileynarrow image

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

Output: failed approach

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}
sandwich
  • 13
  • 3
  • 1
    Welcomed to TeX.SE! Somewhat related to this question: https://tex.stackexchange.com/questions/119730/global-scope-or-permanent-length-or-savebox – Jes Nov 01 '23 at 16:32
  • As said by Jes: try to add \global\imageh=\imageh (and similar) after set \settoheight... statements. – Rmano Nov 01 '23 at 16:42
  • exactly as in your last question \newsavebox should 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
  • This and text/image matching are addressed in https://tex.stackexchange.com/questions/640344/how-can-i-automatically-force-two-side-by-side-floats-of-any-kind-including-tex – John Kormylo Nov 01 '23 at 22:16

0 Answers0