[texhax] Custom Headers

Uwe Lück uwe.lueck at web.de
Wed Oct 18 00:46:46 CEST 2006


Just some hints at typical difficulties with your ideas
and how to deal with them (encounters with output routines
of a strange kind, well-known to [partial] [La]TeXperts):

At 23:47 06.10.06, Karthik Narayanaswamy wrote:

>I have an article class document with sections, subsections and
>subsubsections. I want the headers of the document to read the section
>name (without number),

Just for this, something like

     \lhead{\renewcommand{\thesection}[1]{\unskip}\leftmark}

(and somewhere \rightmark!?) might suffice (by chance).
I suggest `[1]' for gobbling a lonely dot or an ensuing \space;
it may be wrong as well. Or it may be OK with only one of
\leftmark and \rightmark.

>except on pages where I start a new section.
>After trying my hand with published solutions on the web using
>fancyhdr.sty and associated \leftmark and \rightmark, I decided instead
>to write my own macro.

So you know about \leftmark and \rightmark [?]
(else: some LaTeX documentation; additionally
the TeXbook on \firstmark etc.)

>I included this in my custom Section macro:
>
>\newcommand{\Section}[1]{
>     \section{#1}
>     \thispagestyle{empty}
>     \def\Sec{#1}
>% Other lines irrelevant to this e-mail
>}
>
>The headers were then defined as:
>     \pagestyle{fancy}
>     \fancyhf{}
>     \lhead{\Sec}

Already here is a mistake, an effect of which you perhaps haven't
realized so far. (I discovered the same mistake in some macros
of mine recently). When your \Section is processed, it changes
the heading of the current page to the new title -- before LaTeX decides
whether the section heading will appear on the current page or on the
next one -- so in the latter case, the new page heading appears
"too early". In general (roughly), page breaking is considered only
when a "paragraph" has been typeset -- by then, all the assignments
that contributed to the paragraph have been executed.

The "mark" mechanism of (La)TeX (\firstmark etc., \markboth etc.,
\leftmark/\rightmark, ...) is made to avoid this mistake indeed.
By a command like \sectionmark, the information about the
section title is stored, processed only when the page break
has been decided ... (well, this seems not to be entirely true,
I am observing a "mark" bug similar to the well-known wrong
switching of \marginpar sides [see mparhack]).

So you must launch a "mark" command instead of changing
some macro. In general, the argument of \lhead etc.
should not involve macros whose definitions are changed
within the document -- or such changes should happen only
immediately after a \newpage or so.

>Simple enough, and it works ... well, sort of. I found that when a
>section starts at the bottom of a page, LaTeX is smart enough to move
>the section to a new page, but the \pagestyle{empty} command still
>applies to the previous page. That is, I end up with the page where the
>section really starts with a header, and the previous page with no
>headers.

First, from a "style" point of view, I wouldn't recommend changing
the page style when "by pure chance" a section heading happens
to start a page. A change of page style rather should represent
something /contentual/ -- such as when a /chapter/ enters an
essentially new matter. So I would rather recommend to reconsider
where such page breaks are contentually essential -- and then
use \chapter from the report class instead of \Section.

Second, from a (La)TeXnical point of view:
It seems to me that the LaTeX \thispagestyle never
has been designed to work unless on some
"initial" page or after a \newpage or so.
Your goal may be achieved by a new "mark" mechanism:
So far, the "marks" only store section titles etc.
-- two groups, one for the left-hand page, another for the
right-hand page (LaTeX). A third group might transfer
page layout information to the output routine --
yet this needs some deep knowledge etc.

So I consider your query interesting from the point of view
of how to make TeX doing something very special
-- while I think you rather should aim at something usual.

HTH -- Uwe.



More information about the texhax mailing list