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, и печати соответствующего значения.

В общем случае можно было бы включить нумерацию на любой произвольной строке, но код для этого не настроен.

Связанный контент