TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 18 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
    
Definitely worth it if you declare your own commands. +1. – logo_writer May 16 at 1:53

Using the noend option as in

\usepackage[noend]{algpseudocode}

yields

enter image description here

share|improve this answer
4  
Why is this not upvoted more? This solution is definitely neater than the others... – josch Apr 30 '13 at 20:44
2  
Yes, this is the standard practice. – hengxin Oct 15 '13 at 3:43
    
This gives me a compilation error: Option clash for package algpseudocode. and no more info. – Javi V Apr 28 '15 at 17:50

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
    
Isn't it SxNE? Because in the docs, xN means "ending command, with no text for this block" and there is no xn. – logo_writer May 16 at 2:12
    
@logo_writer I don't remember; on the other hand, this appears to work – egreg May 16 at 7:44
    
@egreg I can confirm that SxNE works too! And indeed, SxnE also. I guess it is an undocumented feature. – logo_writer May 17 at 6:33

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.