1

I am trying to write an algorithm which contains nested for loops as follows:

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\title{Algorithm example}

\author{\LaTeX{}}

\date{\today}

\begin{document}
\maketitle

\section{Gibbs sampling Algorithm}

Algorithms can be included using the commands as shown in algorithm \ref{alg:gibbs}.

\begin{algorithm}
\caption{Collapsed Gibbs sampling algorithm for inference}\label{alg:gibbs}
\begin{algorithmic}[1]
\State Choose initial $z$ and $\xi$.
\For{$T$ iterations}
  \For{$D$ Subjects}
    \For{$N_d$ time segments}
      \State do something
    \EndFor
  \EndFor
\EndFor
\end{algorithmic}
\end{algorithm}

\end{document}

This is the result: enter image description here

... and I get following errors:

Undefined control sequence
Package algorithmicx: Some blocks are not closed!!!

I am using IEEE's online template and also checked this question about nested for loops with algorithm, with no luck.


EDIT 1:

Changed \Endfor to \EndFor. The errors disappeared but, gives me same output.

  • Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – dexteritas Jun 29 '17 at 15:28
  • @dexteritas, Agreed! Made requisite changes. – kedarps Jun 29 '17 at 15:39
  • Do you want another output or is the Question answered? – dexteritas Jun 29 '17 at 15:47
  • Although I am not seeing errors anymore, I still get the same output i.e. I don't see an end for line to terminate the for loop. – kedarps Jun 29 '17 at 15:50
  • 1
    Than you have to remove the noend at \usepackage[noend]{algpseudocode}. – dexteritas Jun 29 '17 at 15:51
4

I guess that you are compiling your document in "batch mode", which does not stop at errors but tries to continue, skipping problems.

In your case, the complete error message says:

! Undefined control sequence.
l.16 \Endfor

So the problem is that the command \Endfor is not recognized, but tex tries to continue nevertheless, and later produces another arror about blocks not being closed.

The problem is solved by writing \EndFor (which is the correct spelling of this command) instead of \Endfor.

If you want to see the "end for" in the output, remove the noend option in the package ;-)

| improve this answer | |
  • I made that change and I no longer see the errors. However, I am still getting same output. – kedarps Jun 29 '17 at 15:40
  • @kedarps But the output is correct, isn't? What did you expect? – JLDiaz Jun 29 '17 at 15:51
  • @kedarps Oh, I saw your other comment under the question. Updated answer – JLDiaz Jun 29 '17 at 15:55

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.