在 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}
答案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$~
。
我在宏中添加了一些\strut
s,以避免在您的注释中没有下降符时出现行高问题。
每一级的缩进都会增加\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}