使用 algpseudocode 套件自訂文字而不是行號

使用 algpseudocode 套件自訂文字而不是行號

我不想印 1:, 2:,.. 我想在每行的開頭列印 Step 1:, Step 2:,.. 。這是我的程式碼:

\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}

\begin{algorithm}
    \caption{My algorithm}
\begin{algorithmic}[1]
    \State foo
    \State bar
\end{algorithmic}
\end{algorithm}

\end{document}

我在看問題,該解決方案對我不起作用,顯示

指令 ALC@lno 未定義。

我認為這是因為我使用的是 algpseudocode 包,而解決方案是使用演算法包。此外,我希望將行號替換為「Step 1」、「Step 2」等;但該解決方案是用固定符號取代行號。

答案1

您可以重新定義內部\ALG@step巨集以在列印數字之前新增文字。以下的定義是從algoritmicx來源(由 所使用algpseudocode)複製而來,其中包含單字添加。因為巨集包含@您需要在定義周圍\makeatletter放置的字元。\makeatother

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}

\makeatletter
\def\ALG@step%
   {%
   \addtocounter{ALG@line}{1}%
   \addtocounter{ALG@rem}{1}%
   \ifthenelse{\equal{\arabic{ALG@rem}}{\ALG@numberfreq}}%
      {\setcounter{ALG@rem}{0}\alglinenumber{Step \arabic{ALG@line}}}%
      {}%
   }%
\makeatother

\begin{document}

\begin{algorithm}
    \caption{My algorithm}
\begin{algorithmic}[1]
    \State foo
    \State bar
\end{algorithmic}
\end{algorithm}

\end{document}

結果:

在此輸入影像描述

相關內容