[Tex/LaTex] Algorithm with chapter number

algorithm2e

I want the algorithm to be prefix with chapter
say in chapter 1, it should be 1.1, 1.2, for chapter 2, it should be 2.1, 2.2

But for my case, i only have 1,2,3,… across all chapters, how should I change it?

The include package is listed as followed:

\usepackage[ruled,vlined,linesnumbered,algo2e,resetcount,algochapter]{algorithm2e}

Here is the minimal working example

\documentclass[11pt]{book}
\usepackage{bigstrut}
\usepackage{stmaryrd}
\usepackage{tipa}
\usepackage{amssymb}

\usepackage{textcomp}
\usepackage{times}
\usepackage{pifont}
\usepackage{amsfonts}

\usepackage{makeidx}
\usepackage{moreverb}
\usepackage{calc}
\usepackage{dsfont}


\usepackage{tikz}
\usepackage{color}
\usepackage{etex}
\usepackage{ctable}
\usepackage{bigstrut}
\usepackage{pifont}
\usepackage{amsfonts}
\usepackage{makeidx}
\usepackage{moreverb}
\usepackage{scalefnt}
\usetikzlibrary{shapes.geometric, positioning, fit, calc}

\usetikzlibrary{decorations.markings}

\usepackage{scalefnt}
\usepackage{pgf}
\usepackage{graphicx}
\usepackage{pgfplots}
\usepackage{verbatim}
\usetikzlibrary{
    shapes.geometric,
    positioning,
    fit,
    calc
}
\usepackage{amsmath}



\usetikzlibrary{arrows,automata,positioning,shapes,patterns}

\usepackage{etex}
\usepackage{amsmath, url, proof} % mathabx,
\usepackage{textcomp}
\usepackage{listings}
\usepackage{float}
\usepackage{multirow}
\usepackage[ruled,vlined,linesnumbered,algo2e,resetcount,algochapter]{algorithm2e}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}
\usepackage{wrapfig}
\usepackage{graphicx}
\usepackage[latin1]{inputenc}
\usetikzlibrary{trees}
\usepackage{verbatim}
\usetikzlibrary{trees,decorations.pathmorphing}
\usepackage{listings}
\usepackage[T1]{fontenc}
\usepackage[scaled]{beramono}
\usepackage{lipsum}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%






\def\pgfsysdriver{pgfsys-dvipdfm.def}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\usepackage{epsfig}
\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows,automata,positioning}
\usepackage{epsf}
\usepackage{amsmath,amssymb,amsfonts} %
\usepackage{graphicx}

\usepackage{ltlfonts}
\usepackage{textcomp}
\usepackage{color}
\usepackage{chngcntr}

\usepackage{latexsym} % latex symbols
\usepackage{amsthm} % for defining theorems
\usepackage{syntonly} % verify syntax, not very important
\usepackage{lscape} 

\usepackage{algpseudocode}
\usepackage{algorithm}
\usepackage{caption}
\usepackage{subcaption}% http://ctan.org/pkg/subcaption
\captionsetup{compatibility=false}
\DeclareCaptionSubType*{algorithm}
\renewcommand\thesubalgorithm{\thetable\alph{subalgorithm}}
\DeclareCaptionLabelFormat{alglabel}{Alg.~#2}


\usepackage{pxfonts, pifont} % About fonts
\usepackage{setspace}

\usepackage{graphics}
\usepackage{multirow} % To write complicated tables
\usepackage{colortbl} % Colors
\usepackage{rotating} % For rotating tables

\usepackage{verbatim} % Comment Environment
                      % eg. \begin{comment}
                      %     \end{comment}

\usepackage{mdwlist}

%%

\usepackage[titletoc]{appendix}

\usepackage{url}

\usepackage{style/mythesis}
\usepackage{lscape}

\usepackage{listings}
\newcommand{\myfontsize}{\fontsize{9}{10}\selectfont}
\usepackage[backref=section,colorlinks=true,hyperfootnotes=false]{hyperref}
  \hypersetup{
            colorlinks,%
            citecolor=black,%
            filecolor=black,%
            linkcolor=black,%
            urlcolor=black,breaklinks = true, %
            pdfprintscaling = None, %
            unicode = true, %
            bookmarksnumbered = true,
      }


\setcounter{secnumdepth}{3}

\interfootnotelinepenalty=10000







\begin{document}


\newpage
\pagenumbering{roman}
\singlespacing{ \listofalgorithms
\addcontentsline{toc}{section}{\numberline{}\hspace{-.35in}{\bf List
of Algorithms}}} 

\doublespacing



\chapter{chapter1}~\label{a1}
\begin{algorithm}[H]
\centering
a
\caption{algo}\label{algo1}
\end{algorithm}

\chapter{chapter2}~\label{a2}
\begin{algorithm}[H]
\centering
a
\caption{algo}\label{algo2}
\end{algorithm}

\begin{algorithm}[H]
\centering
a
\caption{algo}\label{algo3}
\end{algorithm}

\end{document}

Best Answer

Since you are using both the algorithm2e and algorithm packages, and both of them implement some same commands/environments, you had to use the algo2e option for algorithm2e to prevent a clash and this overrides the settings for the counter that you set as options for algorith2e, since the environment that will be used is the one from algorithm. You have three possible solutions:

  1. If you don't need the algorithm package, load only algorithm2e and not algorithm and load the former without the algo2e option:

    \documentclass[11pt]{book}
    \usepackage[ruled,vlined,linesnumbered,resetcount,algochapter]{algorithm2e}
    
    \begin{document}
    
    \chapter{chapter1}
    \begin{algorithm}[H]
    \centering
    test
    \caption{algo}
    \label{algo1}
    \end{algorithm}
    
    \end{document}
    

    enter image description here

  2. If you really need both algorithm2e and algorithm in your document, and you want to use the algorithm environment (from the algorithm package) add the following lines to your preamble, after loading the packages:

    \makeatletter 
    \renewcommand\thealgorithm{\thechapter.\arabic{algorithm}} 
    \@addtoreset{algorithm}{chapter} 
    \makeatother
    

    A complete example:

    \documentclass[11pt]{book}
    \usepackage[ruled,vlined,linesnumbered,algo2e,resetcount,algochapter]{algorithm2e}
    \usepackage{algorithm}
    
    \makeatletter 
    \renewcommand\thealgorithm{\thechapter.\arabic{algorithm}} 
    \@addtoreset{algorithm}{chapter} 
    \makeatother
    
    \begin{document}
    
    \chapter{chapter1}
    \begin{algorithm}[H]
    \centering
    test
    \caption{algo}
    \label{algo1}
    \end{algorithm}
    
    \end{document}
    

    enter image description here

  3. If you really need both algorithm2e and algorithm in your document, and you want to use the algorithm environment (from the algorithm2e package), use the algorithm2e environment (the algo2e option renames algorithm to algorithm2e):

    \documentclass[11pt]{book}
    \usepackage[ruled,vlined,linesnumbered,algo2e,resetcount,algochapter]{algorithm2e}
    \usepackage{algorithm}
    
    \begin{document}
    
    \chapter{chapter1}
    \begin{algorithm2e}[H]
    \centering
    test
    \caption{algo}
    \label{algo1}
    \end{algorithm2e}
    
    \end{document}
    

enter image description here

By the way (not related to the question), you are loading packages more than once; this should be avoided.