8

I'm trying to right align comments in a pseudocode environment using the algorithmic package. So I renewed the algorithmiccomment command.

There's a different way to create comments for "normal" statements and for pre-defined statements such as "for", "if", etc.

The problem is, that the right alignment differs for the "normal" comments and the others. How can that be fixed, so that all comments nicely align?

Here's what I mean.

Comments not really aligned well

And this is the code to reproduce my example. Any help is greatly appreciated.

\documentclass[a4paper]{article}
\usepackage{algorithmic}
\renewcommand{\algorithmiccomment}[1]{\hfill \tiny//~#1\normalsize}

\begin{document}
\begin{algorithmic}
\FORALL[This comment is not quite right aligned]{ $a \in B$}
    \STATE X  \COMMENT{This comment is further to the right}
\ENDFOR
\end{algorithmic}
\end{document}
4
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Nov 26, 2013 at 10:38
  • Thanks. I accepted your answer. But your "starter guide" link is broken.
    – Curt
    Nov 26, 2013 at 11:03
  • Wait. Now it works =)
    – Curt
    Nov 26, 2013 at 11:04
  • Glad to help. The "welcome" message is a standard one we use over here :-) Nov 26, 2013 at 11:07

1 Answer 1

5

If you don't mind to switch to the algorithmicx package (algpseudocode variant) the issue is solved.

Note that I've loaded it with the compatible option so you can use your old algorithms without any change.

\documentclass[a4paper]{article}
\usepackage[compatible]{algpseudocode} % or \usepackage{algcompatible}
\renewcommand{\algorithmiccomment}[1]{\bgroup\hfill\tiny//~#1\egroup}

\begin{document}
\begin{algorithmic}
\FORALL[This comment is not quite right aligned]{ $a \in B$}
    \STATE X  \COMMENT{This comment is further to the right}
\ENDFOR
\end{algorithmic}
\end{document} 

Output

enter image description here

And this is how it should be written in "proper" algorithmicx syntax:

\documentclass[a4paper]{article}
\usepackage{algpseudocode}
\renewcommand{\algorithmiccomment}[1]{\bgroup\hfill\tiny//~#1\egroup}

\begin{document}
\begin{algorithmic}
\ForAll{ $a \in B$} \Comment{This comment is not quite right aligned}
    \State X \Comment{This comment is further to the right}
\EndFor
\end{algorithmic}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .