[Tex/LaTex] Error compiling algpseudocode example

algorithmicxerrors

Compiling this piece of code (which I took from the guide of the algorithmicx package) I get a lot of errors and do not understand why. I think it is probably some stupid typo I can't see but this is driving me mad..

Here is the code

\documentclass{article}
\usepackage{algpseudocode}

\begin{document}

\algnewcommand\algorithmicto{\textbf{to}}
\algrenewtext{For}[3]%
   {\algorithmicfor\ #1 \gets #2 \algorithmicto\ #3 \algorithmicdo}
\begin{algorithmic}[1]
\State $p \gets 1$
\For{i}{1}{n}
     \State $p \gets p * i$
\EndFor
\end{algorithmic}

\end{document}

And here is the log:

LOG FILE :
This is pdfTeX, Version 3.1415926-2.3-1.40.12 (MiKTeX 2.9 64-bit) (preloaded format=pdflatex 2012.10.21) 16 DEC 2012 21:46
entering extended mode
**test2.tex
(C:\Users\Antonio\Dropbox\AlgoComplessità\2-StringMatching\test\test2.tex
LaTeX2e <2011/06/27>
Babel <v3.8m> and hyphenation patterns for english, afrikaans, ancientgreek, ar
abic, armenian, assamese, basque, bengali, bokmal, bulgarian, catalan, coptic,
croatian, czech, danish, dutch, esperanto, estonian, farsi, finnish, french, ga
lician, german, german-x-2009-06-19, greek, gujarati, hindi, hungarian, iceland
ic, indonesian, interlingua, irish, italian, kannada, kurmanji, lao, latin, lat
vian, lithuanian, malayalam, marathi, mongolian, mongolianlmc, monogreek, ngerm
an, ngerman-x-2009-06-19, nynorsk, oriya, panjabi, pinyin, polish, portuguese,
romanian, russian, sanskrit, serbian, slovak, slovenian, spanish, swedish, swis
sgerman, tamil, telugu, turkish, turkmen, ukenglish, ukrainian, uppersorbian, u
senglishmax, welsh, loaded.
("C:\Program Files\MiKTeX 2.9\tex\latex\base\article.cls"
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
("C:\Program Files\MiKTeX 2.9\tex\latex\base\size10.clo"
File: size10.clo 2007/10/19 v1.4h Standard LaTeX file (size option)
)
\c@part=\count79
\c@section=\count80
\c@subsection=\count81
\c@subsubsection=\count82
\c@paragraph=\count83
\c@subparagraph=\count84
\c@figure=\count85
\c@table=\count86
\abovecaptionskip=\skip41
\belowcaptionskip=\skip42
\bibindent=\dimen102
)
(C:\Users\Antonio\AppData\Roaming\MiKTeX\2.9\tex\latex\algorithmicx\algpseudoco
de.sty
Package: algpseudocode
("C:\Program Files\MiKTeX 2.9\tex\latex\base\ifthen.sty"
Package: ifthen 2001/05/26 v1.1c Standard LaTeX ifthen package (DPC)
)
(C:\Users\Antonio\AppData\Roaming\MiKTeX\2.9\tex\latex\algorithmicx\algorithmic
x.sty
Package: algorithmicx 2005/04/27 v1.2 Algorithmicx
Document Style algorithmicx 1.2 - a greatly improved `algorithmic' style
\c@ALG@line=\count87
\c@ALG@rem=\count88
\c@ALG@nested=\count89
\ALG@tlm=\skip43
\ALG@thistlm=\skip44
\c@ALG@Lnr=\count90
\c@ALG@blocknr=\count91
\c@ALG@storecount=\count92
\c@ALG@tmpcounter=\count93
\ALG@tmplength=\skip45
)
Document Style - pseudocode environments for use with the `algorithmicx' style
) (C:\Users\Antonio\Dropbox\AlgoComplessità\2-StringMatching\test\test2.aux)
LaTeX Font Info: Checking defaults for OML/cmm/m/it on input line 4.
LaTeX Font Info: ... okay on input line 4.
LaTeX Font Info: Checking defaults for T1/cmr/m/n on input line 4.
LaTeX Font Info: ... okay on input line 4.
LaTeX Font Info: Checking defaults for OT1/cmr/m/n on input line 4.
LaTeX Font Info: ... okay on input line 4.
LaTeX Font Info: Checking defaults for OMS/cmsy/m/n on input line 4.
LaTeX Font Info: ... okay on input line 4.
LaTeX Font Info: Checking defaults for OMX/cmex/m/n on input line 4.
LaTeX Font Info: ... okay on input line 4.
LaTeX Font Info: Checking defaults for U/cmr/m/n on input line 4.
LaTeX Font Info: ... okay on input line 4.
LaTeX Font Info: External font `cmex10' loaded for size
(Font) <7> on input line 9.
LaTeX Font Info: External font `cmex10' loaded for size
(Font) <5> on input line 9.
! Missing $ inserted.
<inserted text>
$
l.10 \For{i}{1}{n}
I've inserted a begin-math/end-math symbol since I think
you left one out. Proceed, with fingers crossed.
! LaTeX Error: Command \item invalid in math mode.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.11 \State
$p \gets p * i$
Try typing <return> to proceed.
If that doesn't work, type X <return> to quit.
! LaTeX Error: Something's wrong--perhaps a missing \item.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.11 \State
$p \gets p * i$
Try typing <return> to proceed.
If that doesn't work, type X <return> to quit.
! LaTeX Error: Something's wrong--perhaps a missing \item.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.11 \State
$p \gets p * i$
Try typing <return> to proceed.
If that doesn't work, type X <return> to quit.
! Missing $ inserted.
<inserted text>
$
l.11 \State $p \gets
p * i$
I've inserted a begin-math/end-math symbol since I think
you left one out. Proceed, with fingers crossed.
! Missing \endcsname inserted.
<to be read again>
\ALG@currentblock@2
l.12 \EndFor
The control sequence marked <to be read again> should
not appear between \csname and \endcsname.
! Extra \endcsname.
\ALG@makenobeginrepeat ...LG@thisblock \endcsname
\relax \def \ALG@thisblock...
l.12 \EndFor
I'm ignoring this, since I wasn't doing a \csname.
! Missing number, treated as zero.
<to be read again>
\ALG@b@2@EndFor@0
l.12 \EndFor
A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)
! Missing number, treated as zero.
<to be read again>
\ALG@currentblock@2
l.13 \end{algorithmic}
A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)
! Missing = inserted for \ifnum.
<to be read again>
\ALG@currentblock@2
l.13 \end{algorithmic}
I was expecting to see `<', `=', or `>'. Didn't.
! Missing number, treated as zero.
<to be read again>
\ALG@currentblock@2
l.13 \end{algorithmic}
A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)
[1
{C:/Users/Antonio/AppData/Local/MiKTeX/2.9/pdftex/config/pdftex.map}]
(C:\Users\Antonio\Dropbox\AlgoComplessità\2-StringMatching\test\test2.aux) )
Here is how much of TeX's memory you used:
727 strings out of 494045
10030 string characters out of 3148380
55788 words of memory out of 3000000
4092 multiletter control sequences out of 15000+200000
4247 words of font info for 16 fonts, out of 3000000 for 9000
715 hyphenation exceptions out of 8191
27i,4n,17p,252b,184s stack positions out of 5000i,500n,10000p,200000b,50000s
<C
:/Program Files/MiKTeX 2.9/fonts/type1/public/amsfonts/cm/cmbx10.pfb><C:/Progra
m Files/MiKTeX 2.9/fonts/type1/public/amsfonts/cm/cmmi10.pfb><C:/Program Files/
MiKTeX 2.9/fonts/type1/public/amsfonts/cm/cmr10.pfb><C:/Program Files/MiKTeX 2.
9/fonts/type1/public/amsfonts/cm/cmr8.pfb><C:/Program Files/MiKTeX 2.9/fonts/ty
pe1/public/amsfonts/cm/cmsy10.pfb>
Output written on test2.pdf (1 page, 45710 bytes).
PDF statistics:
26 PDF objects out of 1000 (max. 8388607)
0 named destinations out of 1000 (max. 500000)
1 words of extra memory for PDF output out of 10000 (max. 10000000)

Best Answer

Just as you use

\State $p \gets 1$

the same has to be used in the definition of \For:

\algrenewtext{For}[3]%
   {\algorithmicfor\ $#1 \gets #2$ \algorithmicto\ $#3$ \algorithmicdo}

which will also print the symbols in math mode. The main problem was that \gets can be used only in math mode, but it would be incorrect to say only #1 $\gets$ #2, because this would print variables in normal text mode.

Related Question