\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}
我遇到了一个编译问题。
我认为我不熟悉它的 algorithm2e 的语法。
请帮我修复这个代码!!
答案1
您误用了数学模式:无需退出并重新进入;而且\textit{$i$}
毫无用处。相反,多字符变量名称应\mathit
在数学模式下排版。
\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}
答案2
该解决方案在第15、20行发现错误并进行更正,并添加规则和行号,使其更具可读性。
代码
\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}
答案3
编译此行
\textit{random($$D_{C_i}$}select $canditate_i$ as agent} }
已更改。还有一些其他更改,但我不确定这是否是最终版本。
\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}
请注意,不应该使用像 这样的结构,而\textit{$i$} - 1
应该使用 简单的结构$i-1$
。
答案4
尝试这个 ..
\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}