3

Is it possible to enter the source code for an appendix section within the actual chapter but get it typeset after the last chapter of book?

For example something that looks like this in the code:

\chapter{Chapter 1}
\section{Section 1}
This is the first section of the first chapter. For details see Appendix 1.
\begin{appendices}
\section{Appendix for Chapter 1, Section 1}
Here is additional information that is typeset at the end of the book.
\end{appendices}
\chapter{Chapter 2}
\section{A new section}
This is a new section in the second Chapter. Even though I add a new appendix here, it will become consolidated with the first appendix at the end of the book.
\begin{appendices}
\section{Appendix for a new section}
This is the appendix for "A new section" in Chapter 2
\end{appendices}

and produces this as the output:

Chapter 1

1.1 Section 1

This is the first section of the first chapter. For details see Appendix 1.

Chapter 2

2.1 A new section

This is a new section in the second Chapter. Even though I add a new appendix here, it will become consolidated with the first appendix at the end of the book.

Appendices

A. Appendix for Chapter 1, Section 1

Here is additional information that is typeset at the end of the book.

B. Appendix for a new section

This is the appendix for "A new section" in Chapter 2

  • You can wrap everything inside a verbatim environment to get the code, or you can always use the listings package to include a file with the code. – Mario S. E. Jul 1 '14 at 14:41
  • You'd need to write to a file, and include it later. See ctan.org/topic/defer-stuff for a selection of tools to help. – Thruston Jul 1 '14 at 14:44
  • 1
    You might find some of these ctan.org/topic/exercise helpful as well. – Thruston Jul 1 '14 at 14:52
  • I;m curious to know why you want to do this. What's wrong with writing it as an Appendix in the normal way...at the end? – Peter Flynn Apr 3 '16 at 18:16
1

This is a modification of https://tex.stackexchange.com/a/186907/4427

\documentclass{book}
\usepackage{environ}

\newtoks\mainnotetoks
\newtoks\tempnotetoks
\newtoks\prenotetoks
\newtoks\postnotetoks

\NewEnviron{appendixatend}{%
  \tempnotetoks=\expandafter{\BODY}%
  \edef\notetemp{%
    \the\mainnotetoks % what was already stored
    \the\prenotetoks % text before the new note
    \the\tempnotetoks % the current note
    \the\postnotetoks % text after the new note
  }%
  % update \mainnotetoks
  \global\mainnotetoks=\expandafter{\notetemp}%
}
\newcommand\includeappendices{%
  \appendix
  \chapter*{Appendix}
  \renewcommand{\thesection}{\Alph{section}}
  \the\mainnotetoks}

% set the pre and post note
\prenotetoks={}
\postnotetoks={}

\begin{document}
\mainmatter

\chapter{Chapter 1}
\section{Section 1}
This is the first section of the first chapter. For details see Appendix 1.
\begin{appendixatend}
\section{Appendix for Chapter 1, Section 1}
Here is additional information that is typeset at the end of the book.
\end{appendixatend}
\chapter{Chapter 2}
\section{A new section}
This is a new section in the second Chapter. Even though I add a new appendix here, it will become consolidated with the first appendix at the end of the book.
\begin{appendixatend}
\section{Appendix for a new section}
This is the appendix for "A new section" in Chapter 2
\end{appendixatend}

\includeappendices
\end{document}

enter image description here

| improve this answer | |
  • thank you. this also does the trick: \def\myAppendix{\appendix} \def\addToAppendix#1{\expandafter\def\expandafter\myAppendix\expandafter{\myAppendix { }#1}} however, your answer seems less hacky and I like the environment – Ulli Jul 2 '14 at 7:25
  • @Ulli your code doesn't compile. Can you post a full answer with your code and double check it first? Thanks. – yegor256 Sep 23 '16 at 21:57
2

Put the content into a macro, and then put this macro at the end of the book:

\def\LastChapter{.....}

...

\LastChapter
| improve this answer | |
  • You can redefine the macro to append new material. One could also use a different macro for each appendix. – John Kormylo Jul 1 '14 at 16:30
  • thank you for pointing that out, here's the macro (again) \def\myAppendix{\appendix} \def\addToAppendix#1{\expandafter\def\expandafter\myAppendix\expandafter{\myAppendix { }#1}} then later in the document add \myAppendix – Ulli Jul 2 '14 at 7:31

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.