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?

Werner
  • 603,163
Richard
  • 5,723
  • 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 09 '12 at 17:03
  • Blank lines are unhappy, @PeterGrill, but thanks! – Richard Apr 09 '12 at 17:08
  • @Richard: Was working on fixing that but egreg was faster!! – Peter Grill Apr 09 '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 09 '12 at 17:12

3 Answers3

94

Using the noend option as in

\usepackage[noend]{algpseudocode}

yields

enter image description here

Thomas
  • 1,173
24

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}​
Werner
  • 603,163
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".

Richard
  • 5,723
egreg
  • 1,121,712