用伪代码写注释,我想将注释插入到注释行的右侧。此主题对我帮助很大。
该算法是行号的,但是我需要没有数字的注释!(就像常规\comment
命令一样),但因为宏\State
中包含命令,所以它实际上是带有数字的正常行。
\algnewcommand{\LineComment}[1]{\State \(\triangleright\) #1}
提前致谢!
答案1
以下定义\LineComment
提供了适当的缩进没有给行编号:
\makeatletter
\algnewcommand{\LineComment}[1]{\Statex \hskip\ALG@thistlm \(\triangleright\) #1}
\makeatother
\Statex
设置一个\item[]
(请注意,该算法实际上是一个列表,因此\item[]
设置一个空项,因此没有行号),同时\ALG@thistlm
包含当前列表项的适当缩进。
\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\makeatletter
\algnewcommand{\LineComment}[1]{\Statex \hskip\ALG@thistlm \(\triangleright\) #1}
\makeatother
\begin{document}
\begin{algorithm}[!ht]
\caption{My Algo.}\label{myalgo}
\begin{algorithmic}[1]
\State $\epsilon$ = 1.0; \Comment{Explore Latency Dimension}
\While {explorationTime $\leq$ timeLimit}
\State $\epsilon = \epsilon / 2$;
\State calculateIncrements($\epsilon$);
\LineComment{Explore L dimension}
\While {lQuery $\leq$ lUpperLimit}
\State Query (0, Query, bQuery, pQuery);
\If {result = WORKING}
\State mark points
\LineComment{no need to explore more. we just want to stop over here.}
\State Break
\Else
\If {result = NOT WORKING}
\State mark from 0 to lQuery as NOT WORKING.
\EndIf
\EndIf
\State lQuery += lEpsIncr;
\EndWhile
\EndWhile
\State calcPoints()
\end{algorithmic}
\end{algorithm}
\end{document}