8

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

\documentclass[11pt,a4paper,twoside,openright]{book}
\usepackage[algochapter,linesnumbered,ruled,lined,boxed]{algorithm2e}
\begin{document}
\begin{algorithm}
    \tcp{not aligned comment}
    \If(\tcp*[h]{comment next to if}){constraint}{ 
         c  \tcp*[l]{bla}
         $d = \min \{c,e\}$ \tcp*[l]{minimum}
    }      

\end{algorithm}
\end{document}

the result is

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

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
end

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

0

2 Answers 2

3

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

\documentclass{article}
\usepackage{algorithm2e}

\makeatletter
\newdimen\commentwd
\let\oldtcp\tcp
\def\tcp*[#1]#2{% only support one style for simplicity
\setbox0\hbox{#2}%
\ifdim\wd\z@>\commentwd\global\commentwd\wd\z@\fi
\oldtcp*[r]{\leavevmode\hbox to \commentwd{\box0\hfill}}}

\let\oldalgorithm\algorithm
\def\algorithm{\oldalgorithm
\global\commentwd\z@
\expandafter\ifx\csname commentwd@\romannumeral\csname c@\algocf@float\endcsname\endcsname\relax\else
\global\commentwd\csname commentwd@\romannumeral\csname c@\algocf@float\endcsname\endcsname
\fi
}
\let\oldendalgorithm\endalgorithm
\def\endalgorithm{\oldendalgorithm
\immediate\write\@auxout{\gdef\expandafter\string\csname commentwd@\romannumeral\csname c@\algocf@float\endcsname\endcsname{%
\the\commentwd}}}

\begin{document}
\begin{algorithm}
    c  \tcp*[l]{bla}
    $d = \min \{c,e\}$ \tcp*[l]{minimum}
\end{algorithm}
\end{document}
4
  • Unfortunately, now a normal command like \tcp{comment} throws an error. Is it possible to also use regular comments? Edited my question. Commented Nov 20, 2012 at 18:05
  • easiest is to change \def\tcp*[#1]#2 to \def\mytcp*[#1]#2 or some such then use \mytcp. Commented Nov 20, 2012 at 18:43
  • This does not work with if-statements, and the font is not the same. Commented Nov 20, 2012 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. Commented Nov 20, 2012 at 20:33
6

Here's a solution I'm using with algorithm2e. The idea is to use right-aligned comments (\tcp*[r]), and put them in a box of fixed length \commentWidth. The important stuff is wrapped in an "aligned comment" macro \atcp for ease of use.

\documentclass[11pt,a4paper,twoside,openright]{book}
\usepackage[algochapter,linesnumbered,ruled,lined,boxed]{algorithm2e}
\begin{document}

\newlength{\commentWidth}
\setlength{\commentWidth}{7cm}
\newcommand{\atcp}[1]{\tcp*[r]{\makebox[\commentWidth]{#1\hfill}}}

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

\end{algorithm}
\end{document}

This results in:

n ima

I do not know how to align comments after if-style blocks, though.

3
  • You can align the if-style comments by using your own \atcp function instead of \tcp*. Commented Jun 17, 2020 at 12:25
  • 1
    This is perfect! Note that the [r] option after \tcp* will automatically insert a newline; So, either omit \; from lines ending with the comment, or remove [r], to avoid double-newlines. Commented Oct 19, 2022 at 17:56
  • 1
    This is awesome! Do you have any idea how you might go about having the comment text linebreak to a new comment if it would otherwise spill over the comment width? I'd love to be able to just type one giant comment and have it automatically pop out on multiple lines which are vertically aligned like this. Not sure if this is a valid use of the comments section here, so I do apologize if this is not the right place to ask! Commented Jan 16 at 15:04

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.