以下算法的行号为 2。我只需要它们从 1 开始,然后按 1 进行:1、2、3、4 等。为什么会发生这种情况?谢谢,问候,
\documentclass{article}
\usepackage[ruled,lined,linesnumbered]{algorithm2e}
\renewcommand{\baselinestretch}{1.2}
\title{Algo}
\author{}
\date{February 2022}
\begin{document}
\vspace{1cm}
\SetNlSty{textbf}{}{:}
\IncMargin{1em}
\setcounter{algocf}{1}
\begin{algorithm}[H]
\nl first line\
\nl second line\
\nl third line \
\BlankLine
\caption{My caption.}\label{alg:cap}
\end{algorithm}
\end{document}
结果:
答案1
尝试此代码。(不同的设置选项)
\documentclass{article}
\usepackage[commentsnumbered, ruled]{algorithm2e} % changed <<<<<<<<<<<<
\renewcommand{\baselinestretch}{1.2}
\title{Algo}
\author{}
\date{February 2022}
\begin{document}
\vspace{1cm}
\SetNlSty{textbf}{}{:}
\IncMargin{1em}
\setcounter{algocf}{1}
\begin{algorithm}[H]
\nl first line\
\nl second line\
\nl third line \
\BlankLine
\caption{My caption.}\label{alg:cap1}
\end{algorithm}
\begin{algorithm}[H]
\nl first line\
\nl second line\
\nl third line \
\BlankLine
\caption{My caption.}\label{alg:cap2}
\end{algorithm}
\end{document}
答案2
有这样的想法吗?我希望能够很好地理解你的问题。
\documentclass{article}
\usepackage[ruled,lined,linesnumbered]{algorithm2e}
\renewcommand{\baselinestretch}{1.2}
\title{Algo}
\author{}
\date{February 2022}
\begin{document}
\vspace{1cm}
\SetNlSty{textbf}{}{:}
\IncMargin{1em}
\setcounter{algocf}{1}
\begin{algorithm}[H]
first line\
second line\
third line \
\BlankLine
\caption{My caption.}\label{alg:cap}
\end{algorithm}
\end{document}
答案3
我删除了linesnumbered
并只保留了lined
包。此外,我\setcounter{AlgoLine}{0}
在 之后设置了\begin{algorithm}[H]
,以确保行从指定的数字开始。这对我很有用,因为我有许多算法接连出现,我需要1
每个算法的行从 重新开始。