6

I'm translating a book where the author originally wrote the section numbering straight through the chapters, without resetting the numbering, and I wanted to keep this original structure. In the original, the table of contents is like,


 1. First chapter
   1.1 First Section
   1.2 Second section
 2. Second Chapter
   2.1 Third section
   2.2 Fourth section

and so on. I have tried every thing I could but the section numbers keep resetting after a new chapter.

2
  • Your example is confusing. You're showing a reset section counter with the name of the section 2.1 being "Third section". That would just mean you use \section{Third section} without changing the counter in any way. Please clarify.
    – Skillmon
    Aug 23, 2022 at 6:17
  • @Marino I have edited the answer. It should clarify a few things, which I omit in the first version. Specifically, you may want to use the starred version of the macro, which redefines \thesection and remove chapter numbers from it or non-starred version and keep the numbering unchanged. I also pointed to two places with more information about counters.
    – Celdor
    Aug 23, 2022 at 10:56

2 Answers 2

7

It can be achieved using counterwithout


EDIT. A couple of remarks.

The package chngcntr is required before Apr 2018 of LaTeX (see LaTeX2e News Issue 28).

\counterwithout* has an unstarred version \counterwithout[format]{counter}{within counter}, which automatically removes within counter if the optional argument is omitted; particularly, in the solution, chapters would no longer appear as a part of section numbering. If this is the desired effect, then \counterwithout should be used, instead.

For more information see the reference: source2e (the section "ltcounts.dtx" p. 400). Also, there is a website Counters on Overleaf, which provides some explanation with a few examples.



\documentclass{report}
%\usepackage{chngcntr}   % Before Apr 2018 LaTeX

\counterwithout*{section}{chapter}   % Section numbering remains unchanged
% \counterwithout{section}{chapter}   % Redefines section numbering


\begin{document}
\chapter{A}
\section{X}
\section{Y}

\chapter{B}
\section{Y}
\section{Z}
\end{document}
3
  • 4
    +1. However, I think that \counterwithout rather than \counterwithout* is what's needed to meet the OP's formatting objective. Aside: You may want to mention that the \counterwithin and \counterwithout macros were integrated into the LaTeX kernel in the spring of 2018. (Maybe more famously, the spring 2018 release of the LaTeX format made utf8 the default input encoding; since then, \usepackage[utf8]{inputenc} is the default.) Hence, loading the chngcntr package is needed only if one is still using a version of the LaTeX format dated 2017 or even older.
    – Mico
    Aug 23, 2022 at 6:06
  • 1
    @Mico Thanks for the comment. I edited the answer and addressed the issues.
    – Celdor
    Aug 23, 2022 at 10:53
  • I think your suggestion would've worked better, but I've found a way of doing the desired effect and for now I won't use in this project, but I'll try in the future with others. Thanks anyway!
    – Marino
    Oct 30, 2022 at 18:08
0

After think a bit I have accomplished the effect I wanted, by doing the following

    \let\OldSection\section
    \renewcommand*{\section}[1]{\OldSection{#1}\stepcounter{secCount}}
    \newcounter{secCount}
    \setcounter{secCount}{1}
    \renewcommand*{\thesection}{\arabic{secCount}}

I don't know if is the most effective or the correct way, but in this specific case worked very well.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .