How to avoid the number line for comments under algorithm2e
Here are no less than four ways to do it, maybe the second is preferable.
- Use separate
\KwData
lines. - A new command
\KwDataXX
is defined that leaves space as ifData:
is written, but without actually writing it. - Using an
aligned
environment; here, only one comment can be left to the right of thealigned
block - using an
align*
environment with comments in the math block itself
Code:
\documentclass{article}
\usepackage[ruled,vlined]{algorithm2e}
\usepackage{amsmath,amssymb}
\usepackage{cprotect}%% only needed to have \verb work in \caption
\makeatletter
\algocf@newcommand{KwDataXX}[1]{%
\sbox\algocf@inputbox{\hbox{\KwSty{Data}\algocf@typo: }}%
\ifthenelse{\boolean{algocf@inoutnumbered}}{\relax}{\everypar={\relax}}%
{\let\\\algocf@newinput\hspace{\wd\algocf@inputbox}\hangindent=\wd\algocf@inputbox\hangafter=\wd\algocf@inputbox#1\par}%
\algocf@linesnumbered% reset the numbering of the lines
}
\makeatother
\begin{document}
\begin{algorithm}[!ht]
\LinesNumbered
\KwData{$\mathbf{X}\in \mathbb{R}^{i\times j}$ \tcp*{comment}}
\KwData{$p_x,p_y,p_z \in \mathbb{N}$ \tcp*{comment}}
\KwData{$\alpha \in \mathbb{R}$ \tcp*{comment}}
\KwResult{$\mathcal{X}\in \mathbb{R}^{i\times j\times k}$ \tcp*{comment}}
\cprotect\caption{algo -- separate \verb!\KwData! lines}
\label{alg:name}
\end{algorithm}
\begin{algorithm}[!ht]
\LinesNumbered
\KwData {$\mathbf{X}\in \mathbb{R}^{i\times j}$ \tcp*{comment}}
\KwDataXX{$p_x,p_y,p_z \in \mathbb{N}$ \tcp*{comment}}
\KwDataXX{$\alpha \in \mathbb{R}$ \tcp*{comment}}
\KwResult{$\mathcal{X}\in \mathbb{R}^{i\times j\times k}$ \tcp*{comment}}
\cprotect\caption{algo2 -- using the new \verb!\KwDataXX! command}
\label{alg:name2}
\end{algorithm}
\begin{algorithm}[!ht]
\LinesNumbered
\DontPrintSemicolon
\KwData{%
$\begin{aligned}
\mathbf{X} & \in \mathbb{R}^{i\times j}\\
p_x,p_y,p_z & \in \mathbb{N}\\
\alpha & \in \mathbb{R}
\end{aligned}$\tcp*{comment}
}
\KwResult{$\mathcal{X}\in \mathbb{R}^{i\times j\times k}$}
\cprotect\caption{algo3 -- using an \verb!aligned! environment, only one comment possible}
\label{alg:name3}
\end{algorithm}
\begin{algorithm}[!ht]
\LinesNumbered
\KwData{%
\begin{align*}
\mathbf{X} & \in \mathbb{R}^{i\times j} && \text{comment}\\
p_x,p_y,p_z & \in \mathbb{N} && \text{comment} \\
\alpha & \in \mathbb{R} && \text{comment}
\end{align*}%
}
\KwResult{$\mathcal{X}\in \mathbb{R}^{i\times j\times k}$}
\cprotect\caption{algo4 -- using \verb!align*! with in-math comments}
\label{alg:name4}
\end{algorithm}
\end{document}
I usually use \LinesNumberedHidden
before \begin{algorithm}
and then use \ShowLn
on each line that I want to have numbered.
For example:
\LinesNumberedHidden
\begin{algorithm}[H]
\dontprintsemicolon
\KwData{$\mathbf{X}\in \mathbb{R}^{i\times j}$\tcp*{comment}\;
$\qquad\quad\:p_x,p_y,p_z \in \mathbb{N}$\tcp*{comment}\;
$\qquad\quad\:\alpha \in \mathbb{R}$\tcp*{comment}\;}
\KwResult{$\mathcal{X}\in \mathbb{R}^{i\times j\times k}$\tcp*{comment}\;}
\ShowLn Line 2
\ShowLn Line 3 etc...
\end{algorithm}
Although I know the previous answer is practically the general and efficient way to do this, I think this is the easiest. I know you would technically have to use the \ShowLn command on every line for this algorithm, but at least you don't have to define a new command.
The \LinesNumberedHidden
command only applies for the next algorithm environment, but not for the rest of the algorithms in your document. So, this is another reason why I find it convenient.