如何在 LaTeX 中的算法环境中添加注释?

Die*_*ego 4 algorithm latex

我正在使用 LaTeX 使用该包编写伪算法algorithm。我想以一种对齐的方式在代码上添加注释。以下几行是我可以做的,但注释不一致。我怎么做?

\begin{algorithm}[H]
\caption{}
\label{}
    \begin{tabbing}
     quad \=\quad \=\quad \kill
     \keyw{for} each a $\in$ A \keyw{do} \\
     \> command; \qquad \qquad $\blacktriangleright$ add text here \\
     \keyw{end} \\

\end{tabbing}
\end{algorithm}

The comments are like that:
 one comment here\\
               other here\\
     other here\\
Run Code Online (Sandbox Code Playgroud)

我该如何对齐它们?

Wer*_*ner 8

如果您要设置算法,请使用专用的伪代码设置包。这是使用algorithmicx's的一个algpseudocode

在此输入图像描述

\documentclass{article}

\usepackage{algorithm,algpseudocode}

\algnewcommand{\algorithmicforeach}{\textbf{for each}}
\algdef{SE}[FOR]{ForEach}{EndForEach}[1]
  {\algorithmicforeach\ #1\ \algorithmicdo}% \ForEach{#1}
  {\algorithmicend\ \algorithmicforeach}% \EndForEach

\begin{document}

\begin{algorithm}
  \caption{An algorithm}
  \begin{algorithmic}[1]
    \ForEach{$a \in A$}%
      \State command \algorithmiccomment{This is a comment}
      \State another command \algorithmiccomment{This is another comment}
    \EndForEach
  \end{algorithmic}
\end{algorithm}

\end{document}
Run Code Online (Sandbox Code Playgroud)

algpseudocode已经定义了\ForAll. 但是,在上面的代码中,我将该定义复制到了\ForEach. 可以使用 添加评论\algorithmiccomment。格式和位置可以修改。