2

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):

my target heading

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:

The chapter title with a hack

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

enter image description here

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}

1

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}}
\setsecheadstyle{\Large\bfseries\memRTLraggedright\color{white}}
\setsubsecheadstyle{\large\bfseries\memRTLraggedright\color{white}}

\makeatletter

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

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

% unnumbered chapters
\xpatchcmd{\@makeschapterhead}
  {\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

\chapter{Chapter heading}

\section{Section heading}

\subsection{Subsection heading}

\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*{Starred section heading}

\subsection*{Starred subsection heading}

\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*{Starred chapter heading}

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

\appendix

\chapter{Appendix chapter heading}

\section{Appendix section heading}

\subsection{Appendix subsection heading}

\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*{Starred appendix section heading}

\subsection*{Starred appendix subsection heading}

\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*{Starred appendix chapter heading}

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

\end{document}

Page 1 output:

Page 1 output


Page 2 output:

Page 2 output

| improve this answer | |
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.