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:
\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}
In answer to your commented request in the code, create something like \LineComment{<comment>}
, which will set a comment right-aligned and italicized:
\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\newcommand{\LineComment}[1]{\Statex \hfill\textit{#1}}
\begin{document}
\begin{algorithm}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{The g.c.d.\ of~$a$ and~$b$}
\State $r \gets a \bmod b$
\While{$r \neq 0$}\LineComment{We have the answer if~$r$ is~$0$}
\State $a \gets b$
\State $b \gets r$
\State $r \gets a \bmod b$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $b$\Comment{The g.c.d.\ is~$b$}
\EndProcedure
\end{algorithmic}
\caption{Euclid’s algorithm}\label{euclid}
\end{algorithm}
\end{document}
You can format \LineComment
the way you want. For example, using
\newcommand{\LineComment}[1]{\Statex \hfill/* \textit{#} */}
would wrap the line comment in /*
...*/
:
Following some guidelines in Using end-of-line delimiter in plain TeX macro, you could also use
\makeatletter
\let\fslash=/
\catcode`\/=\active
\newcommand{/}{\@ifnextchar/{\begingroup\catcode`\^^M=12 \fslash@}\fslash}
{\catcode`\^^M=12 %
\gdef\fslash@/#1^^M{\hfill \fslash\fslash{\itshape#1}\endgroup}}
\makeatother
in order to make /
active. If used in succession //
, it captures the contents until the end-of-line character is reached, placing it in an italicized font:
\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\newcommand{\LineComment}[1]{\Statex \hfill/* \textit{#1} */}
% https://tex.stackexchange.com/q/125549/5764
\makeatletter
\let\fslash=/
\catcode`\/=\active
\newcommand{/}{\@ifnextchar/{\begingroup\catcode`\^^M=12 \fslash@}\fslash}
{\catcode`\^^M=12 %
\gdef\fslash@/#1^^M{\hfill \fslash\fslash{\itshape#1}\endgroup}}
\makeatother
\begin{document}
\begin{algorithm}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{The g.c.d.\ of~$a$ and~$b$}
\State $r \gets a \bmod b$
\While{$r \neq 0$}\LineComment{We have the answer if~$r$ is~$0$}
\State $a \gets b$ // Another comment
\State $b \gets r$
\State $r \gets a \bmod b$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $b$\Comment{The g.c.d.\ is~$b$}
\EndProcedure
\end{algorithmic}
\caption{Euclid’s algorithm}\label{euclid}
\end{algorithm}
\end{document}
The above momentary redefinition of ^^M
(the end-of-line char) could be problematic, but really depends on the circumstances. Regardless, the following might be safer:
\usepackage{etoolbox}
\makeatletter
\let\fslash=/
\catcode`\/=\active
\newcommand{/}{\@ifnextchar/{\fslash@}\fslash}
\def\fslash@/{\hfill \fslash\fslash\itshape}
\AtBeginEnvironment{algorithmic}{%
\let\olditem\item
\renewcommand{\item}{\upshape\olditem}
}
\makeatother
Best Answer
Wrap the comment inside a
t
op-aligned\parbox
of specific width. Consider the following example: