[Tex/LaTex] Adding comments to pseudocode

algorithmspseudocode

I would like to find a pseudocode package that support the possibility to wrote a comment at the right side of a if and that it wont be between { } but rather at the right side of the page, after a symbol.

enter image description here

I would like instead of

if x < p {     .} then

something like this:

if (x < p)         \blacktriangleright x is a ...

my code:

 \documentclass[]{scrbook}
 \usepackage[english]{babel}
 \usepackage[latin1]{inputenc}
 \usepackage[T1]{fontenc}
 \begin{document}
 \begin{algorithm}
 \caption{Influx}
 \label{influx}
 \begin{algorithmic}[1]
 \REQUIRE $p \in [0,1]$, $G$
 \ENSURE None
 \FOR{$i = 0 \to 2^d-1$}
   \IF{$n(\nu_i) = 0$}
     \IF{ $x < p$ \COMMENT{ $x$ is a normal distribution number in the range of $[0,1]$}} 
     \STATE Occupy $v_i$ site with probablility $p$ 
     \ENDIF
   \ENDIF
 \ENDFOR
 \end{algorithmic}
 \end{algorithm}

Any ideas?

Best Answer

To achieve your desire, it would be necessary to make some (as far as I could tell) major changes in the algorithmic package. One possibility is to use the algorithmicx package instead. A little example

\documentclass[]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{algorithm,algorithmicx,algpseudocode}
\usepackage{amssymb}

%\algrenewcommand{\algorithmiccomment}[1]{\hfill$\blacktriangleright$ #1}

\begin{document}
\begin{algorithm}
  \caption{Influx}
  \label{influx}
  \begin{algorithmic}[1]
  \Require $p \in [0,1]$, $G$
  \Ensure None \Comment{a test comment}
  \For{$i = 0 \to 2^d-1$}\Comment{another test comment} 
    \If{$n(\nu_i) = 0$}
      \If{ $x < p$}  \Comment{$x$ is a normal distribution number in the range of $[0,1]$}
      \State Occupy $v_i$ site with probablility $p$ 
      \EndIf
    \EndIf
  \EndFor
  \end{algorithmic}
\end{algorithm}

\end{document}

Here's the result:

enter image description here

Uncommenting the line with \algrenewcommand you'll get a black triangle for the comments.

Related Question