algorithmicx 包在一行上进行注释

algorithmicx 包在一行上进行注释

包中的注释有可能algorithmicx不与右侧对齐吗?

例如我有这样的代码-

\begin{algorithm}[!ht]
\caption{My Algo.}
\label{myalgo}
\begin{algorithmic}

\State $\epsilon$ = 1.0;

\Comment{Explore Latency Dimension}
\While {explorationTime <= timeLimit}
    \State $\epsilon$ = $\epsilon$ / 2;
    \State calculateIncrements($\epsilon$);

    \Comment{Explore L dimension}

    \While {lQuery <= lUpperLimit}
         \State Query (0, Query, bQuery, pQuery);
         \If {result = WORKING}
             \State mark points 
             \Comment{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}

所以发生的事情是,包每次都会将注释对齐到右侧。但是对于这个注释\Comment{no need to explore more. we just want to stop over here.},我希望它位于一行而不是多行上,并且对齐到右侧。这对我来说有点令人困惑。

我们是否可以提出如下评论 -

> no need to explore more. we just want to stop over here.
Break

它应该与语句的缩进级别对齐。

答案1

可以使用以下方法修改注释宏\algrenewcomment,例如

\algrenewcomment[1]{\(\triangleright\) #1}

原始\Comment命令插入了一个\hfill,我已在上面将其删除。这将\Comment全局替换现有命令。但是,您也可以定义自己的(新)\LineComment命令,

\algnewcommand{\LineComment}[1]{\State \(\triangleright\) #1}

并将其与常规混合\Comment,就像我下面所做的那样:

在此处输入图片描述

\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\algnewcommand{\LineComment}[1]{\State \(\triangleright\) #1}
\begin{document}
\begin{algorithm}[!ht]
  \caption{My Algo.}\label{myalgo}
  \begin{algorithmic}
    \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}

相关内容