如何在算法中隐藏特定的行号?

如何在算法中隐藏特定的行号?

我使用 algorithm2e-package 和选项“linesnumbered”。现在每行都有一个编号,但我想只对特殊行进行编号。例如第二行和第五行。

\documentclass[12pt,a4paper]{article}

\usepackage[utf8]{inputenc}

\usepackage{amssymb}

\usepackage[vlined,linesnumbered,ruled,resetcount]{algorithm2e}

\SetKwBlock{Repeat}{repeat}{}

\SetKwInOut{Initialization}{Initialization}

\begin{document}

\begin{algorithm}[H]

\caption{Test algorithm}

\Initialization{Set $U=\infty$}

\While{($\exists$ pending nodes in the tree)}{

    Select an unexplored node. \\

    \Repeat (\{iteration\}){

        Solve problem (5). \\

    \eIf{(5) infeasible}{fathome node.}

      {$(x^{(k+1)},y^{(k+1)})=(x^{(k)},y^{(k)})+(d_x^{(k)},d_y^{(k)})$.}}}
\end{algorithm}

\end{document}

答案1

两种变体:根据行的位置对行进行编号(第 2 行 = 2,第 5 行 = 5),或者按顺序对行进行编号(第 2 行 = 1,第 5 行 = 2)。

第一种变体设置隐藏数字并显示所选行的数字。请注意初始化语句未编号,这是设计使然(如果您也希望该语句也编号,algorithm2e请用 替换该行)。\textbf{Initialization} Set $U=\infty$\\

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amssymb}
\usepackage[vlined,linesnumberedhidden,ruled,resetcount]{algorithm2e}

\SetKwBlock{Repeat}{repeat}{}
\SetKwInOut{Initialization}{Initialization}

\begin{document}
\begin{algorithm}[H]
\caption{Test algorithm}
\Initialization{Set $U=\infty$}
\While{($\exists$ pending nodes in the tree)}{
    \ShowLn
    Select an unexplored node. \\
    \Repeat (\{iteration\}){
        Solve problem (5). \\
    \ShowLn
    \eIf{(5) infeasible}{fathome node.}
      {$(x^{(k+1)},y^{(k+1)})=(x^{(k)},y^{(k)})+(d_x^{(k)},d_y^{(k)})$.}}}
\end{algorithm}
\end{document}

在此处输入图片描述

第二种变体是切换编号,对于选定的行,手动增加算法行计数器并显示数字。

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amssymb}
\usepackage[vlined,linesnumbered,ruled,resetcount]{algorithm2e}

\SetKwBlock{Repeat}{repeat}{}
\SetKwInOut{Initialization}{Initialization}

\newcommand{\nextnr}{\stepcounter{AlgoLine}\ShowLn}

\begin{document}
\LinesNotNumbered
\begin{algorithm}[H]
\caption{Test algorithm}
\Initialization{Set $U=\infty$}
\While{($\exists$ pending nodes in the tree)}{
    \nextnr
    Select an unexplored node. \\
    \Repeat (\{iteration\}){
        Solve problem (5). \\
    \nextnr
    \eIf{(5) infeasible}{fathome node.}
      {$(x^{(k+1)},y^{(k+1)})=(x^{(k)},y^{(k)})+(d_x^{(k)},d_y^{(k)})$.}}}
\end{algorithm}
\end{document}

在此处输入图片描述

答案2

在自动生成行时,使用命令在行前添加数字的方法不起作用,例如结尾if 和 for 语句的行。

这个答案描述了一种不同的方法,在算法开始时指定一个列表,其中包含需要编号的所有行的索引。实际打印行号的宏被修改为检查列表,并且仅在列表中时才打印编号。

同样,这可以通过两种变体来实现,一种是打印位置编号,另一种是打印顺序编号。第一种变体\LnListPos{line indexes}在算法开始时输入命令时使用,第二种变体使用\LnListSeq{line indexes}。当不使用这两个命令时,默认情况下会打印所有行号。

梅威瑟:

\documentclass{article}
\usepackage[linesnumbered,ruled]{algorithm2e}
\usepackage{etoolbox} % for \AtBeginEnvironment
% result of list membership check
\newif\ifmember
% use list check or not
\newif\iflnlistcheck
% number sequentially or not
\newif\iflnseq
% separate counter for sequential numbers
\newcounter{AlgoSeqLine}
% set the default to not using the list check
\AtBeginEnvironment{algorithm}{%
\lnlistcheckfalse%
\lnseqfalse%
\setcounter{AlgoSeqLine}{0}%
}
% command to setup positional numbers
\newcommand{\LnListPos}[1]{%
\lnlistchecktrue%
\gdef\lnlist{#1}%
\lnseqfalse%
}
% command to setup sequential numbers
\newcommand{\LnListSeq}[1]{%
\lnlistchecktrue%
\gdef\lnlist{#1}%
\lnseqtrue%
}
% list membership command
\makeatletter% for \@for see e.g. https://tex.stackexchange.com/a/100684/
% from https://tex.stackexchange.com/a/498576/
% this version from https://tex.stackexchange.com/a/501776/
\newcommand{\MemberQ}[2]{\global\memberfalse%
\@for\next:=#1\do{\ifnum\next=#2\global\membertrue\fi}}

% modified algorithm2e command to print line numbers
\renewcommand{\algocf@printnl}[1]{%
% store current line number (argument #1) in a macro
\def\currentln{#1}%
% check the list if the list check setting is true
\iflnlistcheck%
\MemberQ{\lnlist}{\currentln}%
% if the check is not performed, pretend that the number is always in the list
\else\global\membertrue\fi%
% if the number is in the list (or the check not performed),
% print the number
\ifmember%
% if numbers are sequential then
% increase the sequential counter
% and set the current number to this counter
% otherwise the number is unchanged,
% i.e., the regular line counter from algorithm2e
\iflnseq%
\stepcounter{AlgoSeqLine}%
\edef\currentln{\arabic{AlgoSeqLine}}%
\fi% definition from algorithm2e follows, replaced #1 with \currentln
   \ifthenelse{\boolean{algocf@leftlinenumber}}{%
    \skiplinenumber=\skiptotal\advance\skiplinenumber by\leftskip%
    \strut\raisebox{0pt}{\llap{\NlSty{\currentln}\kern\skiplinenumber}}\ignorespaces%
  }{%
    \sbox\algocf@nlbox{\NlSty{\currentln}}%
    \skiplinenumber=\hsize\advance\skiplinenumber by-\leftskip\advance\skiplinenumber by-\skiptext%
    \advance\skiplinenumber by\algomargin\advance\skiplinenumber by.3em\advance\skiplinenumber by-\wd\algocf@nlbox%
    % to handle particular case of until: printnl is after 'until' keyword has been writen, so we need to substract length of this keyword
    \advance\skiplinenumber by-\algocf@skipuntil%
    \strut\raisebox{0pt}{\rlap{\kern\skiplinenumber\NlSty{#1\ignorespaces}}}\ignorespaces%
  }%
\fi
}%
\makeatother

\begin{document}
\begin{algorithm}
\DontPrintSemicolon
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\textbf{Data}: \\
\For{$\omega = 1,2, \cdots, N$}{
Vectors $Z_n=(z_{1},\cdots , z_{n})$:
$x = y + z$\;
\If{$\tau \in \{\chi\}$ in $\sigma$}{       
}
}
\Output{Score}
\caption{default, all lines numbered}
\end{algorithm}

\begin{algorithm}
\DontPrintSemicolon
\LnListPos{1,2,3,6}
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\textbf{Data}: \\
\For{$\omega = 1,2, \cdots, N$}{
Vectors $Z_n=(z_{1},\cdots , z_{n})$:
$x = y + z$\;
\If{$\tau \in \{\chi\}$ in $\sigma$}{       
}
}
\Output{Score}
\caption{positional line numbers from list}
\end{algorithm}

\begin{algorithm}
\DontPrintSemicolon
\LnListSeq{1,2,3,6}
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\textbf{Data}: \\
\For{$\omega = 1,2, \cdots, N$}{
Vectors $Z_n=(z_{1},\cdots , z_{n})$:
$x = y + z$\;
\If{$\tau \in \{\chi\}$ in $\sigma$}{       
}
}
\Output{Score}
\caption{sequential line numbers based on list}
\end{algorithm}

\end{document}

结果:

在此处输入图片描述

相关内容