# Algorithmic package in LaTeX

I want to write the following algorithm in LaTeX:

 V <- AllUnigramsInTraining(D)
N <- NumberOfTweets(D)
**for** each c ∈ C do
Nc <- Number of tweets in Class c
prior(c) <- [...]
Tweetsc <- All Tweets in class c
**for** each term t ∈ V do
Tct <- No. of times term t appeared in Tweetsc
**end for**
**for** each term t ∈ V do
prob[t][c] <- [...]
**end for**
**end for**


I wrote the following code but it is not compiling properly. Can anybody help?

\begin{algorithmic}
\STATE $V\gets AllUnigramsInTraining(D)$ \\
\STATE $N\gets NumberOfTweets(D)$ \\
\FOR each $c \in C$ \DO \\
\STATE $N_c\gets Number of tweets in Class c$ \\
$$prior(c) = \frac{N_c}{N} \\$$
\STATE $Tweets_c\gets = All Tweets in class c$ \\
\FOR each term $t \in V$ \DO \\
T_ct = No. of times term t appeared in Tweets_c \\
\END
\FOR each term $t \in V$ \DO \\
$$prob[t][c] = \frac{T_ct}{\sum_^t'{T_ct'+ 1}} \\$$
\END
\END
\end{algorithmic}

• Your question at SO got migrated here and subsequently got merged with this one. You need to re-accept an answer now. – Martin Scharrer Jan 16 '12 at 12:50
• I now merged both your accounts together. This changed the displayed username of this question. Feel free to change the user name used on this site using your profile page. – Martin Scharrer Jan 17 '12 at 10:59

There is so much wrong there. You don't need \\ at the end of lines for starters. You have an (unbalanced) inline math $ inside of an equation. You have subscripts outside of math mode. Your sum notation has problems. I don't know what you want exactly, but this should at least give you output, whether it's exactly what you want... I don't know. \documentclass{article} \usepackage{algorithmic} \begin{document} \begin{algorithmic} \STATE$V\gets$AllUnigramsInTraining($D$) \STATE$N\gets$NumberOfTweets($D$) \FORALL{$c \in C$} \STATE$N_c \gets$Number of tweets in Class$c$\STATE $$prior(c) = \frac{N_c}{N}$$ \STATE$Tweets_c \gets $All Tweets in class$c$\FORALL{$t \in V$} \STATE$T_ct$= No. of times term$t$appeared in$Tweets_c$\ENDFOR \FORALL{$t \in V\$}
\STATE $$prob[t][c] = \frac{T_ct}{\sum^{t'}T_ct'+ 1}$$

Your for statements are missing brackets, and your equation blocks should be \state lines.