TransWikia.com

Chapter-specific appendices with numbers related to the chapters

TeX - LaTeX Asked by gwg on May 14, 2021

I am using subappendices or an appendix after each chapter, following this post. This is the Latex I am using

usepackage[toc,page]{appendix}

AtBeginEnvironment{subappendices}{%
    chapter*{Appendix}
    addcontentsline{toc}{chapter}{Appendices}
    counterwithin{figure}{section}
    counterwithin{table}{section}
}

which produces, for example, this:

enter image description here

However, the subappendices’ titles do not have numbers. Ideally, they would and their sections would have numbers based on that number. What I had in mind would be like this:

3. Chapter Three
  3.1 Intro
  3.2 Some details
  3.3 Experiments
3A. Appendix
  3A.1 First subappendix of chapter 3
  3A.2 More details etc

Is there a way to achieve this?

One Answer

You can modify the code from the early post so that:

  • add “thechapter Aquad” (or something like this) to chapter*{...}
  • add conveniently addcontentsline to add “thecahpter A” to appendix at TOC
  • modify thesection to print “A.”

This is done with:

AtBeginEnvironment{subappendices}{%
  chapter*{thechapter Aquadappendixname}
  % add numberline{<num-chap>A} to toc
  addcontentsline{toc}{chapter}{%
    protectnumberline{thechapter A}appendixname}
  counterwithin{figure}{section}
  counterwithin{table}{section}
}

% make section display <num-chap>A.<num-sec>
apptocmd{subappendices}{%
  renewcommand{thesection}{thechapter A.arabic{section}}}{}{}

However, one sees that new appendix numberings (like 1A.2) are longer than previous (like 1.B) and toc lines have numbers too close to titles as you can see below:

numbers too close to titles

So you may increase these gaps. Look for the code of l@chapter and l@section in your class file (book.cls here) and change spacing, like below:

makeatletter
% in l@chapter change 1.5em by 2em
patchcmd{l@chapter}{1.5em}{2em}{}{}
% in l@section change {1.5em}{2.3em} by {2em}{2.8em}
renewcommand*l@section{@dottedtocline{1}{2em}{3em}}
makeatother

The full code:

documentclass{book}
usepackage{appendix}
usepackage{chngcntr}
usepackage{etoolbox}
usepackage{lipsum}

AtBeginEnvironment{subappendices}{%
  chapter*{thechapter Aquadappendixname}
  % add numberline{<num-chap>A} to toc
  addcontentsline{toc}{chapter}{%
    protectnumberline{thechapter A}appendixname}
  counterwithin{figure}{section}
  counterwithin{table}{section}
}

% make section display <num-chap>A.<num-sec>
apptocmd{subappendices}{%
  renewcommand{thesection}{thechapter A.arabic{section}}}{}{}

makeatletter
% in l@chapter change 1.5em by 2em
patchcmd{l@chapter}{1.5em}{2em}{}{}
% in l@section change {1.5em}{2.3em} by {2em}{2.8em}
renewcommand*l@section{@dottedtocline{1}{2em}{3em}}
makeatother

begin{document}

tableofcontents
chapter{Test Chapter One}
section{A regular section}
section{Another regular section}
begin{subappendices}
section{Some title for an appendix}
lipsum[4]
section{Some title for an appendix}
lipsum[4]
end{subappendices}
chapter{Test Chapter Two}
section{A regular section}
section{Another regular section}
begin{subappendices}
section{Some title for an appendix}
lipsum[4]
section{Some title for an appendix}
lipsum[4]
section{Some title for an appendix}
lipsum[4]
end{subappendices}

end{document}

Result: final contents as desired

Correct answer by Miguel V. S. Frasson on May 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP