1

I am trying to remove the number before the for loop in the below algorithm. I would like to continue using the packages I am using, but have only found potential answers for algorithm2e. Is it possible to stop the algorithm from numbering the for statement?

\documentclass[pdflatex,sn-mathphys,lineno,noend]{article}
\usepackage[utf8]{inputenc}
\usepackage{algorithm}
\usepackage{algorithmicx}%
\usepackage{algpseudocode}%
\usepackage{amsfonts, bm, amsmath, amssymb}
\usepackage{mathtools}


\begin{document}



\begin{algorithm}[!htp]
\caption{This is the algorithm 
}

\begin{algorithmic}[1]
\Require $\mathbf{X}$\\

Get initial information;\\

$\begin{aligned}
y = mx + b,
\end{aligned}$

\For{$i \gets 1 \text{ to } \vert k \vert$}{


do something

\EndFor}\\
get results

\end{algorithmic}
\end{algorithm}
\end{document}

Thanks in advance.

1 Answer 1

0

You can temporarily intervene and remove the line number printing algorithm when setting a \For and \EndFor. In the code below, \NoLNFor and \NoLNEndFor do just that using the following steps:

  • Copy the current version of \alglinenumber (used to print the line number)
  • Remove functionality of \alglinenumber
  • Set \For/\EndFor as usual
  • Step the line counter back one
  • Restore \alglinenumber

enter image description here

\documentclass{article}

\usepackage{algorithm,algpseudocode}

\newcommand{\oldalglinenumber}{}% Dummy storage macro
\NewDocumentCommand{\NoLNFor}{ m }{%
  \RenewCommandCopy{\oldalglinenumber}{\alglinenumber}% Copy current version of line number printing macro
  \RenewDocumentCommand{\alglinenumber}{ m }{}% Turn \alglinenumber into a no-op
  \For{#1}% Print regular for command
  \addtocounter{ALG@line}{-1}% Step line counter back
  \RenewCommandCopy{\alglinenumber}{\oldalglinenumber}% Restore line number printing macro
}
\NewDocumentCommand{\NoLNEndFor}{}{%
  \RenewCommandCopy{\oldalglinenumber}{\alglinenumber}% Copy current version of line number printing macro
  \RenewDocumentCommand{\alglinenumber}{ m }{}% Turn \alglinenumber into a no-op
  \EndFor% Print regular end for command
  \addtocounter{ALG@line}{-1}% Step line counter back
  \RenewCommandCopy{\alglinenumber}{\oldalglinenumber}% Restore line number printing macro
}

\begin{document}

\begin{algorithm}
\caption{This is the algorithm}
  \begin{algorithmic}[1]
    \Require $\mathbf{X}$
    \State Get initial information;
    \State $y = mx + b$,
    \NoLNFor{$i \gets 1 \textup{ to } \vert k \vert$}
      \State do something
    \NoLNEndFor
    \State get results
  \end{algorithmic}
\end{algorithm}

\end{document}

You'll note that I updated your code as well to conform to the syntax provided by algorithmicx. That is, don't use \\, but \State. Similarly, $\begin{aligned} <math> \end{aligned}$ is more conveniently written as $<math>$ when there's no alignment involved.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .