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 have written this code to formulate the algorithm for merging two arrays, but I get an error.

\begin{algorithm}[t]
\caption{Serial Merge}
\label{alg:insert}
\begin{algorithmic}[1]
\Require Two sorted arrays $A$ and $B$
\Ensure Sorted Array $C$
\State $lA \leftarrow length(A)$
\State $lB \leftarrow length(B)$
\State $x \leftarrow 0$
\State $y \leftarrow 0$
\State $z \leftarrow 0$

 \While { $x \neq lA$ and $y \neq lB$ }
 \If { $A[x] < $B[y]$}
    \State $C[z++] \leftarrow A[x++]$
  \Else
        \State $C[z++] \leftarrow B[y++]$
    \EndIf
\EndWhile

\While { $x \neq lA$ }
    \State $C[z++] \leftarrow A[x++]$
\EndWhile

\While { $y \neq lB$ }
    \State $C[z++] \leftarrow B[y++]$
\EndWhile

\end{algorithmic}
\end{algorithm}

Error:

! LaTeX Error: Command \item invalid in math mode.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.15     \State
                $C[z++] \leftarrow A[x++]$
? 

It works if I remove the surrounding if else statements. What am I doing wrong?

share|improve this question

closed as too localized by doncherry, Werner, Stefan Kottwitz Dec 13 '12 at 6:31

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
For what is worth. You could have found this error yourself by commenting out parts of the LaTeX code and recompiling your document each time you comment out a new part. When ``the'' error goes away, this usually means it's near the most recent edit. –  Marc van Dongen Dec 13 '12 at 6:35
add comment

1 Answer 1

up vote 3 down vote accepted

In the line

\If { $A[x] < $B[y]$}

there's a spurious $ character before the "B"; simply delete it:

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}

\begin{algorithm}%[t]
\caption{Serial Merge}
\label{alg:insert}
\begin{algorithmic}[1]
\Require Two sorted arrays $A$ and $B$
\Ensure Sorted Array $C$
\State $lA \leftarrow length(A)$
\State $lB \leftarrow length(B)$
\State $x \leftarrow 0$
\State $y \leftarrow 0$
\State $z \leftarrow 0$
%
 \While { $x \neq lA$ and $y \neq lB$ }
 \If { $A[x] < B[y]$}
    \State $C[z++] \leftarrow A[x++]$
  \Else
        \State $C[z++] \leftarrow B[y++]$
    \EndIf
\EndWhile
%
\While { $x \neq lA$ }
    \State $C[z++] \leftarrow A[x++]$
\EndWhile
%
\While { $y \neq lB$ }
    \State $C[z++] \leftarrow B[y++]$
\EndWhile
\end{algorithmic}
\end{algorithm}

\end{document}

enter image description here

share|improve this answer
add comment

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