29

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?

3
  • 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. Commented Apr 9, 2012 at 17:03
  • @Richard: Was working on fixing that but egreg was faster!! Commented Apr 9, 2012 at 17:09
  • But, alas, @PeterGrill, egreg's solution with line numbering on results in unnumbered lines dangling off the bottom of the algorithm. Commented Apr 9, 2012 at 17:12

3 Answers 3

25

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}​
2
  • This is so elegant! It would have saved me so much trouble. Thanks! Commented Apr 27, 2012 at 2:05
  • Definitely worth it if you declare your own commands. +1. Commented May 16, 2016 at 1:53
99

Using the noend option as in

\usepackage[noend]{algpseudocode}

yields

enter image description here

4
  • 8
    Why is this not upvoted more? This solution is definitely neater than the others... Commented Apr 30, 2013 at 20:44
  • 4
    Yes, this is the standard practice. Commented Oct 15, 2013 at 3:43
  • This gives me a compilation error: Option clash for package algpseudocode. and no more info. Commented Apr 28, 2015 at 17:50
  • 3
    is it possible to locally activate this option for some algorithm and not for others? Commented Jul 8, 2019 at 14:37
11

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

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.