1

Having a book document class, I am trying to achieve having different footer/headers for the first page of every chapter.

Based on this answer instructions, I have defined the following custom style, using titlesec:

\newpagestyle{pageHeaderStyle}{%
  \setfoot
  {}
  {\vcenteredhbox{\confidentialClause}}
  {\thepage}
}

And then change the chapter first page style by using:

\renewcommand\chapter{\if@openright\cleardoublepage\else\clearpage\fi
      \thispagestyle{pageHeaderStyle}
      \global\@topnum\z@
      \@afterindentfalse
      \secdef\@chapter\@schapter} 

Everything works fine for every chapter except the ToC, which outputs CONTENTS at both header and footer, like below:

enter image description here

While the expected behaviour (that works fine in the rest of chapters) is:

enter image description here

4
  • \tableofcontents uses \chapter* which is apparently not catched with your titlesec approach
    – user31729
    Mar 6, 2017 at 17:11
  • @ChristianHupfer Any hints on how should I approach this, then? Thanks
    – otorrillas
    Mar 6, 2017 at 17:12
  • Well, not really without seeing your full code. Fragments aren't useful. I also doubt that titlesec and a redefinition of \chapter will go well together
    – user31729
    Mar 6, 2017 at 17:18
  • The \secdef macro \def\secdef#1#2{\@ifstar{#2}{\@dblarg{#1}}} causes \chapter to execute \@chapter and \chapter* to execute \@schapter. Mar 6, 2017 at 17:29

1 Answer 1

1

Here is a way to adapt \thispagestyle in the \chapter command 'dynamically' by replacing it with a \@currentthispagestyle, which is set with \SetCurrentPageStyle

I used fancyhdr (because I am more familiar with that package) and tcolorbox (since I don't know where \vcenteredhbox comes from)

Since \@currentthispagestyle defaults to plain, the ToC will use the plain style.

\documentclass{book}

\usepackage{xpatch}
\usepackage{xparse}
\usepackage[most]{tcolorbox}

\tcbset{confbox/.style={nobeforeafter,
    enhanced jigsaw,
    sharp corners, 
    boxrule=2pt,
    colframe=red,
    leftrule=0pt,
    rightrule=0pt,
    colback=white,
    colupper=red,
    fontupper=\large\bfseries}
}

\usepackage[headheight=35pt]{geometry}
\usepackage{blindtext}
\usepackage{fancyhdr}


\newcommand{\confidentialClause}{CONFIDENTIAL}


%Define a page style
\fancypagestyle{confidentialheader}{%
  \renewcommand{\headrulewidth}{0pt}
  \fancyhf{}
  \fancyhead[R]{\tcbox[confbox]{\confidentialClause}}
  \fancyfoot[C]{\thepage}
}
\makeatletter

\newcommand{\@currentthispagestyle}{plain}

\NewDocumentCommand{\SetCurrentPageStyle}{om}{%
  \cleardoublepage%
  \IfValueTF{#1}{%
    \renewcommand{\@currentthispagestyle}{#1}%
  }{%
    \renewcommand{\@currentthispagestyle}{#2}%
  }%
  \pagestyle{#2}%
}

% Perform a patch to change 'plain' to `\@currentthispagestyle`
\AtBeginDocument{%
   \xpatchcmd{\chapter}{\thispagestyle{plain}}{\thispagestyle{\@currentthispagestyle}}{}{}
}



\makeatother


\begin{document}
\tableofcontents
\SetCurrentPageStyle{confidentialheader}
\chapter{Foo}
\blindtext[20]

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .