如何在 Latex 中的算法中添加选定行的行号?

如何在 Latex 中的算法中添加选定行的行号?

我正在撰写一篇文章提交给 IEEE,按照 IEEEtran 类的说明,我使用该algorithmicx.sty包在环境内编写算法,而figure不是使用环境algorithm,因为 IEEE 使用的唯一浮动结构是图形和表格。algorithm.styalgorithm2e.sty

我正在\begin{algorithmic}[3]自动对伪代码的每三行进行编号,但我还想对第一行和最后一行进行编号。

这是我的 MWE:

\documentclass[a4paper,10pt]{IEEEtran}
\usepackage{algpseudocode}

\begin{document}
See \figurename{~\ref{fig:myalgorithm}} below.

\begin{figure}[!ht]
 \noindent\rule{\linewidth}{0.8pt}
 \noindent \textbf{Algorithm} itsname
 \noindent\hrule
 \begin{algorithmic}[3]
  \Procedure {BellmanKalaba}{$G$, $u$, $l$, $p$}
  \ForAll {$v \in V(G)$}
  \State $l(v) \leftarrow \infty$
  \EndFor
  \State $l(u) \leftarrow 0$
  \Repeat
  \For {$i \leftarrow 1, n$}
  \State $min \leftarrow l(v_i)$
  \For {$j \leftarrow 1, n$}
  \If {$min > e(v_i, v_j) + l(v_j)$}
  \State $min \leftarrow e(v_i, v_j) + l(v_j)$
  \State $p(i) \leftarrow v_j$
  \EndIf
  \EndFor
  \State $l'(i) \leftarrow min$
  \EndFor
  \State $changed \leftarrow l \not= l'$
  \State $l \leftarrow l'$
  \Until{$\neg changed$}
  \EndProcedure
 \end{algorithmic}
 \noindent\rule{\linewidth}{0.8pt}
 \caption{This algorithm was copied from the algorithmicx's package documentation.}\label{fig:myalgorithm}
\end{figure}

\end{document}

行号的显示方式如下: 每三行渲染一次

这就是我希望它出现的样子: 渲染第一行和最后一行

有人能帮助我吗?

答案1

我无法弄清楚最后一步,所以我不得不找别人帮忙。这是 Skillmon 的善意建议,请随意他的回答因为他做了所有艰难的工作

\documentclass[a4paper,10pt]{IEEEtran}
\usepackage{algpseudocode}
\usepackage{etoolbox}

% Patch the line numbering to display the first and last line numbers
\makeatletter
% execute this code at \end{algorithmic} but before the group is closed
\AtEndEnvironment{algorithmic}{\@WriteAlgorithmicLastLine}
% execute this code at \begin{algorithmic} just after the group is opened
\AtBeginEnvironment{algorithmic}{\stepcounter{AlgorithmicEnvironmentCounter}}
\newcounter{AlgorithmicEnvironmentCounter}
\newcommand*\@WriteAlgorithmicLastLine
  {% this macro writes the definition to the aux file, you have to make sure
   % that only the right stuff is expanded (\write expands its argument as
   % much as it is able to)
    \immediate\write\@auxout
      {%
        % we don't want \expandafter to disappear
        \unexpanded{\expandafter\gdef}%
        % we don't want \csname to do its work just here
        \unexpanded{\csname @AlgorithmicLastLine@}%
        % the counter should be expanded
        \the\c@AlgorithmicEnvironmentCounter%
        % \endcsname isn't expandable (if you're unsure whether something is
        % expandable enclose it by \unexpanded or precede it with \noexpand)
        \endcsname%
        % this counter should as well be expanded
        {\arabic{ALG@line}}%
      }%
  }
\def\ALG@step%
  {%
    \addtocounter{ALG@line}{1}%
    \addtocounter{ALG@rem}{1}%
    % Beginning of patch
   \ifthenelse{\equal{\arabic{ALG@rem}}{\ALG@numberfreq}}%
      {\setcounter{ALG@rem}{0}\alglinenumber{\arabic{ALG@line}}}%
      {% if it is no line that already gets a number one of the other rules
       % might apply
        \ifthenelse{\equal{\arabic{ALG@line}}{1}}%
          {\alglinenumber{\arabic{ALG@line}}}%
          {% if not the first line, it may be the last
            % \ifcsdef is a wrapper around \ifcsname to check whether something
            % is defined or not
            \ifcsdef
              {@AlgorithmicLastLine@\arabic{AlgorithmicEnvironmentCounter}}%
              {% if it is defined execute this code
                % \ifnum is a TeX primitive comparing the following two numbers
                % and executing the true branch if the comparison is true,
                % possible comparisons are '=', '>' and '<'.
                \ifnum
                  % csuse is a wrapper around \csname building the
                  % control-sequence which's name is the argument of \csuse
                  % (argument is fully expanded)
                  \csuse
                    {%
                      @AlgorithmicLastLine@%
                      \arabic{AlgorithmicEnvironmentCounter}%
                    }%
                  =\c@ALG@line
                  \alglinenumber{\arabic{ALG@line}}%
                \fi
              }%
              {}% not defined
          }%
      }%
   }%
\makeatother


\begin{document}
See \figurename{~\ref{fig:myalgorithm}} below.

\begin{figure}[!ht]
 \noindent\rule{\linewidth}{0.8pt}
 \noindent \textbf{Algorithm} itsname
 \noindent\hrule
 \begin{algorithmic}[4]
  \Procedure {BellmanKalaba}{$G$, $u$, $l$, $p$}
  \ForAll {$v \in V(G)$}
  \State $l(v) \leftarrow \infty$
  \EndFor
  \State $l(u) \leftarrow 0$
  \Repeat
  \For {$i \leftarrow 1, n$}
  \State $min \leftarrow l(v_i)$
  \For {$j \leftarrow 1, n$}
  \If {$min > e(v_i, v_j) + l(v_j)$}
  \State $min \leftarrow e(v_i, v_j) + l(v_j)$
  \State $p(i) \leftarrow v_j$
  \EndIf
  \EndFor
  \State $l'(i) \leftarrow min$
  \EndFor
  \State $changed \leftarrow l \not= l'$
  \State $l \leftarrow l'$
  \Until{$\neg changed$}
  \EndProcedure
 \end{algorithmic}
 \noindent\rule{\linewidth}{0.8pt}
 \caption
  {%
    This algorithm was copied from the algorithmicx's package documentation.%
    \label{fig:myalgorithm}%
  }
\end{figure}
\end{document}

相关内容