11

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.

2
  • But gotos aren't indented, and if you are using them in such a consistent way, you could replace them with an equivalent while-loop easily. Nov 6, 2011 at 0:08
  • Doesn't listings have a layout tool for this? Some of the 'howto' examples are in psudocode, aren't they?
    – Canageek
    Feb 23, 2012 at 19:56

2 Answers 2

11

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}
1
  • Thank you for your answer! One can also use \algnewcommand\Label[1]{\item[#1:]}. The second approach you've described seems to me to be more idiomatically correct, though. Nov 6, 2011 at 3:02
7

When using algorithm2e I used code line the one bellow for Goto

\begin{algorithm}[!ht]
    \small
    \SetAlgoNoLine
    \LinesNumbered
    \SetKw{KwGoTo}{go to}

    \Begin{
        \For{$i = 1$ \KwTo $L$}{\label{outer_loop}
            \For{$k = 1$ \KwTo $K$} {
                \If{$k = 10$}{
                    \KwGoTo \ref{outer_loop}\;
                 }
            }
        }
    }
\end{algorithm}
1
  • When the algorithm has a caption and I use the label I get the following error:! Paragraph ended before \caption@@xlabel was complete.
    – Vasilis
    Jan 19, 2017 at 7:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .