5

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?

  • Please add a minimal working example. – juliohm Jun 20 '13 at 13:03
3

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

  • The second solution would be perfect. Unfortunately, there are three problems during the compilation. 1) Undefined control sequence \algocf@newcommand{K 2) Missing \begin{document}. \algocf@newcommand{K 3) you can't use macro parameter character # in horizontal mode... cf@inputbox\hangafter=\wd\algocf@inputbox# – no_name Jun 21 '13 at 9:14
  • @no_name This is for version 5.0 of algorithm2e, released in January. I saw you used \linesnumbered instead of \LinesNumbered, so I guess you have an older version. Can you update that package? – mafp Jun 21 '13 at 9:34
  • Thank you, mafp. I have updated the package and it works. – no_name Jun 23 '13 at 1:09
2

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.

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.