5

I am trying to understand how to work with hooks in classes to change the font (size, color, etc.) for the first page of a chapter, and then switch to another style in the chapter pages following the first page. Hence, I have to hook to {cmd/chapter/before} in order to apply my style for the first page, but I am having a hard time figuring out what hook to use to catch when LaTeX does its first pagebreak. I tried {cmd/clearpage/after}, but according to report.cls, \chapter starts with a \clearpage, so the style for my first page gets overwritten. Here's a MWE class:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{CustomReport}[2024/05/15 Custom report class]

\LoadClassWithOptions{report}
\RequirePackage[letterpaper, portrait]{geometry}
\RequirePackage{xcolor}

\AddToHook {cmd/chapter/before}[.]{%
  
  %This part of the hook gets overwritten
  \color{red}%
  \fontsize{15}{24}\selectfont
  \parskip = 0.25in%
  
  \AddToHookNext{cmd/clearpage/after}{%
    \color{black}%
    \fontsize{10}{14}\selectfont
    \parskip = .5\baselineskip plus 2pt%
  }%
}%

Here's a sample document:

\documentclass{CustomReport}

\usepackage{lipsum}

\begin{document}

\chapter{First Chapter}\label{s:First}
\lipsum[1-10]

\chapter{Second Chapter}\label{s:Second}
\lipsum[1-3]

\chapter{Third Chapter}\label{s:Third}
\lipsum[1-10]

\end{document}

How can I hook to the next pagebreak to change the style for the rest of the chapter?

5
  • 1
    The first page of a chapter uses the page style plain in the standard classes. So the best approach is to redefine that page style accordingly. Note that I have a horrible feeling it's my fault you're trying to do it this way ... tex.stackexchange.com/q/717228. But there you wanted to change the colour of the pages, so you have to get the colour behind the content and that means you probably need to do it at ship out. But here you're trying to change the colour of the text, which is part of the content.
    – cfr
    May 16 at 3:46
  • 2
    Could you explain what you actually want the code you show to do?
    – cfr
    May 16 at 3:55
  • 3
    You can not change fonts or colour at an automatic page break as the text after the break has already been set. The page break can happen mid word in the middle of a paragraph, and the whole paragraph has already been set in the original font before line and page breaking happens May 16 at 5:53
  • @cfr, yes I'm trying to change the style of the content and I realize I can't do that at shipout, nor using the page styles. This is why I created this MWE, to better understand how I can hook during content processing. What I am trying to achieve is a chapter divider page that contains the beginning of the chapter's content.
    – drl
    May 16 at 11:47
  • This is your class so there is no reason to use \AddToHook {cmd/chapter/before} as you can simply define \chapter to have this code, there is no need to define it without the commands you need then use the command hook to patch in the missing settings. May 16 at 11:59

1 Answer 1

4

You can not change fonts or colour at an automatic page break as the text after the break has already been set. The page break can happen mid word in the middle of a paragraph, and the whole paragraph has already been set in the original font before line and page breaking happens.

7
  • Could you elaborate a bit more? I think this is where I get it wrong on how the content gets processed. If I do this in the document, it works (but not in the class with hooks): \chapter{First Chapter}\label{s:First} \color{red} \fontsize{15}{22}\selectfont \parskip = 0.25in% \lipsum[1] \clearpage \color{black}% \fontsize{10}{14}\selectfont \parskip = .5\baselineskip plus 2pt% \lipsum[2-10]
    – drl
    May 16 at 11:50
  • 2
    you can change fonts after a forced page break like \clearpage but (as far as I can tell) you are wanting the hooks to change the font wherever the page break on the first page happens to be. You can not do that and hooks are not appropriate here anyway as it is your class there is no need to hook into the \chapter command, you can simply define the command to do what you want. (but you can not change fonts at a page break) @drl May 16 at 11:57
  • I see, but redefining \chapter doesn't change the fact that I can't change fonts at a page break (that was inserted by LaTeX). In order to do that, perhaps I need to define two new commands, such as \ChapterDivider and \ChapterContent, so that content is properly separated. Would that be the proper way of doing this?
    – drl
    May 16 at 12:11
  • Perhaps I should redefine \chapter with an optional argument that contains the content to be added on the divider page.
    – drl
    May 16 at 12:51
  • 1
    @drl if your divider is separating some kind of abstract or prolog for the chapter many classes have specific commands for that such as \setchapterpreamble in the koma classes such as scrbook May 16 at 13:16

You must log in to answer this question.

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