English 中文(简体)
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}

enewcommand{algorithmiccomment}[1]{hfilleqparbox{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}

enewcommandalgorithmiccomment[1]{%
  hfill# eqparbox{COMMENT}{#1}%
}

ewcommandLONGCOMMENT[1]{%
  hfill# egin{minipage}[t]{eqboxwidth{COMMENT}}#1strutend{minipage}%
}
egin{document}
egin{algorithmic} 
STATE do nothing COMMENT{huh?} 
end{algorithmic}
egin{algorithmic} 
STATE do something LONGCOMMENT{this is a comment broken over lines} 
end{algorithmic}
egin{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	healgorithm}{#1}}
algnewcommand{LongComment}[1]{hfill// egin{minipage}[t]{eqboxwidth{COMMENT	healgorithm}}#1strutend{minipage}}

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

egin{algorithm}
egin{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}




相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签