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}

etoc
or have modified your table of contents in some more basic wayadd a comment
button to reply to commentators.\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 Appendix1A
. why1A
when there's no1B
?