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.
[Tex/LaTex] How to typeset gotos and labels using LaTeX pseudocode environments
algorithm2ealgorithms
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
):\algorithmicgoto
provides the style, while\Goto{<label>}
is the actual command to be used inside thealgorithmic
environment. Here<label>
is a label defined on any line using\label{<label>}
:It is also possible combine the
\State
command in\Goto
using: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
(usingxspace
). Additionally there's a\Label
command defined. This issues a\State
command, followed by\unskip
which removed any horizontal skip issued internally byalgorithmicx
to indent the block by the appropriate amount. This allows you to typeset "labels" flush to the left margin in your own style: