[Tex/LaTex] Suppressing page numbering in the first page of chapter in mainmatter but *not* in frontmatter

chaptersfront-matterheader-footerpage-numbering

I am using the book class. I want to suppress page styles (page number, header, footer) in the first page of every chapter in my mainmatter, but not in \frontmatter – where I want to have roman numbers in the footer (right corner).

I can suppress page style in the first page of every chapter document-wide with:

\renewcommand\chapter{\if@openright\cleardoublepage\else\clearpage\fi
                \thispagestyle{empty}% original style: plain
                \global\@topnum\z@
                \@afterindentfalse
                \secdef\@chapter\@schapter}
\makeatother

but this imposes a \pagestyle{empty} in the first page of every "chapter" in the frontmatter, too, so that the TOC pages are not numbered. I want the TOC pages to be numbered, but I don't know how to do this.

MWE:

\documentclass[a4paper,11pt,titlepage,oneside,openany]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{kantlipsum}
% empty style for the first page of every chapter
\makeatletter
\renewcommand\chapter{\if@openright\cleardoublepage\else\clearpage\fi
                \thispagestyle{empty}% original style: plain
                \global\@topnum\z@
                \@afterindentfalse
                \secdef\@chapter\@schapter}
\makeatother

\usepackage{makeidx}

\usepackage[titles]{tocloft}

\usepackage{fancyhdr}
\fancyhead{} % clear default layout
\fancyfoot{} % clear default layout
\fancypagestyle{mainmatter}{%
\fancyhead[L]{\sffamily \small \color{darkgray}\MakeUppercase{\bfseries        \leftmark}}  
\fancyhead[R]{\nouppercase \scshape \small \thepage}
}

\fancypagestyle{frontmatter}{%
\fancyhead[L]{}
\fancyhead[R]{}
\fancyfoot[R]{\thepage}
}

\begin{document}
\frontmatter
\pagestyle{frontmatter}% frontmatter page style
\clearpage
\tableofcontents
\clearpage % with tocloft we need this
\listoffigures
\clearpage % with tocloft we need this
\listoftables
\clearpage

\mainmatter
\pagestyle{mainmatter}

\chapter{A}
\kant
\chapter{B}
\kant
\chapter{C}
\kant
\end{document}

Best Answer

memoir provides the condition \if@mainmatter which you can use to condition on whether or not you're in the \mainmatter section of your document:

\documentclass[a4paper,11pt,titlepage,oneside,openany]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{kantlipsum}
% empty style for the first page of every chapter, except in \frontmatter
\makeatletter
\renewcommand\chapter{\if@openright\cleardoublepage\else\clearpage\fi
                \if@mainmatter
                  \thispagestyle{empty}% plain page style in mainmatter
                \else
                  \thispagestyle{plain}% empty page style outside of mainmatter
                \fi%
                \global\@topnum\z@
                \@afterindentfalse
                \secdef\@chapter\@schapter}
\makeatother

\usepackage{makeidx}

\usepackage[titles]{tocloft}

\usepackage{fancyhdr}
\fancyhead{} % clear default layout
\fancyfoot{} % clear default layout
\fancypagestyle{mainmatter}{%
\fancyhead[L]{\sffamily \small \color{darkgray}\MakeUppercase{\bfseries        \leftmark}}  
\fancyhead[R]{\nouppercase \scshape \small \thepage}
}

\fancypagestyle{frontmatter}{%
\fancyhead[L]{}
\fancyhead[R]{}
\fancyfoot[R]{\thepage}
}

\begin{document}
\frontmatter
\pagestyle{frontmatter}% frontmatter page style
\clearpage
\tableofcontents
\clearpage % with tocloft we need this
\listoffigures
\clearpage % with tocloft we need this
\listoftables
\clearpage

\mainmatter
\pagestyle{mainmatter}

\chapter{A}
\kant
\chapter{B}
\kant
\chapter{C}
\kant
\end{document}

While I've used empty and plain as the chapter page styles in the mainmatter and frontmatter respectively, you can update this to whatever you want, or even create your own new ones.

Also note that memoir provides functionality for creating headers/footers, so you don't need fancyhdr for this.

Related Question