Tx · LaTeX & TikZ

How to Change Comment Layout in algorithmicx Pseudocode

Published By the 9to5Science editorial team

At a glance

  1. 1. Identify the command

    With algpseudocode, comments are printed using the algorithmiccomment command.

  2. 2. Redefine the style

    Use algrenewcommand to change alignment, marker, color, or spacing.

  3. 3. Choose a placement

    Keep comments beside statements, or use an unnumbered state line for a comment below.

With the algorithmicx package and its algpseudocode layout, \Comment{...} prints a comment on the same line as a pseudocode statement. You can change the comment’s marker, color, and alignment by redefining \algorithmiccomment. If you want a comment on its own line instead, use an unnumbered \Statex line.

Change the inline comment style

The algpseudocode layout defines \Comment in terms of \algorithmiccomment. Redefine the latter in your document preamble, after loading algpseudocode:

\algrenewcommand{\algorithmiccomment}[1]{\hfill\textcolor{gray}{\(\triangleright\)~#1}}

This keeps the comment on the statement’s line, pushes it to the right with \hfill, and colors the marker and text gray. The xcolor package provides \textcolor, so load it if you use this styling.

Here is a complete example:

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{xcolor}

\algrenewcommand{\algorithmiccomment}[1]{%
  \hfill\textcolor{gray}{\(\triangleright\)~#1}%
}

\begin{document}
\begin{algorithm}
\caption{Initialize a counter}
\begin{algorithmic}[1]
  \State $i \gets 0$ \Comment{initialize the counter}
  \While{$i < 3$}
    \State $i \gets i + 1$ \Comment{advance the counter}
  \EndWhile
\end{algorithmic}
\end{algorithm}
\end{document}

The packages have separate jobs: algorithm supplies the floating algorithm environment, while algpseudocode supplies the pseudocode commands. The optional [1] after \begin{algorithmic} displays line numbers.

Move the comment or change its marker

The placement is controlled by the definition you provide. For a comment immediately after the statement rather than pushed toward the right margin, omit \hfill:

\algrenewcommand{\algorithmiccomment}[1]{\(\triangleright\)~#1}

To use a different marker, replace \triangleright with text or another suitable symbol. For example, \texttt{//} gives a C-style marker:

\algrenewcommand{\algorithmiccomment}[1]{\hfill\texttt{//}~#1}

The \Comment command still takes one argument, so write the comment text inside braces: \Comment{update the result}. The redefinition changes how that text is printed, not how the algorithm statements are structured.

Put a comment on its own line

An inline comment can become cramped when the statement is long. To put a note below a statement, add an unnumbered line explicitly:

\State $r \gets a + b$
\Statex \(\triangleright\)~The result is stored in $r$.

\Statex creates a statement line without a line number and follows the current pseudocode indentation. It is useful for explanatory notes that should not be aligned at the far right. You can define a small helper command in the preamble if you use this often:

\newcommand{\CommentLine}[1]{\Statex \(\triangleright\)~#1}

Then write \CommentLine{The result is stored in $r$.} on a line within the algorithmic environment. Unlike \Comment, this helper begins a new line, so put it on a separate source line after the statement it explains.

Common mistakes

  • Redefining the wrong command: For algpseudocode, redefine \algorithmiccomment, not \Comment. The latter is the user-facing command that calls the comment-formatting command.
  • Loading packages in the wrong order: Load algpseudocode before redefining \algorithmiccomment, so the command has already been defined.
  • Using \Comment for a separate line: \Comment is intended as an inline comment. Use \Statex for an unnumbered comment line.
  • Confusing pseudocode packages: algorithm2e has different commands and options. The redefinition shown here applies to algorithmicx with algpseudocode, not to algorithm2e.
  • Expecting \hfill to prevent collisions: It aligns the comment toward the right edge; it does not guarantee that long code and a long comment will fit on one line. Shorten the comment or place it on its own line when necessary.

References

  1. The algorithmicx package

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.