9

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?

3
  • Welcome to TeX.sx! Please crop your images to avoid large margins which makes the question harder to read. Commented Jun 23, 2011 at 16:46
  • @Martin, you was a bit faster then me ;) as i cam to fix it, it was already done Commented Jun 23, 2011 at 16:58
  • Yes, I'm quick in fixing some things. Commented Jun 23, 2011 at 16:59

1 Answer 1

16

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.

1
  • 3
    +1 … algorithmicx is far superior to all the other pseudo-code packages. It wins hands down. Commented Jun 23, 2011 at 18:29

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.