1

I would like to create a custom report class (say MyCustomReport) based on the report class, where I can control more precisely the page styles applied throughout the document, so that the document would look something like this: Report example

I tried the fancyhdr package to modify the existing page styles (e.g., plain and empty), but that doesn't give me enough control to apply different styles to different parts of the report (for instance, title vs abstract vs normal chapter vs appendix, etc.)

I also created custom fancypagestyles, but I was unable to apply those in my custom class when for instance \abstract, \chapter, or \appendix are called (I guess my custom page styles get overridden by some \thispagestyle{plain} when they are called). I also want to put as much as possible in MyCustomReport, so that the authors would only need to write a document like this :

\documentclass{MyCustomReport}

\usepackage{lipsum}

\title{Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget}
\author{Jack Black and Jim Brown}
\date{May 2024}
\Abstract{\lipsum[1]}

\begin{document}

\chapter{Model}
\lipsum[1]
\section{My first model}
\lipsum[1-3]

\chapter{Results}
\lipsum[1-3]

\chapter{Conclusion}
\lipsum[1]

\appendix
\chapter{The first appendix}
    \lipsum[1-3]
\chapter{The second appendix}
    \lipsum[1-3]

\end{document}

Is this possible? Here's a MWE for MyCustomReport.cls, although I'm not sure if I'm going in the right direction (I would like the first chapter page to break before its first section, if the first section would land on the same page):

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{MyCustomReport}[2024/05/03 Custom report class]

\LoadClass{report}

\global\let\@abstract\@empty
\newcommand\Abstract[1]{\def\@abstract{#1}}

\RequirePackage[letterpaper, portrait, headheight=0.25in, headsep=0.5in, footskip=0.8in, margin=1in]{geometry}
\RequirePackage{fancyhdr}

\RequirePackage{graphicx}
\graphicspath{{./Images/}}

\RequirePackage{eso-pic}
\RequirePackage{pgfplots}
\RequirePackage{xcolor}
\definecolor{TitleColor}{RGB}{18,81,147}
\definecolor{AbstractColor}{RGB}{0,128,0}
\definecolor{ChapterColor}{RGB}{0,0,128}
\definecolor{AppendixColor}{RGB}{255,102,0}


\RequirePackage{sectsty}
\chapterfont{\color{white}}
\sectionfont{\color{blue}}
\subsectionfont{\color{black}}
\RequirePackage[colorlinks=true,citecolor=blue]{hyperref}

\fancypagestyle{TitlePage}{
    \AddToShipoutPictureBG*{\put(0.5in,0.5in){\begin{tikzpicture}
    \path [fill = TitleColor] (0.5in,0.5in) rectangle (\dimexpr\paperwidth-0.5in\relax,\dimexpr\paperheight-0.5in\relax);
    \end{tikzpicture}}}
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\footrulewidth}{0pt}
    \fancyfoot{}
}

\fancypagestyle{AbstractPage}{
    \AddToShipoutPictureBG*{\put(0.5in,0.5in){\begin{tikzpicture}
    \path [fill = AbstractColor] (0.5in,0.5in) rectangle (\dimexpr\paperwidth-0.5in\relax,\dimexpr\paperheight-0.5in\relax);
    \end{tikzpicture}}}
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\footrulewidth}{0pt}
    \fancyfoot{}
}

\fancypagestyle{ChapterPage}{
    \AddToShipoutPictureBG*{\put(0.5in,0.5in){\begin{tikzpicture}
    \path [fill = ChapterColor] (0.5in,0.5in) rectangle (\dimexpr\paperwidth-0.5in\relax,\dimexpr\paperheight-0.5in\relax);
    \end{tikzpicture}}}
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\footrulewidth}{0pt}
    \fancyfoot{}
}

\fancypagestyle{AppendixPage}{
    \AddToShipoutPictureBG*{\put(0.5in,0.5in){\begin{tikzpicture}
    \path [fill = AppendixColor] (0.5in,0.5in) rectangle (\dimexpr\paperwidth-0.5in\relax,\dimexpr\paperheight-0.5in\relax);
    \end{tikzpicture}}}
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\footrulewidth}{0pt}
    \fancyfoot{}
}

\renewcommand{\maketitle}{\@maketitle
}

\def\@maketitle{
    \thispagestyle{TitlePage}
    \raggedright
    \noindent{\color{white}\fontsize{32}{40}\selectfont{\@title} \par}
    \parskip = 1in
    \noindent{\color{white}\fontsize{15}{12}\selectfont{\@author} \par}
    \parskip = 8.5pt
    \noindent{\color{white}\fontsize{10}{12}\itshape\selectfont{\@date} \par}
    \vfill
    \newpage
    \normalsize
    \thispagestyle{AbstractPage}
}

\AtBeginDocument{
    \maketitle
    \vspace*{\fill}
    \color{white}\fontsize{35}{40}\selectfont{Abstract\\}
    \vspace{0.75in}
    \noindent{\color{white}\fontsize{15}{24}\selectfont{\parskip = 0.25in\@abstract} \par}
    \global\let\@abstract\@empty  
    \global\let\abstract\relax
    \newpage
    \color{blue}
    \normalsize
    \thispagestyle{plain}
}
1
  • 1
    I know you mean well, but you will not make users lives easier with this. Rather the reverse. Commented May 5, 2024 at 5:13

1 Answer 1

4

Don't do this. These types of classes cause users endless grief. You do them a disservice.

Caveat emptor

CODE EDITED to take account of comments and discussion of your followup question.

If for some bizarre reason you must (somebody has a gun to your head, your great aunt will be tortured with poorly made scones if you don't), don't prevent users from doing things correctly and avoid introducing spurious spaces, unneeded dependencies and so on. If this is for students completing an assignment on the use of pgfplots, the requirement may be reasonable. Otherwise not.

Even assuming your actual pages are more attractive than the colour blocks shown, have you considered whether those colours are suitable for everyone? Regardless of colour blindness or visual impairment? Why are you forcing users to use 10pt? Why are you ensuring they'll break things if they load almost any package which might interact with hyperref?

The formatting you're doing has nothing to do with page styles. fancyhdr does headers and footers. Better not to assume nobody will write \pagestyle{plain} and wonder what happened to the colours of set headers and ask why they don't work.

Use hooks. Pass options onto report.cls so users can use e.g. 11pt or draft. If you must load hyperref, do it in a hook so people can load other things, but tell them they need to add cleveref afterwards, if they want to use that. (So they'll need to add it to the hook and make sure it is executed later.) Similarly for bookmark.

By all means use eso-pic. I haven't because this was quicker and easier than figuring out which macro you need to change only the next picture.

\begin{filecontents*}[overwrite]{\jobname.cls}
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{MyCustomReport}[2024/05/03 Custom report class]

\LoadClassWithOptions{report}

\let\@abstract\@empty
\newcommand\Abstract[1]{\def\@abstract{#1}}

\RequirePackage[letterpaper, portrait, headheight=0.25in, headsep=0.5in, footskip=0.8in, margin=1in]{geometry}
\RequirePackage{fancyhdr}

\RequirePackage{graphicx}
\graphicspath{{./Images/}}

\RequirePackage{eso-pic}
% you are forcing users to use older code which
% lacks fixes & features of newer versions
\RequirePackage{pgfplots}
% \RequirePackage{xcolor}
% have you checked how these colours will work
% for people with any of the various types of
% colour blindness? or various kinds of visual
% impairment?
\definecolor{TitleColor}{RGB}{18,81,147}
\definecolor{AbstractColor}{RGB}{0,128,0}
\definecolor{ChapterColor}{RGB}{0,0,128}
\definecolor{AppendixColor}{RGB}{255,102,0}
\colorlet{ChapterFGColour}{white}
\colorlet{AppendixFGColour}{black}
\colorlet{BGColour}{gray!30}

\RequirePackage{calc}

\RequirePackage{sectsty}
\chapterfont{\color{white}}
\sectionfont{\color{blue}}
\subsectionfont{\color{black}}
% loading hyperref here is a spectacularly bad idea
% if your users load any packages from the large number which
% must be loaded before hyperref, things will break
% \RequirePackage[colorlinks=true,citecolor=blue]{hyperref}
\AddToHook{begindocument/before}[mycustomclass]{% consider bookmark
  \RequirePackage[colorlinks=true,citecolor=blue]{hyperref}}

% common
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\fancyfoot{}

\renewcommand{\maketitle}{%
  \thispagestyle{empty}%
  \AddToHookNext{shipout/background}{%
    \put(0.5in,-\paperheight+0.5in){%
      \begin{tikzpicture}
        \path [fill = TitleColor]  (0pt,0pt) rectangle +(\paperwidth-1in,\paperheight-1in);
      \end{tikzpicture}%
    }%
  }%
  \raggedright
  \noindent
  {\color{white}%
    \fontsize{32}{40}\selectfont
    \@title
    \par
  }%
  \parskip = 1in
  \noindent
  {\color{white}%
    \fontsize{15}{12}\selectfont
    \@author
    \par
  }%
  {\parskip = 8.5pt
    \noindent
    \color{white}%
    \fontsize{10}{12}\itshape
    \@date
    \par
  }%
  \vfill
  \newpage
  \normalsize
  \thispagestyle{empty}%
  \AddToHookNext{shipout/background}{%
    \put(0.5in,-\paperheight+0.5in){%
      \begin{tikzpicture}
        \path [fill = AbstractColor] (0,0) rectangle +(\paperwidth-1in,\paperheight-1in);
      \end{tikzpicture}%
    }%
  }%
}

\AtBeginDocument{%
  \maketitle
  \vspace*{\fill}
  \par
  \color{white}
  % \selectfont doesn't take an argument
  {\fontsize{35}{40}\selectfont
    Abstract
    \vspace{0.75in}
    \par
  }
  \noindent
  {\fontsize{15}{24}\selectfont
    \parskip = 0.25in
    \@abstract
    \par
  }%
  \global\let\@abstract\@empty  
  \global\let\abstract\relax
  \newpage
  \color{blue}%
  \normalsize
  \thispagestyle{plain}%
}

\AddToHook {cmd/appendix/after}[mycustomclass]{%
  \colorlet{ChapterColor}{AppendixColor}%
  \colorlet{ChapterFGColour}{AppendixFGColour}%
}
\AddToHook{shipout/background}[mycustomclass]{%
  \put(0.5in,-\paperheight+0.5in){%
    \begin{tikzpicture}
      \path [fill = BGColour] (0pt,0pt) rectangle +(\paperwidth-1in,\paperheight-1in);
    \end{tikzpicture}%
  }%
}
\newcommand\chapter@mod
{\if@openright\cleardoublepage\else\clearpage\fi
  \AddToHookNext{shipout/background}{%
    \put(0.5in,-\paperheight+0.5in){%
      \begin{tikzpicture}
        \path [fill = ChapterColor] (0pt,0pt) rectangle +(\paperwidth-1in,\paperheight-1in);
      \end{tikzpicture}%
    }%
  }%
  \thispagestyle{plain}%
  \global\@topnum\z@
  \@afterindentfalse
  \secdef\@chapter\@schapter}
\RenewDocumentCommand \chapter { O{#2} m +O{} }
{%
  \begingroup
    \color{ChapterFGColour}%
    \chapter@mod[#1]{#2}%
    #3
  \endgroup
  \clearpage
}
\end{filecontents*}
\documentclass{\jobname}

\usepackage{lipsum}

\title{Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget}
\author{Jack Black and Jim Brown}
\date{May 2024}
\Abstract{\lipsum[1]}

\begin{document}

\chapter{Model}[A brief abstract for the first page.]
\lipsum[1]
\section{My first model}
\lipsum[1-3]

\chapter{Results}[\lipsum[2]]
\lipsum[1-3]

\chapter{Conclusion}
\lipsum[1]

\appendix
\chapter{The first appendix}
\lipsum[1-3]
\chapter{The second appendix}
\lipsum[1-3]

\end{document}

Fortuitously, I'm unable to provide visual results due to a bug in Okular-on-X for which I am, in this case, unusually, grateful.

13
  • Thanks, hooks are definitely what I was looking for! As for the colors, they aren't the actual colors used by our graphic designer, which have a subtle variation in between parts of the document (and I haven't finished the font part yet). I agree that pgfplots is not a good requirement, I was using it to draw a background gradient, but I realize I only need tikz for that. The reason I want to separate as much as possible the content from the style is for easier document conversion with Pandoc (although, it will probably be harder than I think...) Commented May 5, 2024 at 12:41
  • How can I change the style (fonts, background, footer and header) for the pages following the first page of a chapter, if those pages exist (i.e., if the entire chapter body doesn't fit in the chapter's title page)? I think I need to catch the second shipout hook (if it exists) after the chapter command, but using a second \AddToHookNext{shipout/background} doesn't seem to work (it appears to overwrite the previous one). Commented May 13, 2024 at 15:57
  • 1
    As shown in the image above, I would like to apply a style to the first page of the chapter (as you did) and another style to the following pages (shown as grey pages). I was finally able to reproduce this by adding \AddToHook{shipout/background} inside (at the end) of the \AddToHookNext{shipout/background} you created. Is this the proper way of doing this? The \RemoveFromHook doesn't seem to be necessary, as the chapter title pages appear to use the \AddToHookNext{shipout/background} when the next \chapter starts and they get rendered correctly. Commented May 13, 2024 at 17:20
  • 1
    @drl If all the other pages should have the same style, I'd add that to \AddToHook{shipout/background} in the preamble/start of the relevant section. Otherwise, you'll end up with needless duplication of code chunks in the hook because every chapter will add the same code. But, yes, you can manipulate hooks in the code added to hooks etc. Indeed, it's frequently necessary or makes the most sense. Just remember that adding code to a hook is global. It accumulates. Only the Next version is more localised. (Still technically global but a one-time thing.) texdoc lthooks-doc/tshipout-doc. Commented May 13, 2024 at 18:42
  • 1
    @drl See edited code please. Commented May 17, 2024 at 21:50

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.