# [Tex/LaTex] customizing the algorithmic package: break and loop labels

algorithmicxalgorithms

I would like to add two customizations to the algorithmic package: introduce a new break command and have the possibility to provide labels to the LOOP so that break {label} is clear to which loop is referring to. I tried to introduce the break command like this:

\newcommand{\algorithmicbreak}{\textbf{break}}


but this won't work, I get the unknown command error when I type \BREAK

However, with this hack I can make it work but it is very unnice:

\renewcommand{\algorithmicprint}{\textbf{break}}
% then use it as
\PRINT


The LOOP labels I have no idea how to introduce.

UPDATE: my full example

    \begin{algorithm}
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}
\renewcommand{\algorithmicprint}{\textbf{break}}
\caption{\text{My-Algorithm}}
\label{algo:blah}
\begin{algorithmic}[1]
\REQUIRE X
\ENSURE Y
% STEP 1
\STATE \COMMENT{{\bf Step 1}} some something
\LOOP % can't annotate the loop :(((
% STEP 2
\STATE \COMMENT{{\bf Step 2}} do something more
\IF {$a = b$}
\RETURN $c$
\ELSE
\LOOP % can't annotate the loop :(((
% STEP 3
\STATE \COMMENT{{\bf Step 3}} do some more
\IF {$b = c$}
\STATE $y = x$
\PRINT % intending to break
\ELSE
% STEP 4
\STATE \COMMENT{{\bf Step 4}} and yet some more
\ENDIF
\ENDLOOP
\ENDIF
\ENDLOOP
\end{algorithmic}
\end{algorithm}


Here is a modification added to algorithmic that allows you to \BREAK and have an annotated loop \ALOOP (and \ENDALOOP). It is based on a copy of the regular \LOOP and \ENDLOOP combinations, with an added parameter for annotation. Of course, more modifications are possible:

\documentclass{article}
\usepackage{algorithm,algorithmic}% http://ctan.org/pkg/algorithms

% Algorithmic modifications
\makeatletter
\newcommand{\ALOOP}[1]{\ALC@it\algorithmicloop\ #1%
\begin{ALC@loop}}
\newcommand{\ENDALOOP}{\end{ALC@loop}\ALC@it\algorithmicendloop}
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}
\newcommand{\algorithmicbreak}{\textbf{break}}
\newcommand{\BREAK}{\STATE \algorithmicbreak}
\makeatother

\begin{document}
\begin{algorithm}
\caption{My-Algorithm}\label{algo:blah}
\begin{algorithmic}[1]
\show\LOOP
\REQUIRE X
\ENSURE Y
% STEP 1
\STATE \COMMENT{\textbf{Step 1}} some something
\ALOOP {outer} % Outer loop
% STEP 2
\STATE \COMMENT{\textbf{Step 2}} do something more
\IF {$a = b$}
\RETURN $c$
\ELSE
\ALOOP {inner} % Inner loop
% STEP 3
\STATE \COMMENT{\textbf{Step 3}} do some more
\IF {$b = c$}
\STATE $y = x$
\BREAK % intending to break
\ELSE
% STEP 4
\STATE \COMMENT{\textbf{Step 4}} and yet some more
\ENDIF
\ENDALOOP
\ENDIF
\ENDALOOP
\end{algorithmic}
\end{algorithm}
\end{document}​


It would also be possible to modify \BREAK to take an argument if you want to annotate that as well (like breaking out of a specific loop, say).

For completeness, I've fixed your use of \bf. As such, see Does it matter if I use \textit or \it, \bfseries or \bf, etc. and Will two-letter font style commands (\bf, \it, …) ever be resurrected in LaTeX?