如何正确显示我的 Latex 伪代码

如何正确显示我的 Latex 伪代码

我使用 latex 编写了此代码,但是,每次运行它时,最后都会出现 =0。请问我该如何停止显示“=0”。

下面是我的 Latex 伪代码:

\begin{algorithm}
  \caption{Approximate Point in Triangulation Algorithm}

  \begin{algorithmic}

    \State S represents the set of anchors seen/reached by the free node.
    \State $N_i$ represents the set of anchors seen/reached by each
    \State anchor \in S. \newline
    \setlength{\parindent}{5ex} \State For \mid$S$\mid  number of anchors \{}
    \State reachNodes = S\cap $N_i$)
    \State { \}
    \State sort reachNodes in descending order
    \State select the three highest reachNodes
    \State /* Centre of Gravity(COG) calculation */
    \State Estimated Position $=$ COG of three highest reachNodes; \newline
   \end{algorithmic}
  \end{algorithm}

显示的内容如下:

在此处输入图片描述

答案1

您的代码包含多个 LaTeX 错误。如果您跳过它们,则输出将出现乱码。

  • 在数学模式下排版数学,并注意再次终止数学模式。

  • 缩进是算法环境处理的事情之一;\For在这种情况下,只需使用提供的命令。

  • 您不需要\newlines。行会自动换行。如果您不希望算法占据整个宽度,请将algorithmic环境放入minipage\begin{minipage}{7cm}\begin{algorithmic}...\end{algorithmic}\end{minipage}

在此处输入图片描述

\documentclass{article}
\usepackage{algpseudocode,algorithm}
\begin{document}
\begin{algorithm}
  \caption{Approximate Point in Triangulation Algorithm}
  \begin{algorithmic}
    \State $S$ represents the set of anchors seen/reached by the free node.
    \State $N_i$ represents the set of anchors seen/reached by each anchor $i{}\in S$.
    \For{each anchor $i\in S$}
    \State $\mathit{reachNodes} = S\cap N_i$
    \EndFor
    \State sort $\mathit{reachNodes}$ in descending order
    \State select the three highest nodes in $\mathit{reachNodes}$
    \State /* Centre of Gravity (COG) calculation */
    \State Estimated Position $=$ COG of three highest $\mathit{reachNodes}$;
   \end{algorithmic}
  \end{algorithm}
\end{document}

相关内容