Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Is it possible in algorithmicx package to have comments not aligned to the right side?

For example I have code like this -

\begin{algorithm}[!ht]
\caption{My Algo.}
\label{myalgo}
\begin{algorithmic}

\State $\epsilon$ = 1.0;

\Comment{Explore Latency Dimension}
\While {explorationTime <= timeLimit}
    \State $\epsilon$ = $\epsilon$ / 2;
    \State calculateIncrements($\epsilon$);

    \Comment{Explore L dimension}

    \While {lQuery <= lUpperLimit}
         \State Query (0, Query, bQuery, pQuery);
         \If {result = WORKING}
             \State mark points 
             \Comment{no need to explore more. we just want to stop over here.}
             \State Break
         \Else
             \If {result = NOT WORKING}
                \State mark from 0 to lQuery as NOT WORKING.
             \EndIf
         \EndIf

         \State lQuery += lEpsIncr;
    \EndWhile
\EndWhile

\State $calcPoints()$

\end{algorithmic}
\end{algorithm}

So what is happening is that, the package everytime aligns the comments to the right side. But for this comment \Comment{no need to explore more. we just want to stop over here.}, I would like it to have it on single line rather than multiple lines and aligned to right. It becomes a little confusing for me.

Is it possible that we can have comments like -

> no need to explore more. we just want to stop over here.
Break

It should be aligned at the indentation level of the statements.

share|improve this question

1 Answer 1

up vote 6 down vote accepted

Modifying the comment macro is possible using \algrenewcomment, like

\algrenewcomment[1]{\(\triangleright\) #1}

The original \Comment command inserted an \hfill, which I've removed above. This would replace the existing \Comment command globally. However, you can also define your own (new) \LineComment command,

\algnewcommand{\LineComment}[1]{\State \(\triangleright\) #1}

and intermix it with the regular \Comment, like I did below:

enter image description here

\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\algnewcommand{\LineComment}[1]{\State \(\triangleright\) #1}
\begin{document}
\begin{algorithm}[!ht]
  \caption{My Algo.}\label{myalgo}
  \begin{algorithmic}
    \State $\epsilon$ = 1.0; \Comment{Explore Latency Dimension}
    \While {explorationTime $\leq$ timeLimit}
      \State $\epsilon = \epsilon / 2$;
      \State calculateIncrements($\epsilon$);
      \LineComment{Explore L dimension}
      \While {lQuery $\leq$ lUpperLimit}
        \State Query (0, Query, bQuery, pQuery);
        \If {result = WORKING}
          \State mark points 
          \LineComment{no need to explore more. we just want to stop over here.}
          \State Break
        \Else
          \If {result = NOT WORKING}
            \State mark from 0 to lQuery as NOT WORKING.
          \EndIf
        \EndIf
        \State lQuery += lEpsIncr;
      \EndWhile
    \EndWhile
    \State calcPoints()
  \end{algorithmic}
\end{algorithm}
\end{document}
share|improve this answer
    
This is perfect ! Exactly what I wanted. Thanks :) –  Raj Oct 1 '12 at 14:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.