LaTeX 中的伪代码

LaTeX 中的伪代码
\documentclass{article}
\usepackage{amsmath}
\usepackage[noend]{algpseudocode}
\usepackage{algorithm}

\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
`\makeatother

\begin{document}
\begin{algorithm}
\caption{AIP}\label{euclid}
\begin{algorithmic}[1]
\State \textit{$\sum^{N_agent}_{i=1}$$L_A$} $\gets$ position of \textit{$agent_i$}
\BState \emph{loop}:
\State \textit{$i$}$\gets one
\State \textit{$i$ plus one}
\If {\textit{distance(eNB,$L_i$)} $< \delta$} \textit{close $agent_i$}
\IF {\textit{distance(eNB,$L_i$)}$>\Delta$} \textit{$agent_i$ power equals P$}
\State \textbf{goto} loop

\end{algorithmic}
\end{algorithm}
\end{document}

有人能帮助我成功编译这个吗?

答案1

类似于此。其中存在拼写错误,如下所示。

  1. makeatother 之前有一个额外的 `。
  2. IF 是 If 的拼写错误。
  3. $gets 之后缺少 $。
  4. P$ 之前缺少 $。
  5. 忘记用 \EndIf 结束 IF

在此处输入图片描述

代码

\documentclass{article}
\usepackage{amsmath}
\usepackage[noend]{algpseudocode}
\usepackage{algorithm}

\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother                           % error was here

\begin{document}
\begin{algorithm}
\caption{AIP}\label{euclid}
\begin{algorithmic}[1]
\State \textit{$\sum^{N_agent}_{i=1}$$L_A$} $\gets$ position of \textit{$agent_i$}
\BState \emph{loop}:
\State \textit{$i$}$\gets$ one         % error was here
\State \textit{$i$ plus one}
\If {\textit{distance(eNB,$L_i$)} $< \delta$} \textit{close $agent_i$}
\If {\textit{distance(eNB,$L_i$)}$>\Delta$} \textit{$agent_i$ power equals $P$}  % error was here
\State \textbf{goto} loop
\EndIf                                 % error was here
\EndIf                                 % error was here
\end{algorithmic}
\end{algorithm}
\end{document}

相关内容