21

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.

27

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}
  • 1
    This is perfect ! Exactly what I wanted. Thanks :) – knowledge_seeker Oct 1 '12 at 14:19
  • This does not respect indentation – SumNeuron Jul 26 '17 at 13:11
  • @sumneuron Can you elaborate on the type of indentation that is ignored? – Werner Jul 26 '17 at 14:00
  • @Werner the start of the comment is indented correctly. However, if the comment is long, the subsequent lines take up the full line width of its containing element. – SumNeuron Jul 26 '17 at 14:10
  • Using \Statex instead of \State in the \LineComment macro prevents the line comment from receiving its own line number. – Thomas Auzinger Mar 1 '18 at 2:41

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.