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

Writing comments in pseudocode, I wanted to insert comments which are not aligned to the right side of the line which is commented. This thread helped me a lot.

However, I want comments to stand above the code line to which they refer and be always left-aligned.

Reason: If the comment refers to an elsif, then the comment is inside the previous if-block. Thus, it is indented. I don't want it to be indented, I want it to be at the same horizontal position as the corresponding line is.

Starting with this

\algnewcommand{\LineComment}[1]{\State \(\triangleright\) #1}

How can I design it to be aligned corresponding to the next line (which is left-aligned in 90% of the cases)?

I am using this:

\algnewcommand{\LineComment}[1]{\State \(\triangleright\) #1}

Also, I want to define a new command, not redefine an existing one.


share|improve this question

try inserting \hfill

\algnewcommand{\LineComment}[1]{\State  \(\triangleright\) #1 \hfill~}
share|improve this answer
This keeps the triangle where it is and moves the actual comment to the right end of the line.. – bogus Jan 23 '13 at 7:51
I just noticed that the problem description is not quite accurate: My comments are supposed to be left aligned, sorry. I updated the description – bogus Jan 23 '13 at 7:52
put the \hfill before the triangle, then the \State should be left and the comment right aligned. See my edit – Herbert Jan 23 '13 at 8:13
this pushes the comment to the right, not to the left – bogus Jan 23 '13 at 8:37
see edit, but I need a complete example to see what you resally want – Herbert Jan 23 '13 at 8:50

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.