[Tex/LaTex] How to change colouring of comments using algorithmicx

algorithmicxcomments

I am using the algorithm and algpseudocode packages, whilst wrapping my algorithms around algorithm and algorithmic blocks. I would like to change the colour or formatting of my comments (\Comment{...}) to make them more or less pronounced compared to standard code, as often with narrow technical documents the comment and pseudocode may come close together and the comment may perhaps overflow to the next line, slightly hampering readability.

Thanks

Best Answer

Just redefine the \algorithmiccomment command provided by algpseudocode. I usually use: \renewcommand{\algorithmiccomment[1]{\hfill$\triangleright$\textit{#1}}

MWE:

\documentclass{article}

\usepackage{algorithm}
\usepackage{algpseudocode}
\renewcommand{\algorithmiccomment}[1]{\hfill$\triangleright$\textit{#1}}

\begin{document}
\begin{algorithm}
\caption{An algorithm with very important comments}
\begin{algorithmic}
\If {$i\geq maxval$}
    \State $i\gets 0$
\Else
    \If {$i+k\leq maxval$}
        \State $i\gets i+k$ \Comment{This is comment}
    \EndIf
\EndIf
\end{algorithmic}
\end{algorithm}
\end{document}

Which produces: