5

I would like to append a character to the chapter number in some cases but not everywhere. The change should also be reflected in the table of contents. The chapter numbering should look like this:

1

2a

2b

3

How can I accomplish this?

4
  • Sounds duplicated but I'm not finding.
    – Sigur
    Feb 19, 2015 at 17:41
  • 1
    Are you sure that you want a chapter named 2a and 2b? Sounds strange
    – user31729
    Feb 19, 2015 at 17:42
  • 1
    Or is a "workaround" to get not over the allowed number of chapters? Very strange and absolutly unusual ... Please rethink if you really want this.
    – Mensch
    Feb 19, 2015 at 17:49
  • Yes. It needs to be that way. The need arises from the fact that the chapter numbers must correspond to a predefined id which cannot be changed. Usually there is only one chapter belonging to that id but this time there are two. Feb 19, 2015 at 17:54

2 Answers 2

8

A variation of my answer to Chapter with specific number (1, 1b, 2, 2b, 3, 4)

\documentclass{book}

\usepackage{hyperref} % optional

\makeatletter
\newcounter{repchapter}
\let\latex@chapter\chapter
\renewcommand{\chapter}{\@ifstar{\latex@chapter*}{\guuk@chapter}}
\newcommand{\guuk@chapter}{\@ifnextchar+{\repeat@chapter}{\restorechapter\latex@chapter}}
\newcommand{\repeat@chapter}[1]{% #1 is +
  \ifnum\value{repchapter}>0
    \addtocounter{chapter}{-1}%
  \fi
  \stepcounter{repchapter}%
  \latex@chapter
}
\newcommand{\restorechapter}{%
  \setcounter{repchapter}{0}%
}
\def\thechapter{\arabic{chapter}\alph{repchapter}}
% this is for hyperref (it does nothing if not loaded)
\def\theHchapter{\arabic{chapter}\alph{repchapter}}
\makeatother

\begin{document}

\tableofcontents

\chapter{Title}
x
\chapter+{First part}
x
\chapter+{Second part}
x
\chapter{Normal}
x
\end{document}

enter image description here

If you need something like

1
2a
2b
3a
3b

you have to issue \restorechapter before chapter 3a in order to reset the subsidiary counter to zero, so

\chapter{One}
<text>

\chapter+{Two A}
<text>

\chapter+{Two B}
<text>

\restorechapter

\chapter+{Three A}
<text>

\chapter+{Three B}
<text>

A possibly better interface, with a key-value syntax:

\documentclass{book}

\usepackage{xparse}

\usepackage{hyperref}


\newcounter{repchapter}
\let\latexchapter\chapter
\renewcommand\thechapter{\arabic{chapter}\alph{repchapter}}
% this is for hyperref (it does nothing if not loaded)
\providecommand\theHchapter{}
\renewcommand\theHchapter{\arabic{chapter}\alph{repchapter}}

\ExplSyntaxOn
\RenewDocumentCommand{\chapter}{sO{}m}
 {
  \IfBooleanTF{#1}
    {\latexchapter*{#3}}
    {\egreg_complex_chapter:nn { #2 } { #3 }}
 }

\keys_define:nn { egreg/complex_chapter }
 {
  tochead .tl_set:N = \l_egreg_complex_chapter_tochead_tl,
  suffix  .bool_set:N = \l_egreg_complex_chapter_suffix_bool,
  suffix  .default:n = { true },
  reset   .bool_set:N = \l_egreg_complex_chapter_reset_bool,
  reset   .default:n = { true },
 }

\cs_new_protected:Npn \egreg_complex_chapter:nn #1 #2
 {
  \keys_set:nn { egreg/complex_chapter }
   {
    tochead = { #2 },
    suffix = false,
    reset = false,
    #1
   }
  \bool_if:NT \l_egreg_complex_chapter_reset_bool
   {
    \setcounter{repchapter}{0}
   }
  \bool_if:NTF \l_egreg_complex_chapter_suffix_bool
   {
    \int_compare:nT { \value{repchapter} > 0 }
     {
      \addtocounter{chapter}{-1}
     }
    \stepcounter{repchapter}
   }
   {
    \setcounter{repchapter}{0}
   }
  \latexchapter[\l_egreg_complex_chapter_tochead_tl]{#2}
 }
\ExplSyntaxOff

\begin{document}

\tableofcontents

\chapter{Title}
x
\chapter[suffix]{First part}
x
\chapter[suffix,tochead=Second part]{Second part with a very long title}
x
\chapter[reset,suffix]{Another first part}
x
\chapter[suffix]{Another second part}
x
\end{document}

The reset key is needed only if a split chapter that needs a fresh number follows another split chapter.

Since we have overloaded the optional argument, the key tochead is used for specifying the abbreviated title.

enter image description here

1
  • Great! Worked for me! Feb 19, 2015 at 18:19
3

Quick (and dirty!)

\documentclass{book}

\let\latexthechapter\thechapter
% Helper counter
\newcounter{alphachapter}
\setcounter{alphachapter}{0}



\makeatletter
\newcommand{\DoReallyBadThingsToChapterCounter}{%
\setcounter{alphachapter}{0}%
\stepcounter{chapter}%
% Store chapter command 
\let\latexc@chapter\c@chapter%
% Force chapter command to be help counter
\let\c@chapter\c@alphachapter%
% Change output format
\renewcommand{\thechapter}{\the\latexc@chapter\alph{alphachapter}}%
}

\newcommand{\RestoreChapterCounterMethod}{%
% Restore chapter counter command
\let\c@chapter\latexc@chapter%
% Restore chapter counter output format
\let\thechapter\latexthechapter%
}
\makeatother



\begin{document}
\tableofcontents
\chapter{First}


\DoReallyBadThingsToChapterCounter
\chapter{Second A}
\chapter{Second B}
\chapter{Second C}

\RestoreChapterCounterMethod
\chapter{Third}

\chapter{Fourth}

\DoReallyBadThingsToChapterCounter
\chapter{Fifth A}
\chapter{Fifth B}
\chapter{Fifth C}
\RestoreChapterCounterMethod

\chapter{Sixth}

\end{document}

enter image description here

You must log in to answer this question.

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