clrscode
can produce some really nice-looking algorithm texts, but if you have nested conditionals, things start to look ugly. clrscode
has the peculiar preference that the \Then
keyword at the end of an \If
statement should go on a second line, indented.
Not only is this not very compact (needed for a paper in two-column format!), it runs counter to the convention in most mainstream programming languages. If you try to place the \Then
at the end of the same line that the \If
is on, the package often overprints the \Then
on top of the conditional expression!
To be clear, you need to do this
\li \If $a = b$
\li \Then foo
but I would really like to do
\li \If $a = b$ \Then foo
or
\li \If $a = b$ \Then
\li foo
Furthermore, I would like to be able to line up \Else
statements with the preceding \If
(not the preceding \Then
!), and this does not seem to be possible, either.
Any pointers?
Best Answer
clrscode
sets the algorithm inside a box (lrbox
) of fixed with (using aminipage
environment), and aligns the algorithm content usingtabbing
. The following updates\Then
and\Else
(and\Until
) fits within your requirements (I think):Of course, more corrections in terms of the input is possible, depending on some clarification.
Also,
clrscode3e
exists which is a "third edition" update ofclrscode
(used in editions 1 and 2 of Introduction to Algorithms). This update, as stated in theclrscode3e
documentation contains a number of user-feedback improvements. Specific to the\If
-\Then
clauses (section Typesetting if statements, p 8):From
clrscode.sty
, here's the formal definition of all the keywords: