2

I use the algorithm2e-package with option "linesnumbered". Now every line gets a number, but I would like to number only special lines. For example the second and fifth line.

\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}
4
  • The title of the question is about hiding a line, while the question itself is about hiding linenumbers - which of the two are you interested in? If you want, you can edit your question with the edit button below the text.
    – Marijn
    Oct 17 '17 at 12:25
  • Welcome to TeX.SX!! Please give a full minimal working example. A MWE should start with a \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.
    – user30471
    Oct 17 '17 at 12:28
  • @Andrew is right that you should provide an MWE, however a possible solution is to put 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).
    – Marijn
    Oct 17 '17 at 12:39
  • If I use this, the numbers don't start with 1
    – Charlotte
    Oct 17 '17 at 12:53
2

Two variants: number the lines according to their position (2nd line = 2, 5th line = 5), or number the lines sequentially (2nd line = 1, 5th line = 2).

The first variant sets hidden numbers and shows the number for selected lines. Note that the Initialization statement is not numbered, this is by design of algorithm2e (replace the line by \textbf{Initialization} Set $U=\infty$\\ if you want this numbered as well).

\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}

enter image description here

The second variant switches of the numbering and, for selected lines, manually increases the algorithm line counter and shows the number.

\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}

enter image description here

0
1

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:

enter image description here

2
  • Thank you. I am using using algorithm2e not algorithm, so can you please edit that or it's not possible?
    – Avra
    Apr 15 at 18:18
  • Thank you very much. My second issues remains as I want to group $x=y+z$ within $Vectors Z_n=(z_1,\cdots , z_n)$ block and also the $if$ statement, can you please show how to do that?
    – Avra
    Apr 15 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.