在LaTeX的算法环境中格式化注释

时间:2021-03-04 06:12:50

I want to typeset an algorithm in LaTeX. I'm using the algorithmic package and environment to do so. Everything is working great except when I add comments (using \COMMENT), they are output immediately after the statements. I would like for all the comments to be aligned (and offset from the statements). Is there an easy way to do so?

我想在LaTeX中键入一个算法。我使用的是算法包和环境。除了添加注释(使用\注释)之外,一切都运行得很好,它们是在语句之后立即输出的。我希望所有的注释都对齐(并从语句中抵消)。有简单的方法吗?

"Reproducing" the PDF output in HTML's pre, I want:

“复制”PDF输出在HTML的pre,我想:

if condition then
   something         # comment 1
else
   something else    # comment 2

rather than

而不是

if condition then
   something  # comment 1
else
   something else  # comment 2

3 个解决方案

#1


11  

I would do it like this:

我会这样做:

\usepackage{eqparbox}
\renewcommand{\algorithmiccomment}[1]{\hfill\eqparbox{COMMENT}{\# #1}}

Note 1: two document compilations are necessary to determine the maximum width of the comment.

注意1:需要两个文档编译才能确定注释的最大宽度。

Note 2: obviously, this only works for single line comments that aren't too long.

注意2:显然,这只适用于不太长的单行注释。


Following on from this idea, here's a complete example in the same sort of way, but also providing a command to have comments that break over lines:

根据这个想法,这里有一个完整的例子,用同样的方式,但也提供了一个命令,让注释可以跨行:

\documentclass{amsbook}
\usepackage{algorithmic,eqparbox,array}
\renewcommand\algorithmiccomment[1]{%
  \hfill\#\ \eqparbox{COMMENT}{#1}%
}
\newcommand\LONGCOMMENT[1]{%
  \hfill\#\ \begin{minipage}[t]{\eqboxwidth{COMMENT}}#1\strut\end{minipage}%
}
\begin{document}
\begin{algorithmic} 
\STATE do nothing \COMMENT{huh?} 
\end{algorithmic}
\begin{algorithmic} 
\STATE do something \LONGCOMMENT{this is a comment broken over lines} 
\end{algorithmic}
\begin{algorithmic} 
\STATE do something else \COMMENT{this is another comment} 
\end{algorithmic}
\end{document}

#2


0  

if condition then
   something        \hspace{2in} # comment 1
else
   something else   \hfill # comment 2

I'm not sure if the hspace and hfill will work inside an environment. I assume that they will. \hfill will set the comments flush right, while \hspace{space} will give you that much space between your text. good luck.

我不确定hspace和hfill能否在环境中工作。我想他们会的。\hfill将设置注释刷新为正确,而\hspace{space}将为您的文本提供足够的空间。祝你好运。

#3


0  

If you want own indentions for different algorithms, you could do this by including the counter in the redefinition of the comment commands. Here is an example:

如果您希望为不同的算法提供自己的缩进,您可以在注释命令的重新定义中包含计数器。这是一个例子:

\documentclass{amsbook}
\usepackage{algorithmicx,algorithm,eqparbox,array}

\algrenewcommand{\algorithmiccomment}[1]{\hfill// \eqparbox{COMMENT\thealgorithm}{#1}}
\algnewcommand{\LongComment}[1]{\hfill// \begin{minipage}[t]{\eqboxwidth{COMMENT\thealgorithm}}#1\strut\end{minipage}}

\begin{document}
\begin{algorithm}
\begin{algorithmic}
\State{do nothing}\Comment{huh?}
\end{algorithmic}
\caption{Test Alg}
\end{algorithm}

\begin{algorithm}
\begin{algorithmic}
\State{do something} \LongComment{this is a comment broken over lines}
\State{do something else} \Comment{this is another comment}
\end{algorithmic}
\caption{Other Alg}
\end{algorithm}
\end{document}

#1


11  

I would do it like this:

我会这样做:

\usepackage{eqparbox}
\renewcommand{\algorithmiccomment}[1]{\hfill\eqparbox{COMMENT}{\# #1}}

Note 1: two document compilations are necessary to determine the maximum width of the comment.

注意1:需要两个文档编译才能确定注释的最大宽度。

Note 2: obviously, this only works for single line comments that aren't too long.

注意2:显然,这只适用于不太长的单行注释。


Following on from this idea, here's a complete example in the same sort of way, but also providing a command to have comments that break over lines:

根据这个想法,这里有一个完整的例子,用同样的方式,但也提供了一个命令,让注释可以跨行:

\documentclass{amsbook}
\usepackage{algorithmic,eqparbox,array}
\renewcommand\algorithmiccomment[1]{%
  \hfill\#\ \eqparbox{COMMENT}{#1}%
}
\newcommand\LONGCOMMENT[1]{%
  \hfill\#\ \begin{minipage}[t]{\eqboxwidth{COMMENT}}#1\strut\end{minipage}%
}
\begin{document}
\begin{algorithmic} 
\STATE do nothing \COMMENT{huh?} 
\end{algorithmic}
\begin{algorithmic} 
\STATE do something \LONGCOMMENT{this is a comment broken over lines} 
\end{algorithmic}
\begin{algorithmic} 
\STATE do something else \COMMENT{this is another comment} 
\end{algorithmic}
\end{document}

#2


0  

if condition then
   something        \hspace{2in} # comment 1
else
   something else   \hfill # comment 2

I'm not sure if the hspace and hfill will work inside an environment. I assume that they will. \hfill will set the comments flush right, while \hspace{space} will give you that much space between your text. good luck.

我不确定hspace和hfill能否在环境中工作。我想他们会的。\hfill将设置注释刷新为正确,而\hspace{space}将为您的文本提供足够的空间。祝你好运。

#3


0  

If you want own indentions for different algorithms, you could do this by including the counter in the redefinition of the comment commands. Here is an example:

如果您希望为不同的算法提供自己的缩进,您可以在注释命令的重新定义中包含计数器。这是一个例子:

\documentclass{amsbook}
\usepackage{algorithmicx,algorithm,eqparbox,array}

\algrenewcommand{\algorithmiccomment}[1]{\hfill// \eqparbox{COMMENT\thealgorithm}{#1}}
\algnewcommand{\LongComment}[1]{\hfill// \begin{minipage}[t]{\eqboxwidth{COMMENT\thealgorithm}}#1\strut\end{minipage}}

\begin{document}
\begin{algorithm}
\begin{algorithmic}
\State{do nothing}\Comment{huh?}
\end{algorithmic}
\caption{Test Alg}
\end{algorithm}

\begin{algorithm}
\begin{algorithmic}
\State{do something} \LongComment{this is a comment broken over lines}
\State{do something else} \Comment{this is another comment}
\end{algorithmic}
\caption{Other Alg}
\end{algorithm}
\end{document}