1

For my algorithms in Algorithm2e, I would like to only number the lines that end with a semicolon \;.

I tried to hack something together using the answers to Algorithm2e - Disabling line numbers for specific lines :

\let\oldnl\nl
\newcommand{\nonl}{\renewcommand{\nl}{\let\nl\oldnl}}
\let\oldKwSty\KwSty
\renewcommand{\KwSty}[1]{\nonl\textnormal{\textbf{#1}}\unskip}

I hoped this would disable numbering on lines with a keyword, but it does not entirely work as required:

\documentclass{article}
\usepackage[ruled, linesnumbered]{algorithm2e}
\let\oldnl\nl
\newcommand{\nonl}{\renewcommand{\nl}{\let\nl\oldnl}}
\let\oldKwSty\KwSty
\renewcommand{\KwSty}[1]{\nonl\textnormal{\textbf{#1}}\unskip}
\begin{document}
\begin{algorithm}[H]
%\SetLine
\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\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms}
\end{algorithm}
\end{document}

Gives Algorithm with insufficient numbering

I would like the other lines with semicolons to also be numbered (i.e. from 1 to 5).

I also tried modifying the \; command, anything I changed there either printed the numbers at the end of the line, or modified the behaviour of the next line.

0 Answers0