# How to define the numbering of specific sections in a report

I am writing lecture notes referring to specific chapters and sections in the textbook chapters and sections.

I use multiple textbooks, so Chapters 1 and 2 of my lecture notes may be from Textbook X, but Chapter 3 of the lecture notes is Chapter 5 in Textbook Y. Similarly for sections. I simply want the section numbering in my lectures to reflect the true section numbers of the respective parts in the textbooks. I put a note at the beginning of every chapter/section to identify the current textbook.

So my question is: how can I re-number specific chapters/sections in a report personally?

• What \documentclass are you using? Also, are you using hyperref?
– Werner
Jun 1 '13 at 1:21
• \documentclass{report} and I might want to use hyperref later. thanks
– Aya
Jun 1 '13 at 1:44

Here's a complete implementation that allows you to specify the \chapter and section numbering at your own discretion. It works with/without hyperref:

\documentclass{report}
\usepackage{regexpatch}% http://ctan.org/pkg/regexpatch

\makeatletter
\xpatchcmd*{\@sect}{the#1}{fixed@seccntformat}{}{}% Insert fixed reference in ToC
\xpatchcmd{\l@chapter}{1.5em}{3.5em}{}{}% Lengthen space for ToC number entry
\newcommand{\fixchapters}{%
\let\old@chapter\chapter%
\renewcommand{\chapter}[1]{\xdef\thechapter{##1}% Store chapter #
\old@chapter}% Old chapter
}
\let\old@section\section
\renewcommand{\section}[1]{\xdef\fixed@seccntformat{#1}% Store section #
\old@section}% Old section
\let\old@subsection\subsection
\renewcommand{\subsection}[1]{\xdef\fixed@seccntformat{#1}% Store subsection #
\old@subsection}% Old subsection
\makeatother

\usepackage{hyperref}% http://ctan.org/pkg/hyperref

\begin{document}

\tableofcontents

\fixchapters

\chapter{A.1.B}[Here's a chapter]{A chapter}
\section{3.4}{A section}
\section{1.2}{Another section}
\chapter{1718}{Another chapter}
\section{5}{Yet another section}
\section{8.8}*{Also a section}

\end{document}


The idea is different for \chapter, but similar for all sectioning commands. For sections, the number is stored in \fixed@seccntformat and patched in the section setting macro \@sect. For \chapters, the counter printing macro \thechapter is updated with the required number.

The reason for supplying \fixchapters is that elements of the \frontmatter use the starred variant of \chapter which are embedded inside macros like \tableofcontents and the \listof-clan. Of course, a better interface can be adopted, if needed.

Some considerations:

• The interface requires you to supply a mandatory argument as the first argument to all sectioning macros. So you will use \section{<number>}[<ToC>]{<title>} always. Similar for \chapter{<number>}[<ToC>]{<title>}.

• Depending on the chapter/section number length, you might have to update the \l@chapter and/or \l@<section> macros to give you more space in the ToC. You'll notice, in my example above, that I've increased the regular 1.5em chapter \numberline width to 3.5em.

• This looks exactly like what I want, although I am not able to compile it yet because of the package you are using. I need to download the file and try then will come back. Thanks very much.
– Aya
Jun 3 '13 at 3:49

I'm a bit foggy about the implementation you're looking for. Here's an idea, but I might be missing the point:

\documentclass{report}
\usepackage{lipsum}

\renewcommand{\chaptername}{Lecture}
\usepackage{xparse}
\ExplSyntaxOn
\tl_new:N \l_my_lecture_book_title_tl
\tl_new:N \l_my_lecture_author_tl
\newcounter{lecturenumber}
\keys_define:nn{ my_lectures }
{
chapter . code:n = { \setcounter{chapter}{\number\numexpr#1\relax}},
section . code:n = { \setcounter{section}{\number\numexpr#1-1\relax}},
book    . tl_set:N = \l_my_lecture_book_title_tl,
author  . tl_set:N = \l_my_lecture_author_tl
}
\NewDocumentCommand{\mylecture}{ om }
{
\setcounter{lecturenumber}{\thechapter}
\keys_set:nn { my_lectures } { #1 }
\section[#2]{#2\hspace*{\fill}\newline
{\small \tl_use:N \l_my_lecture_book_title_tl
\hspace*{\fill}
(\tl_use:N \l_my_lecture_author_tl)}}
\setcounter{chapter}{\thelecturenumber}
}

\ExplSyntaxOff
\pagestyle{empty}
\begin{document}

\tableofcontents

\chapter{Modeling Random Times}
\mylecture[chapter=8,
section=5,
book={Stochastic Differential Equations},
author={Bernt \O{}ksendal},
]{What's a Random Time Change}

\lipsum[1-3]

\mylecture[chapter=24,
section=2,
book={Statistics},
author={Freedman, Pisani, Purves}
]{Modeling Random Events}

\lipsum[4-5]

\chapter{Testing}

\end{document}


There are several issues, some of which might be:

• A table of index will have slightly confusing numbering going on. But you might not care about this. If you do, please elaborate how you want that handled.

• The \section command is called with an optional argument solely because I thought you might want a table of contents.

• The is a very neat approach.
– Werner
Jun 1 '13 at 3:35
• This is very neat and may benefit me well later. But I would also need to rename chapters randomly, not just sections. Thank you very much.
– Aya
Jun 3 '13 at 3:46