1

How can I define a new loop in algorithmicx? For example, code like this:

ex1)

PreLoop s do

--statement1

--statement2

ex2)

PostLoop s do

--statement1

--statement2

Is it possible to define it as a command?

1

Yes, this can be done using the \algdef command. This is an example:

\documentclass{article}
\usepackage{algpseudocode}
\algdef{SE}{PreLoop}{EndPreLoop}[1]{\textbf{preloop} \(\mbox{#1}\) \textbf{do}}{\textbf{end}}%

\begin{document}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
\State $r\gets a\bmod b$
\PreLoop{$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$
\EndPreLoop\label{euclidendwhile}
\State \textbf{return} $b$\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}

\end{document}

For more information, see the algorithmicx manual, e.g., http://mirror.unl.edu/ctan/macros/latex/contrib/algorithmicx/algorithmicx.pdf .

| 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.