On 04/11/2023 03:13, Bob Kerstetter wrote:
> After using LaTeX for 20-ish years, I am attempting to learn TeX,
Brave man :-)
> starting with macros for document structure and table of contents. In
> my first try (listed below):
>
> 1. document section titles format and print as expected.
> 2. \jobname.toc file contains same page number for every entry.
> 3. toc does not show up in document.
The trick with a .toc file is that it's (obviously) the first time
round. After that, it gets written to by headings until the end of the
document. To typeset the ToC, you must read it *before* anything writes
to it.
> %start macros
> % open toc file
> \newwrite\TOCfile
> \immediate\openout\TOCfile=\jobname.toc
I think this will wipe out anything in it. You need to move this line to
*after* the file has been used for input.
> \def\writeTOC#1#2{%
> \immediate\write\TOCfile{#2\quad\leaders\hbox{.}\hfill#1\par}%
> }
I don't think it has to be \immediate because you're not trying to
re-use it immediately (ie in the same run). But I may be wrong on that.
>
> % input toc
> \def\toc{%
> \closeout\TOCfile
> \input \jobname.toc
> }
The file won't be open at this stage, assuming you want the ToC (from
the previous run) at the start of the document, before anything new gets
written to it, so you need to remove the \closeout line. The \input
should then work, and must be followed by the \openout line from above,
so it's ready for use.
But it's been a long time since I played with this.
Peter