# [texhax] Conditional formatting of section headings

Philip G. Ratcliffe philip.ratcliffe at uninsubria.it
Thu Nov 20 17:05:05 CET 2003

> I'm trying to get  different  section heading alignments in even and odd
> pages: on odd(right) pages I want section headings to be right
> aligned(raggedleft) while on even(left) pages I want them to be left
> aligned(raggedright).
>
> I thought I found a good solution redefining the style part of the
> \section command this way:
>
> \renewcommand\section{%
>                                    \@startsection {section}{1}{0cm}%
>                                    {-3.5ex \@plus -1ex \@minus -.2ex}%
>                                    {2.3ex \@plus.2ex}%
>                                    {\sffamily\LARGE  \ifodd\value{page}
> \raggedleft \else \raggedright \fi}}
>
> This works fine, except for sections starting at the bottom of a page:
> as you know, to avoid a page break between the title of these sections
> and the following text, TeX pushes the title of the section to the
> following page, but the heading alignment is determined by the page
> where the section actually begins.
>
> For instance, if a section beginning at the end of page 4 is pushed to
> page 5, its heading will be left aligned because when the \section
> command was issued, the vaule of the page counter was 4.

Try this - be warned that it's a rather dirty hack (it relies on LaTeX
issuing a \par immediately after actually writing the heading, which appears
to be the first moment when the correct page is knwon to LaTeX).

\makeatletter%
\renewcommand\section{%
\@startsection {section}{1}{0cm}%
{-3.5ex \@plus -1ex \@minus -.2ex}%
{2.3ex \@plus.2ex}%
%
\newcounter{@lrhdcnt}
\stepcounter{@lrhdcnt}%
\xdef\@tmplrhlab{@lrhdlab\number\c@@lrhdcnt}%
\expandafter\ifx\csname r@\@tmplrhlab\endcsname\relax\else
\expandafter\ifodd\expandafter\pageref\expandafter{\@tmplrhlab}\relax
\raggedleft \else \raggedright \fi
\fi
\everypar{\expandafter\label\expandafter{\@tmplrhlab}\relax}%
}
\makeatother%

It works by issuing a label command AFTER the heading is actually written,
thus saving the correct page number. On the next pass of LaTeX (yes, it
needs TWO runs) it uses \pageref to get and use the correct page number.  It
achieves this via the \everypar command - this seems reasonably safe here,
but it will wipe out what somebody else might have put in to use immediately
after the heading (that could be worked around if necessary).

Cheers,  Philip G. Ratcliffe

P.S.  That cost a lot of sweat, but it was fun.

__________________________________________________________________
<< ella for Spam Control >> has removed Spam messages and set aside
You can use it too - and it's FREE!  http://www.ellaforspam.com << Message:
Conditional formatting of section headings (1.22 KB) >>  << File:
ATT00004.txt >>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: winmail.dat
Type: application/ms-tnef
Size: 2984 bytes
Desc: not available
Url : http://tug.org/pipermail/texhax/attachments/20031120/8655daf7/winmail.bin