4

I have the following chapter macro (uses code from Heiko Oberdiek):

\def\ChaptTocEntry#1#2{%
  \line{\hbox to1em{\hfil\bf#1}\hskip1em#2\leaderfill\folio}}
\def\ChaptWr#1{{%
  \let\folio\relax
  \edef\next{%
    \write0{\noexpand\ChaptTocEntry{\the\ChaptNo}{#1}}}\next}}
\def\Chapt#1{\advance\ChaptNo by1\SecNo=0%
  \vskip-\lastskip
  \bigskip\noindent{\bf\the\ChaptNo\hskip1em#1}\ChaptWr{#1}\par\nobreak\medskip
  \everypar={{\setbox0=\lastbox}\everypar={}}}

\par\nobreak\medskip is used to not allow page breaks between the heading and the following text. Although the TOC information is written (\ChaptWr{#1}) after the heading text is set (the #1 before \ChaptWr) the wrong page number is used if the \nobreak causes the shift of the heading to the top of the next page.

If \ChaptWr is put later in the definition (after \nobreak) it allows page breaks itself. I don't know why since it does not contain anything which is output to the page (only to a file).

When I simplify the the heading macro to

\def\Chapt#1{%
  \bigskip\noindent{\folio\ #1}\par\nobreak\medskip
  \everypar={{\setbox0=\lastbox}\everypar={}}}

\folio is wrong for "shifted" headings (does still output the previous page number).

7
  • 1
    Either set \folio in the output routine (typical: header or footer line) or you need some kind of \label/\ref mechanism like the one in LaTeX. Aug 14, 2015 at 16:06
  • I did set a new register: \headline={\hfil\folio\hfil\global\PageNo=\pageno} but this makes the problem even worse. I don't understand why \folio is correct in the page header but displayed wrong some vertical skip down in the page (and then correct again more lower in the page). How can I set \folio in the header? Aug 14, 2015 at 18:54
  • 1
    When TeX typesets the paragraphs and sets the page number, it is not yet known, on which page the paragraph lines will be put (asynchronous output routine). Aug 14, 2015 at 19:01
  • I had guest this meanwhile. Since I call the heading macro before that paragraph it means there is no way to get the correct page number for the TOC ... Is there a simple solution that other plain TeX users use in this case? Aug 14, 2015 at 19:24
  • Next thing I need is indeed an index and cross references. So anyway I need something like \label/\ref. This can then be used for TOC too. I hope I find something helpfull in the TeXbook or its source. Thank you for the answers. Aug 15, 2015 at 9:26

1 Answer 1

2

The description of the index implementation in the TeXbook (p. 423-424) provided a simple solution for the heading macro:

\newwrite\TocFh

\def\MkTocEnt{%
  \xdef\WrTocEnt{\write\TocFh{\line{\TocText\leaderfill
    \noexpand\number\pageno}}}\WrTocEnt}

\def\Chapt#1{\advance\ChaptNo by1 \SecNo=0
  \vskip-\lastskip
  \bigskip\noindent{\bf\the\ChaptNo\hskip1em#1}% output heading
  {\def\TocText{\hbox to1em{\hfil\bf\the\ChaptNo}\hskip1em#1}% output TOC data
    \let\next=\TocText\MkTocEnt}%
  \par\nobreak\medskip
  \everypar={{\setbox0=\lastbox}\everypar={}}}

\def\TOC{\input toc\immediate\openout\TocFh=toc}

(\leaderfill is something like \hfil, see TeXbook p. 223.)

This solution should work for index and cross references too.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .