[Tex/LaTex] How to avoid the number line for comments under algorithm2e

algorithm2ecommentsline-numbering

In the declaration of the input and output data, I would like to add a comment in order to explain the kind of data.
I use: \usepackage[ruled,vlined]{algorithm2e}

My code is:

\begin{algorithm}[!ht]
\linesnumbered
\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}\;}
.
.
.
\caption{algo}
\label{alg:name}
\end{algorithm}

Unfortunately, with the introduction of the comments in Kwdata the declarations are numbered.
How could I remove the number line in kwdata?

Best Answer

Here are no less than four ways to do it, maybe the second is preferable.

  1. Use separate \KwData lines.
  2. A new command \KwDataXX is defined that leaves space as if Data: is written, but without actually writing it.
  3. Using an aligned environment; here, only one comment can be left to the right of the aligned block
  4. 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}

sample output