我如何在算法中左对齐我的评论。
我正在使用algorithmicx
包。
我想要的是:将评论拉到左边,如所附的快照所示。
我的算法的代码是
\documentclass[a4paper]{article}
\pagestyle{empty}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage[caption=false,font=footnotesize]{subfig}
\begin{document}
\begin{algorithm} \label{algoQ}
\caption{Algorithm to check quality of methods}
\begin{algorithmic}
\For{\textbf{each} $msd$ in$msdList$} \Comment { \% for each MSD \%}
\Statex \Comment { \% get MSDs referenced method service \%}
\EndFor \textbf{endFor}
\end{algorithmic}
\end{algorithm}
\end{document}
答案1
您可以定义自己的\LeftComment
不插入默认值\hfill
:
\documentclass{article}
\usepackage{algorithm,algpseudocode}
\algnewcommand{\LeftComment}[1]{\Statex \(\triangleright\) #1}
\begin{document}
\begin{algorithm}
\caption{Algorithm to check quality of methods}
\begin{algorithmic}
\For{\textbf{each} msd $\in$ msdList} \Comment { \% for each MSD \%}
\Statex \Comment { \% get MSDs referenced method service \%}
\LeftComment { \% get MSDs referenced method service \%}
\EndFor
\end{algorithmic}
\end{algorithm}
\end{document}
这是带有星号版本的修改版本\LeftComment
。星号版本会打印左对齐注释,而默认(无星号)版本会将其设置为在当前组的缩进级别左对齐:
\documentclass{article}
\usepackage{algorithm,algpseudocode}
\usepackage{xparse}
\makeatletter
\NewDocumentCommand{\LeftComment}{s m}{%
\Statex \IfBooleanF{#1}{\hspace*{\ALG@thistlm}}\(\triangleright\) #2}
\makeatother
\begin{document}
\begin{algorithm}
\caption{Algorithm to check quality of methods}
\begin{algorithmic}
\For{\textbf{each} msd $\in$ msdList} \Comment { \% for each MSD \%}
\Statex \Comment { \% get MSDs referenced method service \%}
\LeftComment { \% get MSDs referenced method service \%}
\LeftComment*{ \% get MSDs referenced method service \%}
\For{\textbf{each} msd $\in$ msdList} \Comment { \% for each MSD \%}
\Statex \Comment { \% get MSDs referenced method service \%}
\LeftComment { \% get MSDs referenced method service \%}
\LeftComment*{ \% get MSDs referenced method service \%}
\EndFor
\EndFor
\end{algorithmic}
\end{algorithm}
\end{document}
答案2
这是嵌套循环问题的替代方案。由于默认算法缩进为 1.5em。新尺寸algindent
设置为 1.5em,以便可以将其乘以 1.5em,并且命令\LeftComment{#1}{#2}
需要两个输入:#1=嵌套缩进的数量,#2=注释文本,如下所示
\algnewcommand\LeftComment[2]{%
\hspace{#1\algindent}$\triangleright$ \eqparbox{COMMENT}{#2} \hfill %
}
代码
\documentclass[a4paper]{article}
\pagestyle{empty}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage[caption=false,font=footnotesize]{subfig}
\usepackage{eqparbox}
\newdimen{\algindent}
\setlength\algindent{1.5em} % algorithmic indent=1.5em
\algnewcommand\LeftComment[2]{%
\hspace{#1\algindent}$\triangleright$ \eqparbox{COMMENT}{#2} \hfill %
}
\begin{document}
\begin{algorithm} \label{algoQ}
\caption{Algorithm to check quality of methods}
\begin{algorithmic}
\For{\textbf{each} $msd$ in$msdList$}
\Statex \LeftComment{1} { \% first for loop service \%}
\For{\textbf{each} $msd$ in$msdList$} \Comment { \% for each MSD \%}
\Statex \LeftComment{2} { \% second nested for loop service\%}
\For{\textbf{each} $msd$ in$msdList$} \Comment { \% for each MSD \%}
\Statex \LeftComment{2} { \% third nested for loop service\%}
\EndFor \textbf{endFor}
\Statex \LeftComment{2} { \% second nested for loop service\%}
\EndFor \textbf{endFor}
\Statex \LeftComment{1} { \% first for loop service\%}
\EndFor \textbf{endFor}
\end{algorithmic}
\end{algorithm}
\end{document}