Algorithm 如何在LaTeX中添加对算法环境的注释?
我正在使用LaTeX编写一个伪算法,使用Algorithm 如何在LaTeX中添加对算法环境的注释?,algorithm,latex,Algorithm,Latex,我正在使用LaTeX编写一个伪算法,使用算法包。我想在代码上添加注释,使它们保持一致。下面几行是我可以做的,但是注释没有对齐。我该怎么做 \begin{algorithm}[H] \caption{} \label{} \begin{tabbing} quad \=\quad \=\quad \kill \keyw{for} each a $\in$ A \keyw{do} \\ \> command; \qquad \qquad $\blacktri
算法
包。我想在代码上添加注释,使它们保持一致。下面几行是我可以做的,但是注释没有对齐。我该怎么做
\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\\
如何对齐它们?如果要设置算法,请使用专用的伪代码设置包。这里有一个使用的是's
algpseudocode
:
algpseudocode
已经定义了\ForAll
。但是,在上面的代码中,我将该定义复制到\ForEach
中。可以使用\algorithmicommment
添加注释。可以修改格式和位置。如果要设置算法,请使用专用的伪代码设置包。这里有一个使用的是'salgpseudocode
:
algpseudocode
已经定义了\ForAll
。但是,在上面的代码中,我将该定义复制到\ForEach
中。可以使用\algorithmicommment
添加注释。格式和位置可以修改。请将此问题发布到。@NicoSchertler:投票关闭(带有迁移建议)并建议将其发布到目标站点并没有帮助。投票通过迁移关闭,或投票通过主题外的方式关闭并建议重新发布。请将此问题发布到。@NicoSchertler:投票关闭(带有迁移建议)并建议将其发布到目标站点无助。投票以迁移结束,或投票以主题外结束,并建议重新发布。
\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}