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.

How can I get rid of the do in a for loop and the then in an if statement when typesetting algorithms using the algorithm and algorithmicx packages?

share|improve this question
1  
I am confused. Are you talking about some structures for writing algorithms implemented by some package? if so, what package are you using? Or are you referring to cycles and conditionals in (La)TeX? Please add to your question a minimal and complete version of code showing the involved structures and packages used. –  Gonzalo Medina Apr 18 '12 at 1:32
    
The former, I'm using algorithm and algorithmic. –  Andrew Latham Apr 18 '12 at 1:33
2  
Please edit the question- after I read the answers, your question sort-of made sense, but still not very much. –  cmhughes Apr 18 '12 at 9:25
    
@cmhughes I think this question makes sense now (in fact, it just helped me!). Perhaps a few words should be added to the question to indicate that it is about the algorithmicx package. –  void-pointer Oct 2 '13 at 7:01
add comment

2 Answers

up vote 7 down vote accepted

You can redefine \algorithmicthen and \algorithmicdo:

\renewcommand\algorithmicthen{}
\renewcommand\algorithmicdo{}

Adding the previous lines in the preamble (respectively, before a particular algorithmic environment enclosing the construct inside a group) will remove the "then" and "do" from all the algorithmic environments (for the particular environment, respectively). If the change has to be applied only to some structures of a particular algorithm while others still will have the "then" and "do", then you can define commands to "switch on/off" the expressions:

\documentclass{article}
\usepackage{algorithm}
\usepackage{algorithmic}

\newcommand\NoDo{\renewcommand\algorithmicdo{}}
\newcommand\ReDo{\renewcommand\algorithmicdo{\textbf{do}}}
\newcommand\NoThen{\renewcommand\algorithmicthen{}}
\newcommand\ReThen{\renewcommand\algorithmicthen{\textbf{then}}}

\begin{document}

\begin{algorithm}
\caption{Calculate $y = x^n$}
\label{alg1}
\begin{algorithmic}
\STATE $y \leftarrow 1$
\NoThen
\IF{$n < 0$}
\STATE $X \leftarrow 1 / x$
\ENDIF
\ReThen
\IF{$n > 0$}
\STATE $X \leftarrow 1$
\ENDIF
\NoDo
\FOR{$N \neq 0$}
\STATE $X \leftarrow X \times X$
\ENDFOR
\ReDo
\WHILE{$N \neq 0$}
\STATE $X \leftarrow X \times X$
\ENDWHILE
\end{algorithmic}
\end{algorithm}

\end{document}

enter image description here

share|improve this answer
add comment

Although this may be up to user preference, the algorithmicx package provides a similar interface to that of algorithmic but with some more (or easy) customization.

Here is an implementation of @Gonzalo's answer that defines \NoThenIf ... \EndIf and \NoDoFor ... \EndFor pairs using "the ONE defining macro \algdef:

enter image description here

\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\begin{document}

\algdef{SE}[FOR]{NoDoFor}{EndFor}[1]{\algorithmicfor\ #1}{\algorithmicend\ \algorithmicfor}%
\algdef{SE}[IF]{NoThenIf}{EndIf}[1]{\algorithmicif\ #1}{\algorithmicend\ \algorithmicif}%
\begin{algorithm}
  \caption{Calculate $y = x^n$}\label{alg1}
  \begin{algorithmic}[1]
    \State $y \leftarrow 1$
    \NoThenIf{$n < 0$}
      \State $X \leftarrow 1 / x$
    \EndIf
    \If{$n > 0$}
      \State $X \leftarrow 1$
    \EndIf
    \NoDoFor{$N \neq 0$}
      \State $X \leftarrow X \times X$
    \EndFor
    \While{$N \neq 0$}
      \State $X \leftarrow X \times X$
    \EndWhile
  \end{algorithmic}
\end{algorithm}
\end{document}

The flags SE in both definitions refer to "Starting command with text" and "Continuing command, with default Ending text". See the algorithmicx documentation for more information on these command definitions (section 4.7 The ONE defining macro, p 20 onward).

share|improve this answer
add comment

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.