Subcaption: Vertical image alignment - Pass through image height

I have two external graphics that I want to add to my document side-by-side using the subcaption-package at the same height. To get the height of the first file, I use \settoheight. In the subfigure this is used in, the value is correctly set. However, when I leave the subfigure, the value seems to be set to zero. Why is that? How can I pass the image height from the left to the right subfigure?

MWE

\documentclass{article}

\usepackage{graphicx}
\usepackage{subcaption}

\newlength{\figwidth}
\newlength{\figheight}
\newlength{\figdim}

\begin{document}

\begin{figure}[htbp]
\begin{subfigure}{0.59\linewidth}
% Length
\setlength{\figwidth}{\linewidth}
\setlength{\figheight}{5cm}
\settoheight\figdim{\includegraphics[width=\figwidth,height=\figheight,keepaspectratio]{example-image-a}}
\setlength{\figheight}{\figdim}
% Figure
\centering
\includegraphics[width=\figwidth,height=\figheight,keepaspectratio]{example-image-a}
\caption{\the\figdim, \the\figheight, left}%
\label{fig:left}
\end{subfigure}%
\hfill
\begin{subfigure}{0.39\linewidth}
% Length
\setlength{\figwidth}{\linewidth}
% Figure
\centering
\includegraphics[width=\figwidth,height=\figheight,keepaspectratio]{example-image-b}
\caption{\the\figdim, \the\figheight, right}%
\label{fig:right}
\end{subfigure}%
\caption{Test}
\label{fig}
\end{figure}

\end{document}


Update

Following @Werner's approach:

\documentclass{article}

\usepackage{graphicx}
\usepackage{subcaption}

\newlength{\figwidth}
\newlength{\figheight}
\newlength{\figdim}

\begin{document}

\begin{figure}[htbp]

% Length
\setlength{\figwidth}{0.59\linewidth}
\setlength{\figheight}{5cm}
\settoheight\figdim{\includegraphics[width=\figwidth,height=\figheight,keepaspectratio]{example-image-a}}
\setlength{\figheight}{\figdim}

\begin{subfigure}{0.59\linewidth}
% Figure
\centering
\includegraphics[width=\figwidth,height=\figheight,keepaspectratio]{example-image-a}
\caption{\the\figheight, left}%
\label{fig2:left}
\end{subfigure}%
\hfill
\begin{subfigure}{0.39\linewidth}
% Figure
\centering
\includegraphics[width=\figwidth,height=\figheight,keepaspectratio]{example-image-b}
\caption{\the\figheight, right}%
\label{fig2:right}
\end{subfigure}%
\caption{Test}
\label{fig2}
\end{figure}

\end{document}


Unfortunately, one has to change the width factor (x*\linewidth) twice.

Update 2

\documentclass{article}

\usepackage{graphicx}
\usepackage{subcaption}

\newlength{\figwidth}
\newlength{\figheight}
\newlength{\figdim}

\begin{document}

\begin{figure}[htbp]
\begin{subfigure}{0.59\linewidth}
% Length
\setlength{\figwidth}{0.7\linewidth}
\setlength{\figheight}{5cm}
\settoheight\figdim{\includegraphics[width=\figwidth,height=\figheight,keepaspectratio]{example-image-16x9}}
\global\setlength{\figwidth}{\figwidth}
\global\setlength{\figheight}{\figdim}
% Figure
\centering
\includegraphics[width=\figwidth,height=\figheight,keepaspectratio]{example-image-16x9}
\caption{\the\figdim, \the\figheight, left}%
\label{fig3:left}
\end{subfigure}%
\hfill
\begin{subfigure}{0.39\linewidth}
% Length
\setlength{\figwidth}{\linewidth}
% Figure
\centering
\includegraphics[width=\figwidth,height=\figheight,keepaspectratio]{example-image-16x10}
\caption{\the\figdim, \the\figheight, right}%
\label{fig3:right}
\end{subfigure}%
\caption{Test}
\label{fig3}
\end{figure}

\end{document}


This is a result of scoping - length changes are reverted if they're made within a group once you exit that group. Here's a minimal example showing this:

\documentclass{article}

\newlength{\mylength}

\begin{document}

\setlength{\mylength}{100pt}
\the\mylength

\begingroup
\setlength{\mylength}{200pt}
\the\mylength
\endgroup

\the\mylength

\end{document}


Since you're capturing the image dimensions inside the subfigure environment, these lengths are lost as soon as you exit that environment.

The solution would be to gather than information within the figure environment, but outside the subfigure environment:

\documentclass{article}

\usepackage{graphicx}
\usepackage{subcaption}

\newlength{\figwidth}
\newlength{\figheight}
\newlength{\figdim}

\begin{document}

\begin{figure}[htbp]
% Length
\setlength{\figwidth}{\linewidth}
\setlength{\figheight}{5cm}
\settoheight\figdim{\includegraphics[width=\figwidth,height=\figheight,keepaspectratio]{example-image-a}}
\setlength{\figheight}{\figdim}
\begin{subfigure}{0.59\linewidth}
% Figure
\centering
\includegraphics[width=\figwidth,height=\figheight,keepaspectratio]{example-image-a}
\caption{\the\figdim, \the\figheight, left}%
\end{subfigure}%
\hfill
\begin{subfigure}{0.39\linewidth}
% Length
\setlength{\figwidth}{\linewidth}
% Figure
\centering
\includegraphics[width=\figwidth,height=\figheight,keepaspectratio]{example-image-b}
\caption{\the\figdim, \the\figheight, right}%
\end{subfigure}%
\caption{Test}
\end{figure}

\end{document}

• I see. This is problematic as the final dimensions of the left subfigure are dependent on linewidth, which changes in the subfigure. Thus I can only calculate the dimensions inside the subfigure. I could do that on the figure level, but I do have to include the factor of the subfigure width. – raedma Dec 15 '17 at 16:26
• @krtek: If you must, you can use \global\setlength{\figdim}{\figdim} to achieve a global definition for \figdim that will be retained after the scope ends. – Werner Dec 15 '17 at 16:35
• Thanks. Works great. I updated my questions with your solutions. – raedma Dec 15 '17 at 16:52