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.

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}
share|improve this question
    
Can you please add to your question a simple document with an algorithm showing in what cases you want the break to appear? You question is a little confusing for me (you mention the algorithmic package, tag with "algorithmicx", and it's not clear to what "LOOP" you are referring to). –  Gonzalo Medina Aug 20 '12 at 14:39
    
Someone edited my tags, that's why ... when people go trigger happy editing other people's questions this is what happens => lot of confusion :) –  Giovanni Azua Aug 20 '12 at 14:52
1  
Ah, I see now. Once you add the little example, I'll retag if that's OK with you. –  Gonzalo Medina Aug 20 '12 at 14:53

2 Answers 2

up vote 4 down vote accepted

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:

enter image description here

\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?

share|improve this answer

This is all much easier if you use the algoritmicx package, it has a lot more functionality and works better with other packages:

\documentclass[12pt]{report}
\usepackage[a4paper,tmargin=1.25in, bmargin=1.25in, lmargin=1.25in, rmargin=1.25in]{geometry}

\usepackage{algpseudocode}
\newcommand{\Break}{\State \textbf{break} }
\algblockdefx[Loop]{Loop}{EndLoop}[1][]{\textbf{Loop} #1}{\textbf{End Loop}}
\begin{document}

Here is an example:

\begin{algorithmic}[1]
\Require Integers $a >0$ and $b$
\Ensure $a\cdot b$
\State $i\leftarrow 0$
\State $c \leftarrow 0$
\Loop{1}
    \If{$i=a$}
        \Break Loop 1
    \EndIf
    \State $c \leftarrow c + b$
    \State $i \leftarrow i + 1$
\EndLoop
\State \textbf{return} $c$
\end{algorithmic}

\end{document}
share|improve this answer
    
thank you! good answer! –  Giovanni Azua Aug 21 '12 at 20:41
    
I didn't accept the answer because I would prefer not having to rewrite my algorithm using yet another package, I am maybe too conservative or lazy :) but thank you! –  Giovanni Azua Aug 22 '12 at 8:58
1  
@GiovanniAzua I made a similar decision a while ago, but then algorithmic is not compatible with cleveref and is not as flexible, so I eventually swapped. Rewriting is not necessary, you can just do a global search and replace for \STATE to \State, \REQUIRE to \Require etc. –  Karalga Aug 22 '12 at 11:46
    
@GiovanniAzua When you accept answers or not, please consider what will be most helpful for the thousands of other visitors to the discussion and how much time they will waste by being misled reading a suboptimal solution, instead of occasional inconvenience for your particular case. It is not mandatory to use a particular solution for your case in order to accept it. As long as you are confident it works (e.g., by trying the minimal example provided above), you can accept it if you think it is a better solution. –  johntex Jul 15 at 3:26

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.