简体   繁体   中英

Formatting comments in LaTeX's algorithmic environment

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?

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

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

rather than

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

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.

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


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

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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM