Algpseudocode(algorithmicx)包注释

Algpseudocode(algorithmicx)包注释

如何将algorithmic环境中的注释右对齐?例如\Comment,如果注释太长,它会被换行并从下一行的开头开始。如果它与右侧对齐,看起来会更好。

\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}

答案1

将注释包装在特定宽度的top-aligned内\parbox。考虑以下示例:

在此处输入图片描述

\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}

相关内容