# Suppress headers when page contains a section title using titlesec

I'm writing a book with titlesec and try to suppress headers containing the section name, when the section starts at the top of the respective page. Given that all my sections start on a new page I tried \assignpagestyle, but it doesn't work. Manually adding \thispagestyle for each section works, but there should be a better solution.

\documentclass[a4paper,12pt]{book}

\usepackage[pagestyles]{titlesec}

{}{}{\thesection~\sectiontitle}%
\setfoot{}{\thepage}{}%
}
\renewpagestyle{plain}{%
\setfoot{}{\thepage}{}%
}
\newpagestyle{section}{%
{}{}{}%
\setfoot{}{\thepage}{}%
}
\assignpagestyle{\section}{section}

\begin{document}

\clearpage

\section{First}     % warning here
even page
\clearpage

\section{Second}    % warning here
suppress header here, odd page, start section on top
\clearpage
chapter headers are ok, even page
\clearpage
keep header here, odd page, no new section

\end{document}


Using MikTeX 2.8 I get the following warnings:

Package titlesec Warning: Page style in straigh class ignored on input line <XXX>

-

\usepackage{xpatch}
\xpretocmd{\section}{\thispagestyle{plain}}{}{}


Or should you want to suppress headers only for odd pages containing section titles:

\usepackage{xpatch}
\usepackage{scrextend}
\xpretocmd{\section}{\ifthispageodd{\thispagestyle{plain}}{}}{}{}

-
Using \usepackage{xpatch} and \xpretocmd{\section}{\thispagestyle{section}}{}{} is equivalent to your second solution, where the odd/even distinction is made using titletoc in the definition of \newpagestyle{section}. – Markus Schmassmann Apr 21 '12 at 1:42

The warning

Package titlesec Warning: Page style in straight class ignored on input line 30


gives you a hint about the problem: the page style you defined for section pages wasn't applied and the reason is that \section is, by default, (using the titlesec terminology) of straight class; i.e., is intended to produce titles in the middle of text. To allow the application of the page style, you can change the class of \section to top:

\titleclass{\section}{top}


This, together with \newcommand\sectionbreak{\clearpage} will give the desired result (the spacing for section titles is corrected using \titlespacing{\section}{0pt}{*-3}{*1.5}):

\documentclass[a4paper,12pt]{book}
\usepackage[pagestyles]{titlesec}

\titleclass\section{top}
\newcommand\sectionbreak{\clearpage}
\titlespacing{\section}{0pt}{*-3}{*1.5}

{}{}{\thesection~\sectiontitle}%
\setfoot{}{\thepage}{}%
}
\renewpagestyle{plain}{%
\setfoot{}{\thepage}{}%
}
\newpagestyle{section}{%
{}{}{}%
\setfoot{}{\thepage}{}%
}
\assignpagestyle{\section}{section}

\begin{document}

\clearpage

\section{First}     % warning here
even page
\clearpage

\section{Second}    % warning here
suppress header here, odd page, start section on top
\clearpage
chapter headers are ok, even page
\clearpage
keep header here, odd page, no new section

\end{document}

-
This will add two pages in the OP's example, as \sections now start on odd pages. – lockstep Apr 21 '12 at 0:06
@lockstep: not necessarily; if the OP uses \sectionbreak to start a new page with \section, then there will be no additional pages. – Gonzalo Medina Apr 21 '12 at 0:16
That's better, but the space above \section is still different from the OP's version. – lockstep Apr 21 '12 at 0:18
@lockstep: true. I've added a comment. – Gonzalo Medina Apr 21 '12 at 0:21
@GonzaloMedina: \titlespacing{\section}{0pt}{*-3}{*1.5} corrects the space above \section, proposed edit of your answer. – Markus Schmassmann Apr 21 '12 at 1:17