1

Is there a simple way to allow in Latex to have:

  • Chapter 1
  • Chapter 2
  • Chapter 3 a
  • Chapter 3 b
  • Chapter 4

All the previous questions that have asked something similar are quite old and have what seems like complicated/not really robust methods to achieve this. Is there any somewhat easy and elegant method?

1 Answer 1

1

This seems sufficiently robust. If you need consecutive subchapters, add \resetchapters when you want to step the counter.

\documentclass[oneside]{book} % oneside is just to have smaller pictures

\usepackage[a6paper,margin=1cm,bottom=1.5cm,heightrounded]{geometry} % just to have smaller pictures

\usepackage{etoolbox} % needed for \preto

\newcounter{keepchapter}
\newif\ifsubchapter

\newcommand{\subchapter}{%
  \ifsubchapter
    % all is already set up
  \else
    \subchaptertrue
    \setcounter{keepchapter}{\value{chapter}}%
    \stepcounter{keepchapter}%
    \renewcommand{\thechapter}{\thekeepchapter\alph{chapter}}%
    \setcounter{chapter}{0}%
  \fi
  \standardchapter
}
\let\standardchapter\chapter
\preto\chapter{\resetchapter}
\newcommand{\resetchapter}{%
  \ifsubchapter
    \setcounter{chapter}{\value{keepchapter}}
    \subchapterfalse
    \renewcommand{\thechapter}{\arabic{chapter}}%
  \fi
}

\begin{document}

\frontmatter

\begingroup\scriptsize % just to have smaller pictures

\tableofcontents

\endgroup % just to have smaller pictures

\chapter{Introduction}

References

\ref{first} \ref{a}
\ref{second-a} \ref{b}
\ref{second-b} \ref{c}
\ref{third-a} \ref{d}
\ref{third-b} \ref{e}

\mainmatter

\chapter{First}\label{first}
\section{Test}\label{a}

\subchapter{Second A}\label{second-a}
\section{Test}\label{b}

\subchapter{Second B}\label{second-b}
\section{Test}\label{c}

\resetchapter
\subchapter{Third A}\label{third-a}
\section{Test}\label{d}

\subchapter{Third B}\label{third-b}
\section{Test}\label{e}

\chapter{Fourth}\label{fourth-b}
\section{Test}\label{f}

\subchapter{Fifth A}\label{fifth-a}
\section{Test}\label{g}

\subchapter{Fifth B}\label{fifth-b}
\section{Test}\label{h}

\end{document}

enter image description here

If hyperref is involved, you need to add a couple of lines

\newcounter{keepchapter}
\newif\ifsubchapter

\newcommand{\subchapter}{%
  \ifsubchapter
    % all is already set up
  \else
    \subchaptertrue
    \setcounter{keepchapter}{\value{chapter}}%
    \stepcounter{keepchapter}%
    \renewcommand{\thechapter}{\thekeepchapter\alph{chapter}}%
    \renewcommand{\theHchapter}{\thekeepchapter\alph{chapter}}%
    \setcounter{chapter}{0}%
  \fi
  \standardchapter
}
\let\standardchapter\chapter
\preto\chapter{\resetchapter}
\newcommand{\resetchapter}{%
  \ifsubchapter
    \setcounter{chapter}{\value{keepchapter}}
    \subchapterfalse
    \renewcommand{\thechapter}{\arabic{chapter}}%
    \renewcommand{\theHchapter}{\arabic{chapter}}%
  \fi
}
2
  • This looks really great! Is there a way to go Chapter 1, Chapter 2a, Chapter 2b, Chapter 3a, Chapter 3b? At the moment I can't see how to make it know to move from Chapter 2b to Chapter 3a given they are both subchapter.
    – J.M
    Commented Jan 17, 2021 at 2:23
  • @J.M I modified the code to allow for manually resetting the mechanism when you want two consecutive series of subchapters with different main number.
    – egreg
    Commented Jan 17, 2021 at 22:43

You must log in to answer this question.

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