2

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

1
  • Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}.
    – jub0bs
    Commented Mar 30, 2014 at 12:52

2 Answers 2

8

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:

2
  • Please, don't use the minimal class for examples; it's not meant for this purpose.
    – egreg
    Commented Mar 30, 2014 at 14:07
  • 1
    That's perfect, thanks. I managed to make my comments grey as a result. You are missing a close brace in your paragraph of text. \renewcommand{\algorithmiccomment[1]{\hfill$\triangleright$\textit{#1}} Commented Mar 30, 2014 at 14:13
0

The same problem can be solved with algpseudocodex package (commentColor option).

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .