Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question

1 Answer 1

up vote 6 down vote accepted

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}
share|improve this answer

Your Answer

 
discard

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.