I got stuck on designing easy-to-use command wrappers for multipanel floats (i.e. subfigures/subtables) built around the

In the MWE below I have omitted top-aligned panels for brevity.
The panel-environment evaluates its contents and dumps them into a macro \panelcontents. Although it does nothing with this value, it does evaluate the contents' height and stores it in \currentpanelheight---I imagine the final version will have to do it anyway, to decide which panel is the tallest automatically.
The problem disappears if I specify the tallest panel's height by hand (line 89 of the MWE). It is to have LaTeX determine this value automatically for each row that poses a problem. I considered a solution where each panel exports a value to an external file, and a separate macro selects the tallest panel on a subsequent run, rather than on-the-fly. There is probably a more elegant way to do it, but I am missing a piece of the puzzle. Can anyone offer any advice?
Code: Select all
\documentclass{book}
\usepackage{subcaption}
\usepackage[labelsep=space]{caption}
%Housekeeping code
\newlength\tallestpanelinrow
\newlength\currentpanelheight
\newlength\currentpanelwidth
\newif\ifcenterpanels
\newif\ifbottomalignpanels
\newcommand{\CenterPanels}{%
\centerpanelstrue
\bottomalignpanelsfalse
}%
\newcommand{\BottomAlignPanels}{%
\centerpanelsfalse
\bottomalignpanelstrue
}%
\BottomAlignPanels
%Row environments
\newenvironment{Bpanelrow}{\par\BottomAlignPanels}{}
%Centered panels dump contents in a savebox
%for future repositioning
\newsavebox{\rowpanelbox}
\newenvironment{Cpanelrow}{\par\CenterPanels%
\begin{lrbox}{\rowpanelbox}}%
{\end{lrbox}%
\usebox\rowpanelbox%
}
%The panel environment
\newsavebox{\panelbox}
\newenvironment{panel}[2][1.0]{%
%Housekeeping code
\setlength{\currentpanelwidth}{#1\textwidth}%
\def\panellabel{#2}%
%This code stores the panelcontents in the \panelbin savebox
\begin{lrbox}{\panelbox}}{\end{lrbox}%
\def\panelcontents{\usebox\panelbox}%
%The panel contents are now stored in \panelcontents
%
%Initialise \currentpanelheight
\settoheight{\currentpanelheight}{\panelcontents}
%
%First panel assumes it is the tallest, if no tallest panel has been defined
\ifdim\tallestpanelinrow<0pt%
\setlength{\tallestpanelinrow}{\currentpanelheight}
\fi
%
%Output the panel
\ifbottomalignpanels
\subcaptionbox{\strut\label{\panellabel}}%
[\currentpanelwidth]%
{\vbox to\tallestpanelinrow{\vfil\hbox{\panelcontents}}}%
\else\ifcenterpanels
\subcaptionbox{\strut\label{\panellabel}}%
[\currentpanelwidth]%
{\vbox to\tallestpanelinrow{\vfil\hbox{\panelcontents}\vfil}}%
\fi
\fi
}
%The table
\newenvironment{multipaneltable}[3][tbhp]{%
\begin{table}[#1]
\begin{center}%
\captionsetup{type=table,hypcap=false,position=bottom,subrefformat=parens}
\caption{#2\label{#3}}%
\setlength{\tallestpanelinrow}{-1pt}%To detect first panel
}%
{%
\end{center}%
\end{table}%
}
\begin{document}
Some text.
\begin{multipaneltable}[h]{Caption goes here.}{tablelabel}%
%
%Un-comment this line to make vert. alignment work.
%\setlength{\tallestpanelinrow}{8em}
%
\begin{Bpanelrow}
\begin{panel}[0.2]{panelone}
\rule{1em}{8em}
\end{panel}
\begin{panel}[0.2]{paneltwo}
\rule{1em}{1em}
\end{panel}
\begin{panel}[0.2]{panelthree}
\rule{1em}{3em}
\end{panel}
\end{Bpanelrow}
%
\begin{Cpanelrow}
\begin{panel}[0.2]{panelfour}
\rule{1em}{8em}
\end{panel}
\begin{panel}[0.2]{panelfive}
\rule{1em}{1em}
\end{panel}
\begin{panel}[0.2]{panelsix}
\rule{1em}{3em}
\end{panel}
\end{Cpanelrow}
%
\begin{Cpanelrow}
\begin{panel}[0.2]{panelfour}
\rule{1em}{1em}
\end{panel}
\begin{panel}[0.2]{panelfive}
\rule{1em}{3em}
\end{panel}
\begin{panel}[0.2]{panelsix}
\rule{1em}{8em}
\end{panel}
\end{Cpanelrow}
%
\end{multipaneltable}
Some text.
\end{document}