[Tex/LaTex] How to typeset gotos and labels using LaTeX pseudocode environments

algorithm2ealgorithms

Is there any way to typeset gotos and labels using LaTeX pseudocode environments (algorithmic, pseudocode, clrscode, algorithm2e, etc.)? Automatic and correct indentation of labels would also be highly appreciated.

Best Answer

You could use the line numbers to act as your labels, and reference them in a "go to" command.

In the following minimal example, I've defined a "go to" command using the following (I'm using algorithmicx):

\algnewcommand{\algorithmicgoto}{\textbf{go to}}%
\algnewcommand{\Goto}[1]{\algorithmicgoto~\ref{#1}}%

\algorithmicgoto provides the style, while \Goto{<label>} is the actual command to be used inside the algorithmic environment. Here <label> is a label defined on any line using \label{<label>}:

enter image description here

\documentclass{article}
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\begin{document}
\algnewcommand{\algorithmicgoto}{\textbf{go to}}%
\algnewcommand{\Goto}[1]{\algorithmicgoto~\ref{#1}}%
\begin{algorithm}
  \caption{Euclid’s algorithm}\label{euclid}
  \begin{algorithmic}[1]
    \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} \label{marker}
      \State $a\gets b$
      \State $b\gets r$
      \State $r\gets a\bmod b$
    \EndWhile
    \State \textbf{return} $b$\Comment{The gcd is b}
    \State \Goto{marker}
  \EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}

It is also possible combine the \State command in \Goto using:

\algnewcommand{\Goto}[1]{\State \algorithmicgoto~\ref{#1}}

However, this would make it impossible to use \Goto with anything else on a line, since it issues a \State, which starts a new line in the algorithm.


Here is another approach, still using algorithmicx. This time I've used a slightly different version of \Goto (using xspace). Additionally there's a \Label command defined. This issues a \State command, followed by \unskip which removed any horizontal skip issued internally by algorithmicx to indent the block by the appropriate amount. This allows you to typeset "labels" flush to the left margin in your own style:

enter image description here

\documentclass{article}
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\usepackage{xspace}% http://ctan.org/pkg/xspace
\begin{document}
\algnewcommand{\algorithmicgoto}{\textbf{go to}}%
\algnewcommand{\Goto}{\algorithmicgoto\xspace}%
\algnewcommand{\Label}{\State\unskip}
\begin{algorithm}
  \caption{Euclid’s algorithm}\label{euclid}
  \begin{algorithmic}
    \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
    \State $r\gets a\bmod b$
    \Label \texttt{marker:}
    \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
    \State \textbf{return} $b$\Comment{The gcd is b}
    \State \Goto \texttt{marker}
  \EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}
Related Question