Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
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. –  Mikael Öhman Nov 6 '11 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 '12 at 19:56

2 Answers 2

up vote 7 down vote accepted

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}
share|improve this answer
    
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. –  Egor Tensin Nov 6 '11 at 3:02

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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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