
를 사용하면 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인지 확인하는 조건을 삽입하고 그에 따라 인쇄하도록 조정합니다 .
일반적으로 임의의 행에 번호 매기기를 설정할 수 있지만 코드는 이에 대해 설정되어 있지 않습니다.