如何在 Latex 中为我的算法添加行号

如何在 Latex 中为我的算法添加行号

我正在编写一个算法。我已经使用以下算法包:

\usepackage{algorithmic}
\usepackage{algorithm}
\usepackage{amsmath}

我也正在使用该\STATE标签。我尝试添加包, \usepackage{algpseudocode}但开始出现错误。

答案1

环境algorithmic采用可选参数。可选参数是一个数字,用于定义行号之间的跳过。默认[0]表示不加行号,而[1]每行都加一个行号。[2]每隔一行加一个行号,依此类推:

在此处输入图片描述

\documentclass{article}

\usepackage{algorithm,algorithmic}

\begin{document}

\begin{algorithm}
  \caption{An algorithm}
  \begin{algorithmic}
    \IF{some condition is true}
      \STATE do some processing
    \ELSIF{some other condition is true}
      \STATE do some different processing
    \ELSIF{some even more bizarre condition is met}
      \STATE do something else
    \ELSE
      \STATE do the default actions
    \ENDIF
  \end{algorithmic}
\end{algorithm}

\begin{algorithm}
  \caption{An algorithm}
  \begin{algorithmic}[1]
    \IF{some condition is true}
      \STATE do some processing
    \ELSIF{some other condition is true}
      \STATE do some different processing
    \ELSIF{some even more bizarre condition is met}
      \STATE do something else
    \ELSE
      \STATE do the default actions
    \ENDIF
  \end{algorithmic}
\end{algorithm}

\begin{algorithm}
  \caption{An algorithm}
  \begin{algorithmic}[2]
    \IF{some condition is true}
      \STATE do some processing
    \ELSIF{some other condition is true}
      \STATE do some different processing
    \ELSIF{some even more bizarre condition is met}
      \STATE do something else
    \ELSE
      \STATE do the default actions
    \ENDIF
  \end{algorithmic}
\end{algorithm}

\end{document}

相关内容