根据章节显示算法编号

根据章节显示算法编号

我必须根据算法出现的章节对算法进行编号。

我使用了命令 \listofalgorithmes。

但我仍然有问题。请参考所附的代码和图片

\documentclass[a4paper,11pt]{report}
\usepackage{tocbibind}
\newcommand{\listofalgorithmes}{\tocfile{\listalgorithmcfname}{loa}}
\usepackage[ansinew]{inputenc}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\usepackage{etoolbox}
\newcounter{algoline}
\newcommand\Numberline{\refstepcounter{algoline}\nlset{\thealgoline}}
\AtBeginEnvironment{algorithm}{\setcounter{algoline}{0}}
\begin{document}
\listofalgorithmes

\chapter{Test}
\begin{algorithm} 
\caption{Algo A1}\label{alg:A1}
\end{algorithm}
\begin{algorithm} 
\caption{Algo A2}\label{alg:A2}
\end{algorithm}

 \chapter{Test2}
 \begin{algorithm} 
 \caption{Algo B1}\label{alg:B1}
\end{algorithm}
\begingroup
\LinesNumberedHidden
\begin{algorithm}
\DontPrintSemicolon
\KwIn{A,B,C }
\KwOut{D,E,F}
\Numberline\textbf{Step 1}\\ AAA.\;
BB, \\ee\; 
\Numberline Then go to the home \\
\Numberline\textbf{Step 2} \\
\Numberline BB,\\ cc\; 
\Numberline Then go to a hotel \\
\SetAlgoRefName{1}
\caption{Ala kanawa}
\label{algo:sj1}
\end{algorithm}
\endgroup

\chapter{Test3}
\begin{algorithm} 
\caption{Algo C}\label{alg:C}
\end{algorithm}

\end{document}

在此处输入图片描述

答案1

algochapter添加选项algorithm2e

\documentclass[a4paper,11pt]{report}
\usepackage{tocbibind}
\newcommand{\listofalgorithmes}{\tocfile{\listalgorithmcfname}{loa}}
\usepackage[ansinew]{inputenc}
\usepackage[linesnumbered,ruled,vlined,algochapter]{algorithm2e}
\usepackage{etoolbox}
\newcounter{algoline}
\newcommand\Numberline{\refstepcounter{algoline}\nlset{\thealgoline}}
\AtBeginEnvironment{algorithm}{\setcounter{algoline}{0}}
\begin{document}
\listofalgorithmes

\chapter{Test}
\begin{algorithm}
\caption{Algo A1}\label{alg:A1}
\end{algorithm}
\begin{algorithm}
\caption{Algo A2}\label{alg:A2}
\end{algorithm}

 \chapter{Test2}
 \begin{algorithm}
 \caption{Algo B1}\label{alg:B1}
\end{algorithm}
\begingroup
\LinesNumberedHidden
\begin{algorithm}
\DontPrintSemicolon
\KwIn{A,B,C }
\KwOut{D,E,F}
\Numberline\textbf{Step 1}\\ AAA.\;
BB, \\ee\;
\Numberline Then go to the home \\
\Numberline\textbf{Step 2} \\
\Numberline BB,\\ cc\;
\Numberline Then go to a hotel \\
%\SetAlgoRefName{1}          %% do you really need this?
\caption{Ala kanawa}
\label{algo:sj1}
\end{algorithm}
\endgroup

\chapter{Test3}
\begin{algorithm}
\caption{Algo C}\label{alg:C}
\end{algorithm}

\end{document}

在此处输入图片描述

相关内容