3

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:

\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage{algpseudocode}\parindent0mm
\algnewcommand{\LineComment}[1]{\State \(\triangleright\) #1}

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

Thanks!

1 Answer 1

2

try inserting \hfill

\algnewcommand{\LineComment}[1]{\State  \(\triangleright\) #1 \hfill~}
6
  • This keeps the triangle where it is and moves the actual comment to the right end of the line..
    – bogus
    Jan 23, 2013 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, 2013 at 7:52
  • put the \hfill before the triangle, then the \State should be left and the comment right aligned. See my edit
    – user2478
    Jan 23, 2013 at 8:13
  • this pushes the comment to the right, not to the left
    – bogus
    Jan 23, 2013 at 8:37
  • see edit, but I need a complete example to see what you resally want
    – user2478
    Jan 23, 2013 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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