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.

algpseudocode lets me write code such as:

\documentclass{article}
\usepackage{algpseudocode}
\begin{document}

\begin{algorithmic}
\While{$n>3$}
   \If{$m>n$}
      \State ...
      \State ...
   \EndIf
   \If{$m>2$}
      \State ...
   \EndIf
\EndWhile
\end{algorithmic}

\end{document}

Yielding results similar to

while n>3
   if m>n
       ...
       ...
   end if
   if m>2
       ...
   end if
end while

I'd like all of that "end if", "end while", "end procedure" text to disappear and everything to compress upwards such that the result is:

while n>3
   if m>n
       ...
       ...
   if m>2
       ...

That is, I'd like a Pythonic-style where indentations indicate blocks.

Can algpseudocode do this? Or is there another package with similar functionality?

share|improve this question
    
Adding \algdef{SE}[WHILE]{While}{EndWhile}[1]{\algorithmicwhile\ #1\ \algorithmicdo}{}% \algdef{SE}[IF]{If}{EndIf}[1]{\algorithmicif\ #1\ \algorithmicthen}{}% will eliminate the text, but leaves a blank line. –  Peter Grill Apr 9 '12 at 17:03
    
Blank lines are unhappy, @PeterGrill, but thanks! –  Richard Apr 9 '12 at 17:08
    
@Richard: Was working on fixing that but egreg was faster!! –  Peter Grill Apr 9 '12 at 17:09
    
But, alas, @PeterGrill, egreg's solution with line numbering on results in unnumbered lines dangling off the bottom of the algorithm. –  Richard Apr 9 '12 at 17:12

3 Answers 3

up vote 11 down vote accepted

Instead of (re)defining the way \While and \If works, you can remove the "end line" text via

\algtext*{EndWhile}% Remove "end while" text
\algtext*{EndIf}% Remove "end if" text

Here's your MWE:

enter image description here

\documentclass{article}
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\algtext*{EndWhile}% Remove "end while" text
\algtext*{EndIf}% Remove "end if" text
\begin{document}

\begin{algorithmic}
\While{$n>3$}
   \If{$m>n$}
      \State ...
      \State ...
   \EndIf
   \If{$m>2$}
      \State ...
   \EndIf
\EndWhile
\end{algorithmic}

\end{document}​
share|improve this answer
    
This is so elegant! It would have saved me so much trouble. Thanks! –  Richard Apr 27 '12 at 2:05

Using the noend option as in

\usepackage[noend]{algpseudocode}

yields

enter image description here

share|improve this answer
3  
Why is this not upvoted more? This solution is definitely neater than the others... –  josch Apr 30 '13 at 20:44
    
Yes, this is the standard practice. –  hengxin Oct 15 '13 at 3:43

You can say

\algdef{SxnE}[WHILE]{While}{EndWhile}[1]{\algorithmicwhile\ #1\ \algorithmicdo}
\algdef{SxnE}[IF]{If}{EndIf}[1]{\algorithmicif\ #1\ \algorithmicthen}
\algdef{cxnE}{IF}{Else}{EndIf}

Look for the \algdef lines in algpseudocode.sty for other constructs to modify.

The flag SxnE means that there's a "start line", but no "end line".

share|improve this answer
    
Seems to work perfectly, thank you. –  Richard Apr 9 '12 at 18:23

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.