Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am using the package algorithm2e. When using comments like this

    \tcp{not aligned comment}
    \If(\tcp*[h]{comment next to if}){constraint}{ 
         c  \tcp*[l]{bla}
         $d = \min \{c,e\}$ \tcp*[l]{minimum}


the result is

// not aligned comment
if constraint then // comment next to if 
   c; // bla
   d = min{c,e}; // minimum

I would like to have the comments aligned, that is

// not aligned comment
if constraint then       // comment next to if
   c;                    // bla
   d = min{c,e};         // minimum

I only found an answer for the package algorithmicx: Algpseudocode (algorithmicx) package comments.

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

This flushes them right padded to the longest comment so the \\ line up. It takes a couple of runs to get the measuring. I added some $ to avoid errors that were generated when I tried your MWE.

enter image description here


\def\tcp*[#1]#2{% only support one style for simplicity
\oldtcp*[r]{\leavevmode\hbox to \commentwd{\box0\hfill}}}

\expandafter\ifx\csname commentwd@\romannumeral\csname c@\algocf@float\endcsname\endcsname\relax\else
\global\commentwd\csname commentwd@\romannumeral\csname c@\algocf@float\endcsname\endcsname
\immediate\write\@auxout{\gdef\expandafter\string\csname commentwd@\romannumeral\csname c@\algocf@float\endcsname\endcsname{%

    c  \tcp*[l]{bla}
    $d = \min \{c,e\}$ \tcp*[l]{minimum}
share|improve this answer
Unfortunately, now a normal command like \tcp{comment} throws an error. Is it possible to also use regular comments? Edited my question. –  Jana Nov 20 '12 at 18:05
easiest is to change \def\tcp*[#1]#2 to \def\mytcp*[#1]#2 or some such then use \mytcp. –  David Carlisle Nov 20 '12 at 18:43
This does not work with if-statements, and the font is not the same. –  Jana Nov 20 '12 at 19:30
Ive never used the package just based on your mwe, so don't know about if statement markup, The code I posted does nothing with fonts, oh you might prefer to replace \box0 by #2 if the tcp command changes font. –  David Carlisle Nov 20 '12 at 20:33
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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