20

The Algorithm2e package has specific options for disabling/hiding line numbering for the whole document or algorithm, and then enabling/showing them for specific lines. I want to do the inverse. I mean, enabling line numbering for the whole document, and then disabling them for specific lines. (A question with similar topic was already asked, but it aims at a different target.)

To clarify more, I needed manual line breaks with proper indentations. So, I found the excellent answer of @Werner here. The problem, though, is that the manually created lines get numbering, which is undesirable in my case. Here's an example, taken directly from Werner's answer, with line numbers enabled:

\documentclass{article}
\usepackage[linesnumbered]{algorithm2e}% http://ctan.org/pkg/algorithm2e
\makeatletter
\newcommand{\nosemic}{\renewcommand{\@endalgocfline}{\relax}}% Drop semi-colon ;
\newcommand{\dosemic}{\renewcommand{\@endalgocfline}{\algocf@endline}}% Reinstate semi-colon ;
\newcommand{\pushline}{\Indp}% Indent
\newcommand{\popline}{\Indm\dosemic}% Undent
\makeatother
\begin{document}
\begin{algorithm}[H]
  \SetAlgoLined
  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e }
    initialization\;
  \While{not at end of this document}{
    read current\;
    \eIf{understand}{
      go to next section\;
      \nosemic this is a very long statement that has to be\;
        \pushline\dosemic wrapped over two lines\;
      \popline current section becomes this one\;
    }{
      go back to the beginning of current section\;
    }
  }
  \caption{How to write algorithms}
\end{algorithm}
\end{document}

wrapped lines with numbering

As you can see, line #7 is the wrapped line, but it gets numbered. What I want is that this line does not get any number, and the next line (line #8) gets the number 7.

24

When you load algorithm2e with linesnumbered, it executes \nl at every paragraph break (taken from algorithm2e.sty):

%
% line numbering
%
\newcommand{\LinesNumbered}{%
  \setboolean{algocf@linesnumbered}{true}%
  \renewcommand{\algocf@linesnumbered}{\everypar={\nl}}}%

So, \nl is the thing we are interested in adjusting.

You can remove the line number for a single line using the following \nonl command:

\let\oldnl\nl% Store \nl in \oldnl
\newcommand{\nonl}{\renewcommand{\nl}{\let\nl\oldnl}}% Remove line number for one line

Here's a minimal example showing the output:

enter image description here

\documentclass{article}
\usepackage[linesnumbered]{algorithm2e}% http://ctan.org/pkg/algorithm2e
\makeatletter
\newcommand{\nosemic}{\renewcommand{\@endalgocfline}{\relax}}% Drop semi-colon ;
\newcommand{\dosemic}{\renewcommand{\@endalgocfline}{\algocf@endline}}% Reinstate semi-colon ;
\newcommand{\pushline}{\Indp}% Indent
\newcommand{\popline}{\Indm\dosemic}% Undent
\let\oldnl\nl% Store \nl in \oldnl
\newcommand{\nonl}{\renewcommand{\nl}{\let\nl\oldnl}}% Remove line number for one line
\makeatother
\begin{document}
\begin{algorithm}[H]
  \SetAlgoLined
  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e }
    initialization\;
  \While{not at end of this document}{
    read current\;
    \eIf{understand}{
      go to next section\;
      \nosemic this is a very long statement that has to be\;
        \pushline\dosemic\nonl wrapped over two lines\;
      \popline current section becomes this one\;
    }{
      go back to the beginning of current section\;
    }
  }
  \caption{How to write algorithms}
\end{algorithm}
\end{document}

\nonl redefines \nl for one usage. That usage is just to redefine itself to use the original form (a similar-style trick is used in Cunning (La)TeX tricks).

  • Brilliant, I wish I could up vote your answer twice! – carlito Jun 20 '15 at 21:11

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.