我正在尝试删除以下算法中 之前的数字for loop
。我想继续使用我正在使用的软件包,但只找到了 的潜在答案algorithm2e
。是否可以阻止算法对for
语句进行编号?
\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}
提前致谢。
答案1
\For
您可以在设置和时临时干预并删除行号打印算法\EndFor
。在下面的代码中,使用以下步骤执行此操作\NoLNFor
:\NoLNEndFor
- 复制当前版本
\alglinenumber
(用于打印行号) - 删除功能
\alglinenumber
- 设置
\For
/\EndFor
照常 - 将线路计数器往后退一位
- 恢复
\alglinenumber
\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}
您会注意到,我也更新了您的代码,以符合提供的语法algorithmicx
。也就是说,不要使用\\
,而是\State
。同样,当不涉及对齐时,$\begin{aligned} <math> \end{aligned}$
更方便的写法是。$<math>$