1

I need to suppress the line numbers for \For loops and \If statements in my algorithm. I found the \skipnumber command online which does the job, however for some reason it messes up the indentation of my next \State statement. I also see that for some reason there is no space between the \Else statement and the text. I have attached a screenshot of the problem and the relevant code. Any ideas?

The problem

\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}
0

First of all, \ForAll is defined within algpseudocode as a block structure with a Start, but no End:

\algdef{S}[FOR]{ForAll}[1]{\algorithmicforall\ #1\ \algorithmicdo}%

This is important to note, as it plays with the indentation. So, we redefine it to have an End:

\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

and also remove the End text (since you're using the noend package option). The above combination is similar to

\algdef{SN}[FOR]{ForAll}{EndForAll}[1]{\algorithmicforall\ #1\ \algorithmicdo}%

Next, I redefine the way your \skipnumber macro works. Instead of supplying a number, let's define two switches - \stopnumbering and \resumenumbering - that deactivates and reactivates line numbering. It makes the codes more readable, I think.

enter image description here

\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.