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.

I am writing a LaTeX document for some pseudo code I have written. I have a bunch of while loops, but the way latex is formatting them, it looks like:

while(x)
   while(y)
      while(z)

whereas I want it to do:

while(x)
   while(y)
while(z)

so I tried using \endwhile, and got the following error:

Undefined control sequence.
l.44    \ENDWHILE

I am using

\usepackage{algorithm}
\usepackage{program}

after using wikibooks - Algorithms & Pseudocode site, in which they use \endwhile, so I am wondering what I am doing wrong.

share|improve this question
    
Possibly related: Print programs with its proper syntax –  Werner Aug 24 '11 at 23:34
add comment

migrated from stackoverflow.com Aug 24 '11 at 23:28

This question came from our site for professional and enthusiast programmers.

2 Answers

up vote 1 down vote accepted

Uhm, you actually only need one of those packages. Pick either one, and it should work.

Example for algorithmic:

\documentclass{article}

\usepackage{algorithmic}

\begin{document}
  \begin{algorithmic}
  \WHILE{condition}
    \WHILE{condition}
      \STATE\COMMENT{do something}
    \ENDWHILE
  \ENDWHILE
  \WHILE{condition}
    \STATE\COMMENT{do something else}
  \ENDWHILE
  \end{algorithmic}
\end{document}

Example for program:

\documentclass{article}

\usepackage{program}

\begin{document}
  \begin{program} 
  \WHILE condition \DO 
    \WHILE condition \DO 
      \COMMENT{do something}
    \OD
  \OD
  \WHILE condition \DO 
    \COMMENT{do something else}
  \OD
  \end{program}
\end{document}
share|improve this answer
    
Ah, that uses algorithmic, not algorithm... Now it is working. I definately like algorithmic more than program, so thank you. I was going off of that website. –  Jim Aug 24 '11 at 20:47
    
Oops, my bad. I misread that in your post, sorry. algorithm only adds the float environments, so with algorithm and algorithmic you should be fine: \begin{algorithm}\begin{algorithmic}...\end{algorithmic}\end{algorithm} –  pulsar Aug 24 '11 at 20:58
    
wicked, thank you. –  Jim Aug 24 '11 at 20:59
add comment

In case anyone else runs into this problem, it turns out that you need to have while-do statements

while{x} \DO
 a,b,c = z
 while{z} \DO
  z = 1 \OD
\OD
while{y} \DO
 y += 1 \OD

That seemed to fix the indentation problem for me.

share|improve this answer
add comment

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.