我需要抑制算法中\For
循环和语句的行号。我在网上找到了可以完成这项工作的命令,但不知何故,它弄乱了我的下一个语句的缩进。我还发现,不知何故语句和文本之间没有空格。我附上了问题和相关代码的屏幕截图。有什么想法吗?\If
\skipnumber
\State
\Else
\documentclass[12pt]{report}
\usepackage[chapter]{algorithm}
\usepackage[noend]{algpseudocode}
\makeatletter
\newcommand*{\skipnumber}[2][1]{%
{\renewcommand*{\alglinenumber}[1]{}\State #2}%
\addtocounter{ALG@line}{-#1}}
\makeatother
\begin{document}
\begin{algorithm}[h!]
\begin{algorithmic}[1]
%SOME OTHER STUFF HERE
\State \textbf{Apply threshold of $\pm t$ around centre gradient value ($G_c$) in a $3\times3$ neighbourhood to determine $S_{GLTP}$ codes for the image}
\skipnumber[5]{\ForAll{gradient magnitude value ($G_i$) around $G_c$}
\If{$G_i > G_c + t$} $S_{GLTP}(i)\gets+1$
\ElsIf{$G_i < G_c - t$} $S_{GLTP}(i)\gets-1$
\Else $S_{GLTP}(i)\gets 0$
\EndIf
\EndFor}
\Statex \textbf{repeat} for each $3\times3$ neighbourhood
\Statex
\State \textbf{Compute positive ($P_{GLTP}$) and negative ($N_{GLTP}$) $GLTP$ coded image representations from $S_{GLTP}$ values}
\skipnumber[5]{\ForAll{$S_{GLTP}(i)$ value in a $3\times3$ neighbourhood}
\If {$S_{GLTP}(i)>0$} $S_P(i)\gets1$ \& $S_N(i)\gets0$
\ElsIf {$S_{GLTP}(i)<0$} $S_P(i)\gets0$ \& $S_N(i)\gets1$
\Else $S_P(i)\gets0$ \& $S_N(i)\gets0$ \EndIf
\EndFor}
\Statex $P_{GLTP}= \sum_{i=0}^{7} S_P(i)\times 2^i$
\Statex $N_{GLTP}= \sum_{i=0}^{7} S_N(i)\times 2^i$
\Statex \textbf{repeat} for each $3\times3$ neighbourhood
\end{algorithmic}
\end{algorithm}
\end{document}
答案1
首先,\ForAll
在algpseudocode
作为具有 tart 的块结构S
,但没有E
nd:
\algdef{S}[FOR]{ForAll}[1]{\algorithmicforall\ #1\ \algorithmicdo}%
这一点很重要,因为它会影响缩进。因此,我们重新定义它以使其具有E
nd:
\algdef{SE}% Start and End
[FOR]% New block
{ForAll}% Creates \ForAll{<stuff>}
{EndForAll}% Creates \EndForAll
[1]% One argument passed to \ForAll
{\algorithmicforall\ #1\ \algorithmicdo}% Text for \ForAll
{\algorithmicend\ \algorithmicforall}% Text for \EndForAll
\algtext*{EndForAll}% Remove text associated with \EndForAll
并删除E
nd 文本(因为您使用了noend
package 选项)。上述组合类似于
\algdef{SN}[FOR]{ForAll}{EndForAll}[1]{\algorithmicforall\ #1\ \algorithmicdo}%
接下来,我重新定义宏的工作方式\skipnumber
。我们不再提供数字,而是定义两个开关 -\stopnumbering
和\resumenumbering
- 来停用和重新启用行号。我认为这样可以让代码更易读。
\documentclass{article}
\usepackage{algorithm,amsmath}
\usepackage[noend]{algpseudocode}
\let\oldState\State
\newcommand*{\stopnumbering}{%
\let\olditem\item
\renewcommand{\item}[1][]{\olditem[]}%
\let\State\Statex}
\newcommand*{\resumenumbering}{%
\let\item\olditem
\let\State\oldState}
\algdef{SE}[FOR]{ForAll}{EndForAll}[1]{\algorithmicforall\ #1\ \algorithmicdo}{\algorithmicend\ \algorithmicforall}%
\algtext*{EndForAll}
\begin{document}
\begin{algorithm}[h!]
\begin{algorithmic}[1]
%SOME OTHER STUFF HERE
\State \textbf{Apply threshold of $\pm t$ around centre gradient value ($G_c$) in
a $3\times3$ neighbourhood to determine $S_{\text{GLTP}}$ codes for the image}
\stopnumbering
\ForAll{gradient magnitude value ($G_i$) around $G_c$}
\If{$G_i > G_c + t$} $S_{\text{GLTP}}(i) \gets +1$
\ElsIf{$G_i < G_c - t$} $S_{\text{GLTP}}(i) \gets -1$
\Else{} $S_{\text{GLTP}}(i) \gets 0$
\EndIf
\EndForAll
\Statex \textbf{repeat} for each $3 \times 3$ neighbourhood
\resumenumbering
\State \textbf{Compute positive ($P_{\text{GLTP}}$) and negative ($N_{\text{GLTP}}$) $\text{GLTP}$
coded image representations from $S_{\text{GLTP}}$ values}
\stopnumbering
\ForAll{$S_{\text{GLTP}}(i)$ value in a $3 \times 3$ neighbourhood}
\If {$S_{\text{GLTP}}(i)>0$} $S_P(i) \gets 1$ \& $S_N(i) \gets 0$
\ElsIf {$S_{\text{GLTP}}(i)<0$} $S_P(i) \gets 0$ \& $S_N(i) \gets 1$
\Else{} $S_P(i) \gets 0$ \& $S_N(i) \gets 0$
\EndIf
\EndForAll
\Statex $P_{\text{GLTP}} = \sum_{i=0}^{7} S_P(i) \times 2^i$
\Statex $N_{\text{GLTP}} = \sum_{i=0}^{7} S_N(i) \times 2^i$
\Statex \textbf{repeat} for each $ 3\times 3$ neighbourhood
\end{algorithmic}
\end{algorithm}
\end{document}