# Is it possible to make LaTeX read the sectioning in reverse direction?

I want to make a note (for internal use only) while I am reading a book. For the sake of convenience, I want to put a note about the current topic I am reading on the first page of the PDF output to avoid pressing the END button to go the last page. I am sorry. I am a bit lazy to make use of a separate file for sectioning. It is my real scenario. Therefore, the heading structure must be reversed as follows.

\documentclass{book}
\usepackage{tipa}
\begin{document}

\chapter{Book Printing versus Ordinary Typing}

% end of chapter 1
\ldots
\section{Pronounction of \TeX}
\TeX\ is often pronounced \textipa{/\textprimstress t\textepsilon k/} in English.

\section{Root of the name \TeX}
\TeX\ is the upper case of $\tau\epsilon\chi$ which is a Greek word that means art as well as technology.

\chapter{The Name of the Game}
\end{document}


Having completed the book, I want to reverse the heading structure to the normal order. But doing this by hand seems to be tedious and cumbersome.

Shortly speaking, is it possible for LaTeX to read the heading structure in reverse direction? I mean I want LaTeX to read the above code as if it is written as follows.

\documentclass{book}
\usepackage{tipa}
\begin{document}
\chapter{The Name of the Game}

\section{Root of the name \TeX}
\TeX\ is the upper case of $\tau\epsilon\chi$ which is a Greek word that means art as well as technology.

\section{Pronounction of \TeX}
\TeX\ is often pronounced \textipa{/\textprimstress t\textepsilon k/} in English.

\ldots

% end of chapter 1

\chapter{Book Printing versus Ordinary Typing}
\end{document}


Edit:

I think the most convenient marks I can adopt are as follows.

% begin
\section{Root of the name \TeX}
\TeX\ is the upper case of $\tau\epsilon\chi$ which is a Greek word that means art as well as technology.
% end

-
Either this requires grapping of very long sections, or you should simply create a file for each section. Thereafter, you can manipulate the sections via a list and reverse etc. as wanted. Or how do you expect the interface? –  zeroth Mar 24 '13 at 20:08
For reversing a list, a recent question comes in mind: tex.stackexchange.com/questions/36034/… –  zeroth Mar 24 '13 at 20:09
I think I don't need any interface. Just as simple as the code I gave above. –  stalking is prohibited Mar 24 '13 at 20:17
There are all kinds of requirements that you have not stated, the main one being is there any verbatim material. If you grab each section as a macro argument all verbatim-like stuff will fail, and doing this without grabbing the sections as macro arguments would be similarly tricky. The suggested markup makes the problem optimally difficult as there is no end marker for each section and it varies sometimes a section ends with (the next) \section, sometimes with \chapter. Could a markup that clearly delimits each section unit not be used? –  David Carlisle Mar 24 '13 at 20:36
well use perl to reverse the text then –  David Carlisle Mar 24 '13 at 20:46

This version sets the chapter/section counts you could extend it to get these using label/ref and take multiple passes, but it's probably not worth it in any real case.

\documentclass{book}
\usepackage{tipa}

\let\oldchapter\chapter
\let\oldsection\section
\makeatletter
\setcounter{chapter}{3}
\def\chapter#1{\egroup
\setbox\z@\vbox{\unvbox\tw@\unvbox\z@}\setbox\tw@\vbox\bgroup\oldchapter{#1}%
\setcounter{section}{3}}
\def\section{\egroup
\setbox\z@\vbox{\unvbox\tw@\unvbox\z@}\setbox\tw@\vbox\bgroup\oldsection}

\AtBeginDocument{\setbox\z@\vbox{}\setbox\tw@\vbox\bgroup}
\AtEndDocument{\egroup
\unvbox\tw@
\unvbox\z@}

\begin{document}

\chapter{Book Printing versus Ordinary Typing}

% end of chapter 1
\ldots
\section{Pronounction of \TeX}
\TeX\ is often pronounced \textipa{/\textprimstress t\textepsilon k/} in English.

\section{Root of the name \TeX}
\TeX\ is the upper case of $\tau\epsilon\chi$ which is a Greek word that means art as well as technology.

\chapter{The Name of the Game}
\end{document}

-
Try to make 4 chapters and one section for each chapter. You will see that the chapter counter starts with a negative number and the section number is 1 greater than the chapter number. :-) –  stalking is prohibited Apr 5 '13 at 5:13
@Karl'sstudents yes sure it is set to 3 by hand in this version (it was late at nght:-) similarly the section counters, you either need to just manually up that number or just use a \label/\ref to the last chapter and last section in each chapter and set the numbers correctly on the second pass –  David Carlisle Apr 5 '13 at 8:35