2

I have two sub-algorithms for side-by-side placement using the algpseudocode layout from the algorithmicx package. Since each sub-algorithm needs to be individually referred in text, I chose the subfigure environment provided by the subcaption package. Thanks to the community's help, I have made a good start.

However, the two sub-algorithms are typeset too closely to visually distinguish them. Furthermore, there is excessive white space to the right of the RHS algorithm .

The figure below describes the issue.

bad horizontal alignment of sub-algorithms

It is produced by the following MWE

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

The annotations describe a proposed fix, i.e. flush the respectively sub-algorithms within their minipages/parboxes. This did not work for me and even if it did, I am afraid this could be sub-optimal.

There could be other better fixes -- perhaps instead of hard-coding each subfigure to 0.48 of \textwidth , maybe it is possible to dynamically calculate the optimal container size of each sub-algorithm and place them flush with their respective margins.

Any help on how to achieve this is much appreciated.

  • Tried \raggedright and \raggedleft? – Andreas Storvik Strauman May 19 '18 at 19:23
  • @AndreasStorvikStrauman yes. Already tried. Does not seem to work – Krishna May 19 '18 at 19:24
3

You have two problems here:

  1. The right minipage is to wide for it's contents, leaving a lot of space on the right. This can be fixed by reducing the width of both, the minipage and the right subfigure.
  2. The \fbox adapts its width automatically to the contents. Thus, the \hfill between the subfigures doesn't do anything. Replacing \fbox by \framebox[\textwidth] fixes this.

The result:

enter image description here

The code:

\documentclass{article}

\usepackage{subcaption}
%\usepackage{algorithm} <-- doesn't exist
\usepackage{algorithmicx} % used this instead
\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.32\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}%
  }

  \framebox[\textwidth]{%
    \begin{subfigure}[b]{0.48\textwidth}
      \usebox{\algboxA}
      \caption{background processes}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.32\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.