[Tex/LaTex] \subsubsubsection count not reset when starting a new section, subsection, etc

sectioningsubdividingtable of contents

I am trying to use subsubsubsection on my report, and I using a code that was given on this website.

On below my MWE :

\documentclass[a4paper,11pt,french]{report}
\usepackage[francais]{babel}

\usepackage{titlesec}
\titleclass{\subsubsubsection}{straight}[\subsection]

\newcounter{subsubsubsection}
\renewcommand\thesubsubsubsection{\thesubsubsection.\arabic{subsubsubsection}}
\renewcommand\theparagraph{\thesubsubsubsection.\arabic{paragraph}} % optional; useful if paragraphs are to be numbered

\titleformat{\subsubsubsection}
  {\normalfont\normalsize\bfseries}{\thesubsubsubsection}{1em}{}
\titlespacing*{\subsubsubsection}
{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}

\makeatletter
\renewcommand\paragraph{\@startsection{paragraph}{5}{\z@}%
  {3.25ex \@plus1ex \@minus.2ex}%
  {-1em}%
  {\normalfont\normalsize\bfseries}}
\renewcommand\subparagraph{\@startsection{subparagraph}{6}{\parindent}%
  {3.25ex \@plus1ex \@minus .2ex}%
  {-1em}%
  {\normalfont\normalsize\bfseries}}
\def\toclevel@subsubsubsection{4}
\def\toclevel@paragraph{5}
\def\toclevel@paragraph{6}
\def\l@subsubsubsection{\@dottedtocline{4}{10em}{5em}}
\def\l@paragraph{\@dottedtocline{5}{10em}{5em}}
\def\l@subparagraph{\@dottedtocline{6}{14em}{6em}}
\makeatother

\setcounter{secnumdepth}{4}
\setcounter{tocdepth}{4}

\begin{document}

\tableofcontents

\pagebreak

\section{test}
\subsubsubsection{test11}
\subsubsubsection{test12}
\subsubsubsection{test13}

\section{test2}
\subsubsubsection{test21}
\subsubsubsection{test22}
\subsubsubsection{test23}

\end{document}

As you will notice, the subsubsubsection is not reset between these 2 sections.

How can I fix it please ?

How can I fix it please ? Thanks in advance for your help.

Best Answer

The subsubsubsection counter is reset at subsubsection. In your example the subsubsection counter never changed. (It remains always at zero.)

(More precisely, the reset command is only triggered when \stepcounter or \refstepcounter is called on the parent counter. As no \subsubsection command was ever issued, the counter is never step-increased, and the reset command never called.)

You can see the problem even without defining your own new section level. If you compile the following MWE:

\documentclass{article}
\begin{document}

\section{Test}
\subsubsection{test2}
\section{test3}
\subsubsection{test4}
\subsection{test 5}
\subsubsection{test6}
\end{document}

You will see that test2 is labeled 1.0.1 and test4 is labeled 2.0.2. But once you increment the subsection counter (when I put in test 5), the counter now behaves properly: test6 is 2.1.1.

This doesn't happen just with sectioning commands, by the way. If you use the \numberwithin command from the AMS packages to set the level for equations, or use the amsthm commands to tie theorem numbering to section levels, you should never tie the resetting to a counter that cannot be reliably incremented.

A possible work-around is given here: in your preamble add

\makeatletter
\@addtoreset{subsubsubsection}{section}
\@addtoreset{subsubsubsection}{subsection}
\makeatother

this will make increasing sections and subsections both also trigger the reset code.