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.
\documentclass{article}
\usepackage{amsmath}
\usepackage{algorithm2e}

\begin{document}
\begin{algorithm}
\caption{CAS}\label{CAS}
 {$\sum^{N_{C}}_{i=1}$$L_C$} $\gets$ position of \textit{$canditate_i$}\\
{$\sum^{N_{C}}_{i=1}$$D_{C_i}$} $\gets$ distance of \textit{(eNB,$canditate_i$)}\\
\textit{$i$}=$N_C$\\
sort $D_{C_i}$ by descending order\\
$i=N_C$
\While{\textit{$i$}> 0}{
\textit{$i$}= \textit{$i$} - 1

\textit{select $canditiate_i$ as agent}

\textit{random($$D_{C_i}$}select $canditate_i$ as agent}
}


\end{algorithm}
\end{document}

I encounter a compile problem.

I think I'm not Not familiar with its algorithm2e's grammar.

please help me fix this code!!

share|improve this question
    
irrelevant to the question, but i think the word "canditate" (also "canditiate") is really meant to be "candidate". –  barbara beeton Jul 15 at 14:57

3 Answers 3

up vote 1 down vote accepted

You're misusing math mode: there's no need to exit and reenter it; also \textit{$i$} is useless. Conversely, multicharacter variable names should be typeset with \mathit in math mode.

\documentclass{article}
\usepackage{amsmath}
\usepackage{algorithm2e}

\begin{document}
\begin{algorithm}
\caption{CAS}\label{CAS}
$\sum^{N_{C}}_{i=1}L_C \gets \text{position of } \mathit{candidate}_i$\\
$\sum^{N_{C}}_{i=1}D_{C_i} \gets\text{distance of } (\mathit{eNB},\mathit{candidate}_i)$\\
$i=N_C$\\
sort $D_{C_i}$ by descending order\\
$i=N_C$
\While{$i>0$}{
$i=i - 1$

select $\mathit{candidate}_i$ as agent

$\mathit{random}(D_{C_i})$ select $\mathit{candidate}_i$ as agent
}
\end{algorithm}
\end{document}

enter image description here

share|improve this answer

This solution finds errors and corrected in line 15, 20 and added rule and linenumbers into it, making it more readable.

enter image description here

Code

\documentclass{article}
\usepackage{amsmath}
\usepackage[ruled,vlined,linesnumbered]{algorithm2e}

\begin{document}

\begin{algorithm}
\caption{CAS}\label{CAS}
 {$\sum^{N_{C}}_{i=1}$$L_C$} $\gets$ position of \textit{$canditate_i$}\\
{$\sum^{N_{C}}_{i=1}$$D_{C_i}$} $\gets$ distance of \textit{(eNB,$canditate_i$)}\\
\textit{$i$}=$N_C$\\
sort $D_{C_i}$ by descending order\\
$i=N_C$

\While{\textit{i} $> 0$}{
\textit{$i$}= \textit{$i$} - 1

\textit{select $canditiate_i$ as agent}

\textit{random($D_{C_i}$) select $canditate_i$ as agent}
}
\end{algorithm}
\end{document}
share|improve this answer
    
Do you think that \textit{$i$}= \textit{$i$} - 1 is correct? –  egreg Jul 15 at 8:23
    
Well, I would say $i=i-1$ is correct in terms of programming pseudo code. But I won't use \textit{$i$} writing. Please enlighten me, I am not totally understand your question. –  Jesse Jul 15 at 8:32
    
I meant it's incorrect from the typesetter point of view: the - becomes a hyphen and the spacing is all wrong. –  egreg Jul 15 at 8:33
    
Thanks for the pointer. –  Jesse Jul 15 at 8:39

To be compiled this line

\textit{random($$D_{C_i}$}select $canditate_i$ as agent} }

is changed. there are some other changes, however I am not sure, if it is a final version.

\documentclass{article}

\usepackage{amsmath} 
\usepackage{algorithm2e}

\begin{document} 
\begin{algorithm} 
\caption{CAS}
\label{CAS} 
{$\sum^{N_{C}}_{i=1}$$L_C$} $\gets$ position of \textit{$canditate_i$}\\
 {$\sum^{N_{C}}_{i=1}$$D{C_i}$} $\gets$ distance of \textit{(eNB,$canditate_i$)}\\ 
\textit{$i$}=$N_C$\ sort $D_{C_i}$ by descending order\\
 $i=N_C$ \While{{$i> 0$}}{{$i$}= {$i - 1$}

\textit{select $canditiate_i$ as agent}

\textit{random($D_{C_i}$}select $canditate_i$ as agent} 

\end{algorithm}
 \end{document}

enter image description here

Please observe that instead of constructions like \textit{$i$} - 1 there should be simply $i-1$.

share|improve this answer
    
Of course \textit{$i$}=$N_C$ is wrong. –  egreg Jul 15 at 8:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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