LaTeX Internals
From Liki
[edit] The source code handbook
If you are really interested in the internals of LaTex: http://home.imf.au.dk/burner/Manualer/TeX/source2e.pdf
[edit] A problem solved for Bob Gilmore by Dan Cross
I recently encountered a problem in some TeX code in a class file that printed a mini table of contents at the beginning of each chapter. The problem was that if two sections existed on the same page then there were both given the same section number (the number of the second section) in the contents list. The command used the \write command to write a line into a .toc file for the chapter. However, TeX does not execute write commands as it finds them, but at the end up a page. The command included the \thesection command to report the current section, but this was evaluated at the page end, after another section was defined, thus both calls to this function expanded to the same value, the value of the second section.
So, to force TeX to execute this command when encountered I changed \write to \immediate\write. This created the correct expansion of \thesection, but the chapter title was not reported correctly - there was extra TeX commands at the front. Well, I have to explanation for the following work around, but this seemed to work. I tried to copycat the TeX .toc writing calls and used a temporary token to hold the variable as foll0ows (the section title is variable #7 in this routine):
\newtoks\@temptokenz
\@temptokenz{#7}%
\immediate\write\currchap{\string\contentsline{xsection}{\string\numberline{\thesection}\string\nobreakspace{}%
{\the\@temptokenz}}{\thepage}}%
---Admin interuption---Thanks Dan for this...for the rest of us who aren't latex wizards, here is the line that didn't work:
\write\currchap{\string\contentsline{xsection}{\string\numberline{\thesection}#7}{\thepage}}
It was added to a file w-book.cls (Wiley cls file for writing Wiley text books in latex) by a co-author of Bob Gilmore's, and then remodeled by Dan Cross to work properly.
In fact, the whole section added for the purpose of adding local table of contents at the beginning of each chapter is:
%% To make Chapter Table of Contents
%% January 27, 2002
%%
%% Type \chaptableofcontents after \chapter{title} command:
%% \chapter{Chapter Title}
%% \chaptableofcontents
\def\x@dottedtocline#1#2#3#4#5{%
\ifnum #1>\c@tocdepth \else \vskip \z@ plus.01\p@
{\leftskip #2\relax
\rightskip \@tocrmarg \parfillskip -\rightskip \parindent #2\relax
\@afterindenttrue \interlinepenalty \@M \leavevmode \@tempdima #3\relax
\advance \leftskip \@tempdima \hbox {}\hskip -\leftskip{\bf #4}%
\nobreak% \leaders \hbox {$\m@th \mkern
% \@dotsep mu.\mkern \@dotsep mu$}
\hfill\nobreak\hbox to\@pnumwidth {\hfil #5}\par }\fi}
\def\l@xsection{
\x@dottedtocline{1}{\parindent}{2.5em}}%was 2.3em
\def\@sect#1#2#3#4#5#6[#7]#8{\let\dolabelnow\relax%
\ifnum #2>\c@secnumdepth
\let\@svsec\@empty\else
\refstepcounter{#1}\edef\@svsec{\csname the#1\endcsname\hskip 1em }\fi
\@tempskipa #5\relax
\ifdim \@tempskipa>\z@
%
\begingroup#6\relax
\@hangfrom{\hskip #3\relax\@svsec}{\interlinepenalty \@M
\hyphenpenalty=10000 % No hyphenation in section heads
\raggedright
%% make section head uppercase
\ifnum#2=1{\def\label##1{\gdef\dolabelnow{\savelabel{##1}}}%
\global\setbox0=\hbox{\def\\ {\relax}#8}}%
\uppercase{\fi%
\def\label##1{}%
\def\\ {\hfill\break}%
#8\ifnum#2=1}\fi\par}%%
\endgroup%
\csname #1mark\endcsname{#7} %
%
{%<===
\def\\ { }%
\addcontentsline{toc}{#1}{\ifnum #2>\c@secnumdepth\else%
\protect\numberline{\csname the#1\endcsname}\fi
#7 }%<===
}%<===
%
\else
\def\@svsechd{#6\hskip #3\relax
\@svsec #8\csname #1mark\endcsname
{#7}\addcontentsline
{toc}{#1}{\ifnum #2>\c@secnumdepth \else
\protect\numberline{\csname the#1\endcsname}\fi
#7}}\fi\@xsect{#5}
\dolabelnow %% to make label not be uppercase.
\ifnum #2=1
\newtoks\@temptokenz
\@temptokenz{#7}%
\immediate\write\currchap{\string\contentsline{xsection}{\string\numberline{\thesection}\string\nobreakspace{}%
{\the\@temptokenz}}{\thepage}}%
%\write\currchap{\string\contentsline{xsection}{\string\numberline{\thesection}#7}{\thepage}}
\fi}
\def\chaptableofcontents{\openin1=ch\thechapter.toc
\ifeof1
\typeout{^^J^^J Please run LaTeX on this chapter^^J
again to form a Chapter Table of Contents^^J^^J}
\else
\closein1
\input ch\thechapter.toc\fi
\ifx\undefined\currchap
\newwrite\currchap
\else\immediate\closeout\currchap\fi
\immediate\openout\currchap=ch\thechapter.toc
\vskip12pt}
\endinput
--A great place to start in understanding the internals of tex. Back to Dan's explanation:
I defined \@temptokenz as a temporary holder to the variable and expanded that token in the writing command. This eliminated the unwanted part of the variable. Howerver, the \nobreakspace command was also missing (we want this), so I added it back in by hand (\string forces what comes after to be passed as a string and not expanded as a macro).
If anyone has an explanation for how \@temptokenz works here or another way around this problem, let me know.

