[Tex/LaTex] Thesis table of contents: including chapter-specific appendices immediately after their respective chapters, rather than all at the end

appendicestable of contents

I'm wrapping up my dissertation and my table of contents currently reads along these lines:

    1. chapter one
      • 1.1 section one
      • 1.2 section two
    1. chapter two
      • 2.1 section one
      • 2.2 section two
  • A. appendix for chapter one
  • B. appendix for chapter two

if at all possible, i'd really like to be able to do something along these lines:

    1. chapter one
      • 1.1 section one
      • 1.2 section two
      • 1.A appendix
    1. chapter two
      • 2.1 section one
      • 2.2 section two
      • 2.A appendix

does anyone know how to do this sort of thing? thanks in advance!

Edit

Thank you for the quick reply @Au101. ideally i'd like the actual appendix to chapter one to come before the actual start of chapter two. i am pretty experienced with latex, but this is my first time putting together a book-like document rather than just an individual article…

Best Answer

Some weird code-golfing with xparse and a slightly (;-)) modified \section command -- do not use \subsection there!

I just added a 4th optional argument to \section, which is a *, indicating that this should be an appendix section, going right here, not at the end of the document. (well, a chapter appendix then)

It will break if another 'normal' \section will follow.

All is reset if the next \chapter comes into action.

And there is some warning by hyperref, but the links work.

\documentclass{book}

\usepackage{xpatch}

\usepackage{xparse}

\usepackage{blindtext}

\newcounter{appsection}[chapter]

\usepackage{hyperref}


\makeatletter

\let\latex@section\section
\let\latex@thesection\thesection

\RenewDocumentCommand{\section}{soms}{%
  \IfBooleanTF{#1}{%
    \latex@section*{#4}
  }{%
    \IfBooleanT{#4}{%
      \ifnumgreater{\value{appsection}}{0}{%
      }{%
        \setcounter{section}{0}%
        \renewcommand{\thesection}{\thechapter.\Alph{appsection}}%
        \renewcommand{\theHsection}{\thechapter.\Alph{appsection}}%
      }
      \stepcounter{appsection}%
    }%
    \IfValueTF{#2}{%
      \latex@section[#2]{#3}%
    }{%
      \latex@section{#3}%
    }%  
  }%
}



% Restore the section number format. 
\xpretocmd{\chapter}{\renewcommand{\thesection}{\latex@thesection}}{}{}

\makeatother


\begin{document}
\tableofcontents
\chapter{First}
\section{First section}
\section{Second section}
\section{First appendix}*

\chapter{Second}
\blindtext[10]
\section{First section}
\section{Second section}
\clearpage
\section{First appendix}*
\blindtext[10]
\clearpage
\section{Second appendix}*
\blindtext[10]


\chapter{Third}
\blindtext[10]
\section{First section}
\section{Second section}
\section{Third section}
\section{Fourth section}
\clearpage
\section{First appendix}*
\blindtext[10]
\clearpage
\section{Second appendix}*
\blindtext[10]

\section{Third appendix}*
\blindtext[10]




\end{document}

Update Improved interface and preventing sections to be added after some section has been declared as appendix section. It automatically adds a Appendix and a clearpage. subsections etc. work as expected as well as references.

The macro call is \section![toc title]{body title} if is meant to be an appendix section at the end of the chapter. If ! is omitted, the standard command is used.

\documentclass{book}
\usepackage{xpatch}
\usepackage{xparse}
\usepackage{blindtext}

\newcounter{appsection}[chapter]
\usepackage[bookmarksopen=true,bookmarksopenlevel=5]{hyperref}


\makeatletter

\newif\ifappsectionclearpage
\appsectionclearpagetrue

\newif\ifappsection
\appsectionfalse

\let\latex@section\section
\let\latex@thesection\thesection

\RenewDocumentCommand{\section}{st!om}{%
  \IfBooleanTF{#1}{%
    \latex@section*{#4}
  }{%
    \IfBooleanTF{#2}{%
      \appsectiontrue%
      \ifnumgreater{\value{appsection}}{0}{%
      }{%
        \setcounter{section}{0}%
        \renewcommand{\thesection}{\thechapter.\Alph{appsection}}%
        \renewcommand{\theHsection}{\thechapter.\arabic{appsection}}%
      }
      \stepcounter{appsection}%
      \ifappsectionclearpage
      \clearpage
      \fi
      \IfValueTF{#3}{%
        \latex@section[#3]{\appendixname~#4}%
      }{%
        \latex@section{\appendixname~#4}%
      }%    
    }{%
      \ifappsection
      %% Nothing in here!
      \else
      \IfValueTF{#3}{%
        \latex@section[#3]{#4}%
      }{%
        \latex@section{#4}%
      }%    
      \fi
    }%
  }%
}




\xpretocmd{\chapter}{\renewcommand{\thesection}{\latex@thesection}\protect\appsectionfalse}{}{}

\makeatother


\begin{document}
\tableofcontents
\chapter{First}
\section{First section}
\section{Second section}
\section!{First appendix}

In \ref{thirdapp:thirdchap} we will see that

\chapter{Second}
\blindtext[10]
\section{First section}
\section{Second section}
\clearpage
\section!{First appendix}
\blindtext[10]
\clearpage
\section!{Second appendix}
\blindtext[10]


\chapter{Third}
\blindtext[10]
\section{First section}
\section{Second section}
\section{Third section}
\section{Fourth section}
\clearpage
\section!{First appendix}
\blindtext[10]
\clearpage
\section!{Second appendix}
\subsection{First}
\blindtext[10]

\section!{Third appendix} \label{thirdapp:thirdchap}
\blindtext[10]

\section{Fifth section} % Will be ignored

\end{document}

enter image description here

Related Question