GeneralErrors with algorithm package: Undefined control sequence

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
danylo
Posts: 5
Joined: Thu Aug 09, 2007 5:41 pm

Errors with algorithm package: Undefined control sequence

Post by danylo »

Hi,
this has been driving me nuts on how come its not working:

Code: Select all

\usepackage{algorithm}
\usepackage{algpseudocode}

...

\begin{algorithm}{}
\caption{$QT\_Clust(S,d)$}
\label{alg:qt}
\begin{algorithmic}[1]
\IF{$N$ is even}
\STATE $X \Leftarrow X \times X$
\STATE $N \Leftarrow N / 2$
\ELSE{$N$ is odd}
\STATE $y \Leftarrow y \times X$
\STATE $N \Leftarrow N - 1$
\ENDIF
    \State return ...
\end{algorithmic}
\end{algorithm}
for some reason I every line I get an error: Undefined control sequence: \IF (or \STATE or \ELSE). What is the problem with my syntax? Also if I want to reference this algorithm later on how would I do it?
Thanks.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Errors with algorithm package: Undefined control sequence

Post by gmedina »

Beware of small caps. Try this:

Code: Select all

\begin{algorithm}{}\label{alg:qt}
 \caption{$QT\_Clust(S,d)$}
 \begin{algorithmic}
  \If {$N$ is even}
  \State $X \Leftarrow X \times X$
  \State $N \Leftarrow N / 2$
  \Else {$N$ is odd}
  \State $y \Leftarrow y \times X$
  \State $N \Leftarrow N - 1$
  \EndIf
  \State return ...
 \end{algorithmic}
\end{algorithm}
Remark: the line \Else {$N$ is odd} seems odd to me.
Since you have already defined a \label for your algorithm, you can reference it like this:

Code: Select all

As algorithm \ref{alg:qt} shows ...
1,1,2,3,5,8,13,21,34,55,89,144,233,...
danylo
Posts: 5
Joined: Thu Aug 09, 2007 5:41 pm

Errors with algorithm package: Undefined control sequence

Post by danylo »

Hi,
thanks for the \ref comment, that works now.
I removed the \Else line but I still get the same error for the other lines.
For some reason though if I change the inside of \begin{algorithmic} to this:

Code: Select all

    \For{all ...}
        \State do ...
    \EndFor
    \State return ...
I get no errors.
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Errors with algorithm package: Undefined control sequence

Post by gmedina »

danylo wrote: I removed the \Else line...


My remark addressed the "meaning" of the command inside the whole algorithm (you don't have to remove it, just review your algorithm).
danylo wrote: For some reason though if I change the inside of \begin{algorithmic} to this:
\For{all ...}
\State do ...
\EndFor
\State return

As I said in my reply, beware of capitalization; LaTeX commands are sensible to capitalization. The code I posted works perfectly (in a "formal" sense; i.e., it's syntactically correct, but the semantics is up to you).
1,1,2,3,5,8,13,21,34,55,89,144,233,...
danylo
Posts: 5
Joined: Thu Aug 09, 2007 5:41 pm

Re: Algorithm class

Post by danylo »

It works now. I didn't realize that the commands were case sensitive. Thanks. One of other thing I'm curious about is that in the \Comment command is there a way to change the way the comment is produced? Right now a little arrow is used to show the start of the comment. I would prefer something like // instead.
One more thing - how do you force a line break in the middle of a command?
Thanks.
danylo
Posts: 5
Joined: Thu Aug 09, 2007 5:41 pm

Re: Algorithm class

Post by danylo »

Sorry, I have one more question...
I start off my pseudo code with:
\begin{algorithm}{}
\label{alg:qt}
\caption{$QT\_Clust(S,d)$}

Then later in my text I do \ref{alg:qt} later in my text but it returns the section number the pseudo code is in and no information about the actual algorithm. How do I change this?
Thanks.
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Errors with algorithm package: Undefined control sequence

Post by gmedina »

danylo wrote: I'm curious about is that in the \Comment command is there a way to change the way the comment is produced? Right now a little arrow is used to show the start of the comment. I would prefer something like // instead.

Add this in the preamble:

Code: Select all

\renewcommand{\algorithmiccomment}[1]{// #1}
danylo wrote: One more thing - how do you force a line break in the middle of a command?

Please, be more explicit.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Errors with algorithm package: Undefined control sequence

Post by gmedina »

danylo wrote: Then later in my text I do \ref{alg:qt} later in my text but it returns the section number the pseudo code is in and no information about the actual algorithm. How do I change this?

Try this:

Code: Select all

\begin{algorithm}
\caption{QT\_Clust(S,d)}\label{alg:qt}
\begin{algorithmic}
\If {$N$ is even}
\State $X \Leftarrow X \times X$ 
\State $N \Leftarrow N / 2$
\Else {$N$ is odd}
\State $y \Leftarrow y \times X$
\State $N \Leftarrow N - 1$
\EndIf
\State return ...
\end{algorithmic}
\end{algorithm}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
User avatar
Sebi
Posts: 20
Joined: Sat Jan 20, 2007 11:18 pm

Re: Algorithm class

Post by Sebi »

You can even label each line of the algorithm, in order to reference to it.
Very handy if the algorithm is changed later on!
Post Reply