TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using algorihtm2e and I am formatting a new command to align comments in the algorithms like in the answer of Align comments in algorithm with package algorithm2e. Now when I try to use positions options like this


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

\expandafter\ifx\csname commentwd@\romannumeral\csname c@\algocf@float 
\global\commentwd\csname commentwd@\romannumeral\csname c@\algocf@float
commentwd@\romannumeral\csname c@\algocf@float\endcsname\endcsname{%


The result is that [ht] is written in the first line of the algorithm. When I don't use that, it works. Does anyone have an idea how I can align the comments and use positioning?

share|improve this question
Your code said algorihm not algorithm. Even with the corrected environment name, I don't get your results. It seems to work fine. – A.Ellett Nov 25 '12 at 15:02
up vote 1 down vote accepted

Your redefinition of algorithm is causing the problem. Use the following instead:

\expandafter\ifx\csname commentwd@\romannumeral\csname c@\algocf@float 
\global\commentwd\csname commentwd@\romannumeral\csname c@\algocf@float

Since the environment algorithm takes an optional argument as part of its definition, it "checks ahead" to see if you follow \algorithm with [ or not. You've placed the older definition \oldalgorithm at the start of the newly redefined \algorithm, meaning it is literally followed by \global, which is not [. Placing \oldalgorithm at the end of \algorithm allows scanning to pick up [ in the input stream correctly.

share|improve this answer

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.