# Algorithmicx - caption alignment of 2-column sub-algorithms of different height

I have 2 sub-algorithms that need to be placed side-by-side (2 columns) using the algpseduocode layout from the algorithmicx package. Since I need to refer to each of them individually in the following text paragraphs whilst also having a common caption, I chose the subfigure environment provided by the subcaption package.

The problem is that, while I'd like the (content of the) algorithms themselves to be vertically aligned centrally, I need their individual captions to also line up. But presently I do not know how to do it. The figure illustrates the current alignment and the requirement position of the caption.

Here is the MWE to produce this

\documentclass{article}
\usepackage{subcaption}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage[margin=1cm]{geometry}

\begin{document}

\begin{figure}[h]
\centering
\fbox{
\begin{subfigure}[c]{0.50\textwidth}
\begin{algorithmic}[0]
\Procedure{Main}{}
\State configure interrupts
\State enable timers
\State $\vdots$
\While{\textproc{True}} \Comment{until shutdown}
\State background task \#1 \Comment{safety checks}
\State $\vdots$
\If{Calibrate}
\Function{doCalibration}{$x$}
\State subroutine for calibration
\State $\vdots$
\EndFunction
\EndIf
\State $\vdots$
\State background task \#$n$ \Comment{serial comm.}
\EndWhile
\EndProcedure
\end{algorithmic}
\caption{background processes}
\label{subfig:bgRTprocess}
\end{subfigure}
\hfill
\begin{subfigure}[c]{0.47\textwidth}
% \centering
\raggedright
\begin{algorithmic}[0]
\Function{ISR}{}
\State $\vdots$
\State initialise model
\Function{computeControl}{$x$}
\State evalute modeleqns
\State $\vdots$
\EndFunction
\State $\vdots$
\State write control output to DAC
\vfill
\EndFunction
\end{algorithmic}
\caption{foreground process}
\label{subfig:fgRTprocess}
\end{subfigure}
}
\caption{RT software architecture of a microcontroller}
\label{fig:basicRTCsoftwarearch}
\end{figure}

\end{document}


How can I achieve this desired placement?

For simplicity of a solution, I'd suggest raising the right-hand algorithm into position by adding something like \vspace{3.75\baselineskip} after the algorithmic environment and using a [b]ottom alignment for the subfigure:

\documentclass{article}

\usepackage{subcaption}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage[margin=1cm]{geometry}

\begin{document}

\begin{figure}
\centering
\fbox{
\begin{subfigure}[b]{0.50\textwidth}
\begin{algorithmic}[0]
\Procedure{Main}{}
\State configure interrupts
\State enable timers
\State $\vdots$
\While{\textproc{True}} \Comment{until shutdown}
\State background task \#1 \Comment{safety checks}
\State $\vdots$
\If{Calibrate}
\Function{doCalibration}{$x$}
\State subroutine for calibration
\State $\vdots$
\EndFunction
\EndIf
\State $\vdots$
\State background task \#$n$ \Comment{serial comm.}
\EndWhile
\EndProcedure
\end{algorithmic}
\caption{background processes}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.47\textwidth}
\begin{algorithmic}[0]
\Function{ISR}{}
\State $\vdots$
\State initialise model
\Function{computeControl}{$x$}
\State evalute modeleqns
\State $\vdots$
\EndFunction
\State $\vdots$
\State write control output to DAC
\EndFunction
\end{algorithmic}
\vspace{3.75\baselineskip}
\caption{foreground process}
\end{subfigure}
}
\caption{RT software architecture of a microcontroller}
\end{figure}

\end{document}


An alternative (for perfect vertical alignment) would be to place the larger content in a box that one can measure and then move the smaller box into position by half of the difference. The end result would be only a marginal improvement on the vertical alignment.

Here is the alternative method - storing each algorithm in a box that can be measured. That way you can move the smaller one into position:

\documentclass{article}

\usepackage{subcaption}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage[margin=1cm]{geometry}

\newsavebox{\algboxA}
\newsavebox{\algboxB}

\begin{document}

\begin{figure}

\savebox{\algboxA}{%
\begin{minipage}[b]{0.48\linewidth}
\begin{algorithmic}[0]
\Procedure{Main}{}
\State configure interrupts
\State enable timers
\State $\vdots$
\While{\textproc{True}} \Comment{until shutdown}
\State background task \#1 \Comment{safety checks}
\State $\vdots$
\If{Calibrate}
\Function{doCalibration}{$x$}
\State subroutine for calibration
\State $\vdots$
\EndFunction
\EndIf
\State $\vdots$
\State background task \#$n$ \Comment{serial comm.}
\EndWhile
\EndProcedure
\end{algorithmic}
\end{minipage}%
}%
\savebox{\algboxB}{%
\begin{minipage}[b]{0.48\linewidth}
\begin{algorithmic}[0]
\Function{ISR}{}
\State $\vdots$
\State initialise model
\Function{computeControl}{$x$}
\State evalute modeleqns
\State $\vdots$
\EndFunction
\State $\vdots$
\State write control output to DAC
\EndFunction
\end{algorithmic}
\end{minipage}%
}

\fbox{%
\begin{subfigure}[b]{0.48\textwidth}
\usebox{\algboxA}
\caption{background processes}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.48\textwidth}
\raisebox{\dimexpr.5\ht\algboxA-.5\ht\algboxB}{%
\usebox{\algboxB}%
}
\caption{foreground process}
\end{subfigure}
}
\caption{RT software architecture of a microcontroller}
\end{figure}

\end{document}

• I do not want to hand-compute the lengths such as 3.75 to achieve vertical centering. I need to typeset more specific statements within the procedure and functions that any manually set-up height shall immediately change. Is there a way for latex to calculate the required height for centered vertical alignment automatically. I am using LuaTex. Does having lua interpreter help in any way for such computations? – Krishna May 19 '18 at 15:11
• @Krishna: I've added an update to the answer: Storing the algorithms in boxes which can be measured. That way you can move them around as needed. – Werner May 19 '18 at 15:33