3

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...

  • 1
    Do you want that behaviour just in the table of contents, but with the actual appendices themselves all at the back - or do you actually want the appendix to chapter one itself to come before the start of chapter two. In both cases, but especially if it's just the table of contents - my friend, I hope you know what you're doing ... :P – Au101 Jul 9 '15 at 18:52
  • P.S. Welcome to TeX.SE. You'll probably find that you'll get better answers if you include a minimum working example: meta.tex.stackexchange.com/questions/228/…. It will certainly likely be nicer for us to help you. For example, the solution may change if you've loaded a package like etoc or have modified your table of contents in some more basic way – Au101 Jul 9 '15 at 18:55
  • @ABD Welcome! Instead of adding comments in the answer block, you can click the add a comment button to reply to commentators. – AboAmmar Jul 9 '15 at 19:19
  • @ABD: An appendix is basically nothing else than an additional \chapter or \section, with another counting/numbering style, most probably. This is possible of course, but appendix means: to append ... at the end ;-) It looks rather weird to have just an Appendix 1A. why 1A when there's no 1B? – user31729 Jul 9 '15 at 19:19
  • you want to format appendix (contents of appendix in the body) as sections following corresponding chapters or just costum the table of contents – touhami Jul 9 '15 at 20:52
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.