# Memoir chapter style with with background color spreading across text / number

Greetings to the community!

My goal is to create the pdflatex document using the memoir class, which would have \chapters, \sections, and \subsections have the following formatting (font is not important, placing heading on the colored background is important):

i.e. the \chapter*{Features} has a colored background spread across the linewidth, and if there is a number, it is on the colored background as well.

After a bit of googling, I got smth like:

\renewcommand\chaptitlefont{%
\huge\bfseries%
\leavevmode\color{white}%
}

\renewcommand\printchapternum{
\huge\bfseries%
\leavevmode\color{white}%
{\thechapter}
}


with combination of defining chapter like this:

\chapter*{\colorbox{my-blue}{\strut\rlap{\textbf{Executive Summary}}\hspace{\linewidth}\hspace{-2\fboxsep}}}


Which results in the followings:

1. The chapter title with a hack:

1. ToC (white text over white background, I selected some of ):

My hack has the following problems:

• Titles of the chapters that I do not create explicitly (e.g. Contents) have white font over white background;

• Titles of the chapters that I do name explicitly in ToC got also blue background, which is not desired.

• Chapter titles do not include the Chapter number. Instead of Introduction and Syntax (i.e. all the chapters created with \chapter) as a chapter title, my goal is to have 1 Introduction, 2 Syntax, 3 Semantics (when using \chapter opposite to \chapter*)

My questions are as follows:

1. How would I create the chapter titles, that have background color spanned over chapter title text, including chapter number.

2. How in this case (1) keep the formatting of chapters that I do not name explicitly (e.g. Contents, List of Figures, List of Tables, etc.) consistent?

3. How to avoid that the background color is also shown in the ToC?

Thanks in advance for any help / pointers!

The minimal example is below:

\documentclass[a4paper,12pt,oneside]{memoir}
\usepackage[english]{babel}
\usepackage{xcolor}
\definecolor{my-blue}{RGB}{0,150,226}

\renewcommand\chaptitlefont{%
\huge\bfseries%
\leavevmode\color{white}%
}

\renewcommand\printchapternum{
\huge\bfseries%
\leavevmode\color{white}%
{\thechapter}
}

\begin{document}
\frontmatter
\pagenumbering{arabic}
\tableofcontents*
\clearpage

\chapter*{\colorbox{my-blue}{\strut\rlap{\textbf{Summary}}\hspace{\linewidth}\hspace{-2\fboxsep}}}
\chapter{\colorbox{my-blue}{\strut\rlap{\textbf{Introduction}}\hspace{\linewidth}\hspace{-2\fboxsep}}}
\chapter{Syntax}
\chapter{Semantics}
\end{document}



Here's a tikz solution that patches a bunch of the memoir class internal macros.

\documentclass[openany]{memoir}

\usepackage{tikz}
\usepackage{xpatch}

\definecolor{my-blue}{RGB}{0,150,226}

\chapterstyle{reparticle}
\renewcommand*{\chaptitlefont}{\normalfont\Large\bfseries\color{white}}

\makeatletter

\newcommand{\@selyunin@box}[1]{%
\tikz\node[fill=my-blue, inner sep=1mm, text width=\linewidth-2mm]{#1\strut\par};}

% numbered chapters
{\ifm@m@And
\afterchapternum
\else
\printchapternonum
\fi
\interlinepenalty\@M
\printchaptertitle{#1}}
{\@selyunin@box{%
\ifm@m@And
\afterchapternum
\else
\printchapternonum
\fi
\interlinepenalty\@M
\printchaptertitle{#1}}}
{}
{}

% unnumbered chapters
{\printchaptertitle{#1}}
{\@selyunin@box{\printchaptertitle{#1}}}
{}
{}

% TOC, LOF, and LOT headings
\xpatchcmd{\@tocmaketitle}
{\@nameuse{printtoctitle}{\contentsname}}
{\@selyunin@box{\@nameuse{printtoctitle}{\contentsname}}}
{}
{}
\xpatchcmd{\@lofmaketitle}
{\@nameuse{printloftitle}{\contentsname}}
{\@selyunin@box{\@nameuse{printloftitle}{\contentsname}}}
{}
{}
\xpatchcmd{\@lotmaketitle}
{\@nameuse{printlottitle}{\contentsname}}
{\@selyunin@box{\@nameuse{printlottitle}{\contentsname}}}
{}
{}

% numbered sections
\xpatchcmd{\M@sect}
{\@hangfrom{\hskip #3\relax\@svsec}%
\interlinepenalty \@M #9\@@par}
{\@selyunin@box{%
\@hangfrom{\hskip #3\relax\@svsec}%
\interlinepenalty \@M #9}}
{}
{}

% unnumbered sections
\xpatchcmd{\@mem@old@ssect}
{\@hangfrom{\hskip #1}%
\interlinepenalty \@M #5\@@par}
{\@selyunin@box{%
\@hangfrom{\hskip #1}%
\interlinepenalty \@M #5}}%
{}
{}

\makeatother

\begin{document}

\tableofcontents

\section{A very long section heading that needs to go on to the next line}

\subsection{A very long subsection heading that needs to go on to the next line}

\section*{A very long starred section heading that needs to go on to the next line}

\subsection*{A very long starred subsection heading that needs to go on to the next line}

\chapter{A very long chapter heading that needs to go on to the next line}

\chapter*{A very long starred chapter heading that needs to go on to the next line}

\appendix

\section{A very long appendix section heading that needs to go on to the next line}

\subsection{A very long appendix subsection heading that needs to go on to the next line}

\section*{A very long starred appendix section heading that needs to go on to the next line}

\subsection*{A very long starred appendix subsection heading that needs to go on to the next line}

\chapter{A very long appendix chapter heading that needs to go on to the next line}

\chapter*{A very long starred appendix chapter heading that needs to go on to the next line}

\end{document}


## Page 2 output:

• Thank you very much, that is exactly I was looking for! – selyunin Jan 6 at 10:51
• I have one more question: whenever I try to include the \usepackage{hyperref} hyperref package, the sections and subsections loose the patched formatting (i.e. become white over white). Is there a way to use the hyperref package as well? – selyunin Jan 7 at 16:26
• OK, I have figured it out, the  \usepackage{hyperref} should be after the patched commands, and not before – selyunin Jan 8 at 4:26
• @selyunin, yes hyperref will patch things too and should usually be loaded as late as possible. – David Purton Jan 8 at 9:57