[Tex/LaTex] algorithmicx package comments on a single line

algorithmicxhorizontal alignmentindentation

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.

Best Answer

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}