28

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.

1 Answer 1

39

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}
6
  • 2
    This is perfect ! Exactly what I wanted. Thanks :) Oct 1, 2012 at 14:19
  • This does not respect indentation
    – SumNeuron
    Jul 26, 2017 at 13:11
  • @sumneuron Can you elaborate on the type of indentation that is ignored?
    – Werner
    Jul 26, 2017 at 14:00
  • 2
    Using \Statex instead of \State in the \LineComment macro prevents the line comment from receiving its own line number. Mar 1, 2018 at 2:41
  • 1
    However, this flushes the comment to the left. Check tex.stackexchange.com/a/184829/25523 for a solution to this issue. Mar 1, 2018 at 2:48

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.