5

How do I rename the While loop using algorithm and algpseudocode?

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
 \begin{algorithm*}
  \caption{bar baz}
   \label{alg:interesting}
    \begin{algorithmic}[1]
     \While{foo bar}
     \EndWhile
    \end{algorithmic}
 \end{algorithm*}
\end{document}

It prints it out as

While foo bar do
end while

I would like to it come as

Until foo bar repeat

end while

How do I change only the first part of the while loop.

3

The original definition of the while block in algpseudocode.sty is

\algdef{SE}[WHILE]{While}{EndWhile}[1]{\algorithmicwhile\ #1\ \algorithmicdo}%
{\algorithmicend\ \algorithmicwhile}%

you can then replace \algorithmicwhile with \algorithmicuntil, and \algorithmicdo with \algorithmicrepeat; a complete example:

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}

\algdef{SE}[WHILE]{While}{EndWhile}[1]{\algorithmicuntil\ #1\ \algorithmicrepeat}{\algorithmicend\ \algorithmicwhile}%

\begin{document}
 \begin{algorithm*}
  \caption{bar baz}
   \label{alg:interesting}
    \begin{algorithmic}[1]
     \While{foo bar}
     \EndWhile
    \end{algorithmic}
 \end{algorithm*}
\end{document}

enter image description here

I only replaced the first occurrence of \algorithmicwhile as requested in the question; perhaps it would be better to change both.

Perhaps, as egreg has suggested in a comment, a better option would be to define a new Until block; here's how this could be done:

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}

\algdef{SE}[UNTIL]{Until}{EndUntil}[1]{\algorithmicuntil\ #1\ \algorithmicrepeat}{\algorithmicend\ \algorithmicuntil}%

\begin{document}
 \begin{algorithm*}
  \caption{bar baz}
   \label{alg:interesting}
    \begin{algorithmic}[1]
     \Until{foo bar}
     \EndUntil
    \end{algorithmic}
 \end{algorithm*}
\end{document}

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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