15

I want to do something like this (see image below) where I can describe my algorithm in smaller steps or "phases" as this person calls them. I've used the algorithm and algorithmic package before but I can't figure out how to do these "sub-algorithms" and i cant seem to find it in any tutorial. enter image description here

0

2 Answers 2

18

You can use the algpseudocode package and define a simple command to let you format the phases; a simple example:

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}

\newcommand\Algphase[1]{%
\vspace*{-.7\baselineskip}\Statex\hspace*{\dimexpr-\algorithmicindent-2pt\relax}\rule{\textwidth}{0.4pt}%
\Statex\hspace*{-\algorithmicindent}\textbf{#1}%
\vspace*{-.7\baselineskip}\Statex\hspace*{\dimexpr-\algorithmicindent-2pt\relax}\rule{\textwidth}{0.4pt}%
}
\begin{document}

\begin{algorithm}
\caption{Euclid’s algorithm}\label{euclid}
\begin{algorithmic}[1]
\Require something
\Ensure something
\Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
\State $r\gets a\bmod b$
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $b$\Comment{The gcd is b}
\EndProcedure%
\Algphase{Phase 1 - Gossip piece ids}
\Require something
\Function{somename}{someparams}
\While{$x>-5$}
\State $x\gets x-1$
\EndWhile
\EndFunction
\Algphase{Phase 2 - Require chunks}
\Ensure something
\Procedure{somename}{someparams}
\While{$x>-5$}
\State $x\gets x-1$
\EndWhile
\EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}

enter image description here

1
  • Hi, if we try it in two column format, the horizontal lines besides phase goes outside the margin. Any quick help? Commented Jul 12, 2023 at 14:32
2

The horizontal lines besides phase will be going outside the margin. To adapt it to the margin, just delete \rule{\textwidth}{0.4pt} in the command and put \hrule. The command then becomes:

\newcommand\Algphase[1]{%
\vspace*{-.7\baselineskip}\Statex\hspace*{\dimexpr-\algorithmicindent-2pt\relax}\hrule%
\Statex\hspace*{-\algorithmicindent}\textbf{#1}%
\vspace*{-.7\baselineskip}\Statex\hspace*{\dimexpr-\algorithmicindent-2pt\relax}\hrule%
}

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.