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>}
:
\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:
\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}
Here is a sample code for your algorithm using the package algorithm2e
as suggested by your tags:
\documentclass[12pt]{article}
\usepackage[plainruled]{algorithm2e}
\usepackage{amsmath}
\begin{document}
\begin{algorithm}[ht!]
\KwIn{%
$A, B \text{ and } C$
}%
{\bf Initialization:} $\alpha, \beta \text{ and } u$ ;
\While{$|u^{k+1}-u^k | \geq \epsilon$}{%
compute $\alpha$ as $\hat{\alpha}$ with Eq. \eqref{eq:1};
compute $\beta$ as $\hat{\beta}$ with Eq. \eqref{eq:2};
compute $u$ as $\hat{u}$ with Eq. \eqref{eq:3}.
}
\KwOut{%
$\alpha \text{ and } \beta$
}%
\caption{Some algorithm}%
\label{algo:solution}%
\end{algorithm}
\end{document}
And here is what the output looks like:
There are many different options within algorithm2e
to configure the layout of the algorithm. You can for instance replace the option plainruled
by plain
in order to remove the horizontal lines. More information is available in the package documentation here. See section 7.3.
As for the numbering of the algorithm, if you completely remove the number, you will not be able to label it and reference it in the text. Have a look at section 9.2 and the command \SetAlgoRefName
in order to customize the referencing.
Best Answer
All you need is to create the block, which can be done using simply something like
to have ready to use arguments. Then, in your algorithm you would say, for example,
A complete example in which I also changed the fonts used to sans serif and the color of the rules so as to match the attached image for the desired result; of course, these changes are optional and can be safely removed.
The result: