20

How to align comments in algorithmic environment to the right? For \Comment, if the comment is too long it gets wrapped and starts at the beginning of next line. It would look better if it was aligned to the right side.

\documentclass{report}
\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}
\begin{algorithm}
\begin{algorithmic}
\Function{foo}{x}
\State $x \gets 1$ \Comment{very long comment}
\EndFunction
\end{algorithmic}
\end{algorithm}
\end{document}
0

1 Answer 1

25

Wrap the comment inside a top-aligned \parbox of specific width. Consider the following example:

enter image description here

\documentclass{report}
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\begin{document}
\begin{algorithm}
  \begin{algorithmic}
    \Function{foo}{x}
      \State $x \gets 1$ \Comment{very long comment 
        very long comment very long comment very long comment very long comment
        very long comment very long comment very long comment very long comment
        very long comment very long comment very long comment very long comment
        very long comment very long comment very long comment very long comment.}
      \State $x \gets 1$ \Comment{\parbox[t]{.5\linewidth}{very long comment 
        very long comment very long comment very long comment very long comment
        very long comment very long comment very long comment very long comment
        very long comment very long comment very long comment very long comment
        very long comment very long comment very long comment very long comment.}}
    \EndFunction
  \end{algorithmic}
\end{algorithm}
\end{document}
1
  • 3
    Is there a way to make it automatically use the width available on the first line? So that you don't have to set 0.5\linewidth and so that it doesn't leave so much space between the equation and the comment?
    – m4p85r
    Commented Jul 30, 2018 at 5:21

You must log in to answer this question.

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