Algorithmicxはn番目ごとに番号を付けるときに最初の行番号を印刷します

Algorithmicxはn番目ごとに番号を付けるときに最初の行番号を印刷します

を使用すると、algorithmicx行番号のオン/オフを切り替えたり、n行ごとに番号を付けたりすることが非常に簡単になります。このMWE(ドキュメントから引用)

\documentclass{article}
\usepackage{algpseudocode}
\usepackage{algorithm}
\begin{document}
\begin{algorithm}[H]
\caption{Euclide's algorithm}\label{euclid}
\begin{algorithmic}[5]
\Procedure{Euclide}{$a,b$}\Comment{The g.c.d. of a and b}
   \State $r\gets a\bmod b$
   \While{$r\not=0$}\Comment{We have the answer if r is 0}
      \State $a\gets b$
      \State $b\gets r$
      \State $r\gets a\bmod b$
   \EndWhile\label{euclidendwhile}
   \State \Return $b$\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}

この例では、最初の番号付き行は 5 行目です。最初の行の番号付けをオンにする方法はありますか?

次の質問ですが、任意の行の番号を変更することは可能ですか?

答え1

印刷条件を変更するには、特定のマクロを更新する必要があります。

ここに画像の説明を入力してください

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

\makeatletter
\def\ALG@step%
   {%
   \stepcounter{ALG@line}%
   \stepcounter{ALG@rem}%
   \ifnum\value{ALG@line}=1
     % Print line number if it is 1
     \alglinenumber{\arabic{ALG@line}}%
   \else\ifnum\value{ALG@rem}=\ALG@numberfreq
     % Print line number if the ALG@rem = \ALG@numberfreq
     \setcounter{ALG@rem}{0}\alglinenumber{\arabic{ALG@line}}%
   \fi\fi
   }%
\makeatother

\begin{document}
\begin{algorithm}[H]
  \caption{Euclide's algorithm}\label{euclid}
  \begin{algorithmic}[5]
    \Procedure{Euclide}{$a,b$}\Comment{The g.c.d.\ of~$a$ and~$b$}
       \State $r \gets a \bmod b$
       \While{$r \neq 0$}\Comment{We have the answer if~$r$ is~$0$}
          \State $a \gets b$
          \State $b \gets r$
          \State $r \gets a \bmod b$
       \EndWhile\label{euclidendwhile}
       \State \Return $b$\Comment{The g.c.d.\ is~$b$}
    \EndProcedure
  \end{algorithmic}
\end{algorithm}
\end{document}

調整により、\ALG@step行番号が 1 であるかどうかを確認する条件が挿入され、それに応じて印刷されます。

一般的には、任意の行で番号付けをオンにすることは可能ですが、コードはそのために設定されていません。

関連情報