[comp.text.tex] TeXnical question

zjdg11@hou.amoco.com (Jim Graham) (06/19/91)

I've been working on setting up a macro within my standard macro collection
that would (in plain TeX) create a table of contents as the file is processed.

a file, referred to by \contentsfile , is opened, and when various heading
macros are called, they write the heading, etc., and the current pageno to
the file.

The macros look something like this:

\def\mainheading#1{\goodbreak
    \centerline{\subtitles\uppercase{#1}} \par
    \ifcontents
       \immediate\write\contentsfile{|countitem \uppercase{#1} |dotfill \folio}
       \immediate\write\contentsfile{}
    \fi
    }

Note, when the table of contents is pulled in, "|" is defined as an escape
char, just like \ normally is.  I had to do this because I couldn't figure
out how to print the "\" to the file as such (if someone can help there as
well, I'm all ears...or eyes, as the case may be  ;-)  ).

If I call this as \mainheading{foo} I'll end up with something like
   |countitem FOO |dotfill 42

Most of the time, this works out to be correct.  HOWEVER, and this is where
I need some assistance, if it just so happens that the heading WAS going to
be written in at the bottom of the page (with the paragraph starting at the
top of the next page) before the \goodbreak fixed that, TeX still lists the
page number for that section as being the previous page, when in fact it has
been moved into the next page.

Now, I'm sure there's something stupid that I'm missing....but....

I'm kinda looking for 2 things here.  First, I'd really like to know how
to fix this situation.  Second, if someone can point me to the right page
numbers in The TeXbook to learn more about this situation, I'd love to hear
it.

Thanks in advance,
   --jim

Standard disclaimer....These thoughts are mine, not my employer's.

------------------------------------------------------------------------------
Share and Enjoy!  (Sirius Cybernetics Corporation, complaints division)
73, de n5ial

Internet:  zjdg11@hou.amoco.com    or    grahj@gagme.chi.il.us
Amateur Radio:
   TCP/IP:    jim@n5ial.ampr.org (44.72.47.193)
   Packet:    BBS went QRT for good...still searching for new one.
------------------------------------------------------------------------------

eijkhout@s41.csrd.uiuc.edu (Victor Eijkhout) (06/20/91)

zjdg11@hou.amoco.com (Jim Graham) writes:

>\def\mainheading#1{\goodbreak
>    \centerline{\subtitles\uppercase{#1}} \par
>    \ifcontents
>       \immediate\write\contentsfile{|countitem \uppercase{#1} |dotfill \folio}
>       \immediate\write\contentsfile{}
>    \fi
>    }

>Note, when the table of contents is pulled in, "|" is defined as an escape
>char, just like \ normally is.  I had to do this because I couldn't figure
>out how to print the "\" to the file as such (if someone can help there as
>well, I'm all ears...or eyes, as the case may be  ;-)  ).

Do \string\countitem or \noexpand\countitem

>If I call this as \mainheading{foo} I'll end up with something like
>   |countitem FOO |dotfill 42

>Most of the time, this works out to be correct.  HOWEVER, and this is where
>I need some assistance, if it just so happens that the heading WAS going to
>be written in at the bottom of the page (with the paragraph starting at the
>top of the next page) before the \goodbreak fixed that, TeX still lists the
>page number for that section as being the previous page, when in fact it has
>been moved into the next page.

Well, if you just remove the \immediate everything will be fine,
because then the write is executed when the part of the page
that contains it is actually written out.

However, you may also want to write the chapter number, somehting like
    \write\tocfile{\string\tocchap{\chapno}{#1}{\folio}}
The problem with this is that if more than one chapter starts
on a certain page (which can easily happen with sections or subsections)
you will twice get the second chapter number. Remedy for that:
    \edef\act{\noexpand\write{\string\tocchap{\chapno}{#1}{\noexpand\folio}}}
    \act
In the \edef the chapternumber will be expanded, and the
write statement will contain th correct number.

>Now, I'm sure there's something stupid that I'm missing....but....

You were missing something indeed, but it's not stupid. All of this
is quite nontrivial. Table of contents are particularly nasty,
because you may want to read them in while you are building them
(for instance the toc comes after the preface, and the preface should
appear in the toc, so the file is open for writing, and you
want to read it in in that place).

>I'm kinda looking for 2 things here.  First, I'd really like to know how
>to fix this situation.  Second, if someone can point me to the right page
>numbers in The TeXbook to learn more about this situation, I'd love to hear
>it.

The only place in the TeXbook that treats this is exercise (!!!) 21.10.
I suggest you keep an eye peeled for 'TeX in practice' by Stephan
von Bechtolsheim and 'A TeXnician's Reference Guide' by yours truly,
which are both to appear later this year and which treat this sort
of stuff in detail.

Another option is to take an existing macro packag that has the
toc facility, and try to understand what's happening there.

Victor.

zjdg11@hou.amoco.com (Jim Graham) (06/20/91)

In article <1991Jun19.211753.4838@csrd.uiuc.edu> eijkhout@s41.csrd.uiuc.edu (Victor Eijkhout) writes:

>Do \string\countitem or \noexpand\countitem

yep, got mail from one of the folks at Texas A&M (WHOOP!) who is part of the
TeX Support team, and he also suggested the \string\ fix.  it worked, of
course.  :-)  and he suggested killing the \immediate, as you did.  and of
course, that also worked.

However, you bring up another issue that has just gotten me confused
again....and perhaps will result in more learning on my part.  :-)

>However, you may also want to write the chapter number, somehting like
>    \write\tocfile{\string\tocchap{\chapno}{#1}{\folio}}

In fact, in a slightly different version of the macros, my file called
rfpmacros.tex, I do the following:

\def\newsection#1{\advance\sectionnumber by 1
   \subsectionnumber=0 \subsubsectionnumber=0
   \null{\subtitles
   \secno .\the\subsectionnumber ) \uppercase{#1}
   \vskip.05in\par}
   \ifcontents
     \write\contentsfile{\string\bulletitem
       \secno .\the\subsectionnumber ) \uppercase{#1} \string\dotfill \folio}
     \write\contentsfile{}
  \fi
  }

I have similar macros for subsections and sub-subsections.

>The problem with this is that if more than one chapter starts
>on a certain page (which can easily happen with sections or subsections)
>you will twice get the second chapter number.

And here's the confusion.....  I definitely do get the correct results,
both in the text and in the \contentsfile.  Is it because I'm using
\the\sectionnumber and so on?  Or am I doing something else right w/o knowing
it?  :-)

>I suggest you keep an eye peeled for 'TeX in practice' by Stephan
>von Bechtolsheim and 'A TeXnician's Reference Guide' by yours truly,
>which are both to appear later this year and which treat this sort
>of stuff in detail.

will something be posted to the net when these come out?  sounds like 2 books
to have sitting next to The TeXbook.

thanks,
   --jim

Standard disclaimer....These thoughts are mine, not my employer's.

------------------------------------------------------------------------------
Share and Enjoy!  (Sirius Cybernetics Corporation, complaints division)
73, de n5ial

Internet:  zjdg11@hou.amoco.com    or    grahj@gagme.chi.il.us
Amateur Radio:
   TCP/IP:    jim@n5ial.ampr.org (44.72.47.193)
   Packet:    BBS went QRT for good...still searching for new one.
------------------------------------------------------------------------------