如何在 algorithmicx 伪代码中缩进长注释延续

如何在 algorithmicx 伪代码中缩进长注释延续

在 MWE 中,我尝试在给出的答案中添加行延续缩进在 algorithmicx 中正确缩进没有行号的注释

例如我正在尝试写\LineCommentCont

我正在使用该linegoal包来自动设置宽度参数\parbox,并\hangindent缩进延续。

我有两个问题:1)\linegoal似乎返回的空间比可用空间少一点。2)我应该如何分配正确的\hangindent数量?

或者,您可以用完全不同的方法提供答案。

\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx

\usepackage{linegoal}

\makeatletter
\algnewcommand{\LineComment}[1]{\Statex \hskip\ALG@thistlm \(\triangleright\) #1}
\algnewcommand{\LineCommentCont}[1]{\Statex \hskip\ALG@thistlm \parbox[t]{\linegoal}{\hangindent=1em\hangafter=1 $\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. no need to explore more. we just want to stop over here. no need to explore more. we just want to stop over here. }
          \LineCommentCont{no need to explore more. we just want to stop over here. no  to explore more. we just want to stop over here. 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}

algorithmicx 中的注释行延续

答案1

使用以下定义\LineCommentCont

在此处输入图片描述

\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx

\makeatletter
\newlength{\trianglerightwidth}
\settowidth{\trianglerightwidth}{$\triangleright$~}
\algnewcommand{\LineComment}[1]{\Statex \hskip\ALG@thistlm $\triangleright$ #1}
\algnewcommand{\LineCommentCont}[1]{\Statex \hskip\ALG@thistlm%
  \parbox[t]{\dimexpr\linewidth-\ALG@thistlm}{\hangindent=\trianglerightwidth \hangafter=1 \strut$\triangleright$ #1\strut}}
\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. no need to explore more. we just want to stop over here. no need to explore more. we just want to stop over here. }
          \LineCommentCont{no need to explore more. we just want to stop over here. no  to explore more. we just want to stop over here. 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}

由于你正在缩进“列表”(算法),因此\ALG@thistlm你只需要从中减去它就\linewidth可以得到正确的宽度\parbox。因此不需要使用linegoal确定线的剩余宽度。

宽度\trianglerightwidth是通过测量的宽度获得的$\trianglewidth$~

我在宏中添加了一些\struts,以避免在您的注释中没有下降符时出现行高问题。


每一级的缩进都会增加\algorithmicindent(默认为1.5em)。使用

\algnewcommand{\LineCommentCont}[1]{\Statex \hskip\ALG@thistlm%
  \parbox[t]{\dimexpr\linewidth-\ALG@thistlm}{\hangindent=\algorithmicindent \hangafter=1 \strut\makebox[\algorithmicindent][l]{$\triangleright$}#1\strut}}

插入与其他结构相同间距的缩进:

在此处输入图片描述

答案2

那简单吗

\algnewcommand{\LineCommentCont}[1]{\State \phantom{$\triangleright$} #1}

相关内容