The approach of prefixing a line with a command to add a number does not work when the lines are automatically generated, such as end lines for if and for statements.
This answer describes a different approach, where a list is specified at the start of the algorithm that contains the indexes of all lines that need to be numbered. The macro that actually prints the line numbers is modified to check the list and only print the number if it is in the list.
Again this can be done in two variants, one that prints a positional number and one that prints a sequential number. The first variant is used when the command \LnListPos{line indexes}
is entered at the start of the algorithm, the second variant uses \LnListSeq{line indexes}
. When none of these two commands is used then all line numbers are printed by default.
MWE:
\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}
Result:
edit
button below the text.\documentclass
command, have a minimal preamble and then\begin{document}...\end{document}
. The code should compile and be as small as possible to demonstrate your problem. In addition to clarifying your question, a MWE provides people with working code to start from, so it makes it much easier for people to help you and, hence, much more likely that some one will.usepackage[linesnumberedhidden]{algorithm2e}
and then\ShowLn
on a separate line before every line that you want numbered (as in tex.stackexchange.com/questions/125160/…, third example).