algpseduocode
我有 2 个子算法需要使用包中的布局并排放置(2 列)algorithmicx
。由于我需要在以下文本段落中分别引用它们,同时还要有一个共同的标题,所以我选择了包subfigure
提供的环境subcaption
。
问题是,虽然我希望算法本身(内容)垂直居中对齐,但我需要它们各自的标题也对齐。但目前我不知道该怎么做。该图说明了当前的对齐方式和标题的要求位置。
这是MWE
制作这个
\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 background task \#2
\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 read sensor data from ADC
\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}
我怎样才能实现这个期望的位置?
答案1
\vspace{3.75\baselineskip}
为了简化解决方案,我建议通过在环境后添加类似的内容algorithmic
并使用[b]
ottom 对齐来将右侧算法提升到位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 background task \#2
\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 read sensor data from ADC
\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}
另一种方法(为了实现完美的垂直对齐)是将较大的内容放在一个可以测量的框中,然后将较小的框移动到差值的一半的位置。最终结果只是垂直对齐的微小改进。
这是替代方法 - 将每个算法存储在一个可以测量的盒子中。这样您就可以将较小的算法移动到位:
\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 background task \#2
\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 read sensor data from ADC
\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}