22

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?

  • 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
21

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}​
|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 '16 at 1:53
74

Using the noend option as in

\usepackage[noend]{algpseudocode}

yields

enter image description here

|improve this answer|||||
  • 6
    Why is this not upvoted more? This solution is definitely neater than the others... – josch Apr 30 '13 at 20:44
  • 3
    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
  • 1
    is it possible to locally activate this option for some algorithm and not for others? – Simon C. Jul 8 '19 at 14:37
10

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

|improve this answer|||||
  • 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 '16 at 2:12
  • @logo_writer I don't remember; on the other hand, this appears to work – egreg May 16 '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 '16 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.