I want to know how can I have the nice ▷ $\triangleright$ comment symbol of algorithmicx in algorithm2e?


From the algorithm2e documentation (section 11.4 to define comments, p 35):

\SetKwComment{Comment}{<start>}{<end>} defines a macro \Comment{text comment} which writes text comment between <start> and <end>. Note that <start> or <end> can be empty.

It defines also \Comment*{side comment text} macro which allows to put comment on the same line as the code. This macro can take various option to control its behaviour:

  • \Comment*[r]{side comment text} put the end of line mark (; by default) and side comment text just after and right justified, then end the line. It is the default.

  • \Comment*[l]{side comment text} same thing but side comment text is left justified.

  • \Comment*[h]{side comment text} put side comment right after the text. No end of line mark is put, and line is not terminated (is up to you to put \; to end the line).

  • \Comment*[f]{side comment text} same as the previous one but with side comment text right justified.

Here's an example of the above:

enter image description here

\SetKwComment{Comment}{$\triangleright$\ }{}

  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e }
  \While{not at end of this document}{
    read current\;
      go to next section \Comment*[r]{Some comment}
      current section becomes this one\;
      go back to the beginning of current section\;
  \caption{How to write algorithms}


If you wish to reformat the comment font, you can adjust \CommentSty. In algorithm2e this is done via (for example) \SetCommentSty{itshape} to obtain an \itshape or italics comment.

Here is a corresponding example showing the default comment style in algorithmicx, taken directly from the algorithmicx documentation:

enter image description here


  \caption{Euclid’s algorithm}\label{euclid}
    \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
      \State $r\gets a\bmod b$
      \While{$r\not=0$}\Comment{We have the answer if r is 0}
        \State $a\gets b$
        \State $b\gets r$
        \State $r\gets a\bmod b$
      \State \textbf{return} $b$\Comment{The gcd is b}

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.