7

I am tinkering with the Hansen chapter style for the memoir class, as reviewed and listed in texblog. The Hansen style is unfortunately not discussed in the memoir class documentation.

I changed it to be left-aligned and use \sfstyle chapter title font. I would now like to control the color of the chapter title.

My current attempt:

\documentclass[12pt,english,american]{memoir}
\makeatletter
\usepackage{color} 

\usepackage{kpfonts}
\usepackage{calc}
\definecolor{ChapNumColor}{gray}{0.90}
\definecolor{ChapTitleColor}{gray}{0.65}
% helper macros
\newcommand\numlifter[1]{\raisebox{-18mm}[0pt][0pt]{\smash{#1}}}
\newcommand\numindent{\kern08mm}
\newlength\chaptertitleboxheight
\makechapterstyle{hansen}{
  \renewcommand\printchaptername{%
    \color{ChapTitleColor}%
  %\raggedleft
  }
  \renewcommand\printchapternum{%
    \begingroup%
    \leavevmode%
    \chapnumfont%
    \strut%
    \numindent%
    \numlifter{\thechapter}%
    \endgroup%
  }
  \renewcommand*{\printchapternonum}{%
    \vphantom{\begingroup%
      \leavevmode%
      \chapnumfont%
    \numlifter{\vphantom{9}}%
    \numindent%
    \endgroup}
  \afterchapternum}
  \setlength\midchapskip{0pt}
  \setlength\beforechapskip{0.5\baselineskip}
  \setlength{\afterchapskip}{3\baselineskip}
  \renewcommand\chapnumfont{%
    \fontsize{40mm}{0mm}%
    \bfseries%
    \fontfamily{\sfdefault}\selectfont%
    \color{ChapNumColor}%
  }
  \renewcommand\chaptitlefont{%
    \huge%
    \sffamily%
    %\color{ChapTitleColor}%
    %\raggedleft%
  }%
  \settototalheight\chaptertitleboxheight{%
  \parbox{\textwidth}{\chaptitlefont \strut bg\\bg\strut}}
  \renewcommand\printchaptertitle[1]{%
    \parbox[t][\chaptertitleboxheight][t]{\textwidth}{%
    %\microtypesetup{protrusion=false}% add this if you use microtype
    \chaptitlefont\strut ##1\strut}%
  }
}
\chapterstyle{hansen}

\makeatother

\usepackage{babel}
\begin{document}
\chapter*{Preface}
content
\chapter{Introduction}
content
\chapter{Conclusion}
content
\end{document}

This correctly colors numbered \chapters but not unnumbered \chapter*s, including the table of contents heading.

option 1

If I comment the statement \color{ChapTitleColor} in the macro \printchaptername and instead uncomment the color statement in the macro \chaptitlefont further below, I do get the right coloring but now the chapter title has been pushed down by a line or so.

option 2

Perhaps I have missed an end-of-line percent mark somewhere, as I am relatively unpractised at TeX's syntax.

(NB I do not know for sure if this applies to the \chapter* although I have indicated this in the image. Can I overlay a ruler grid on a latex page to check these things?)

How can I fix option 1 or option 2 without affecting the formatting?

What is the "correct" way to color this style, as intended by the developer?

1 Answer 1

8

You were on the right track. You can use the \chaptertitlefont modification only, just add a \leavevmode before the color command.

  \renewcommand\chaptitlefont{%
    \huge%
    \sffamily%
    \leavevmode\color{ChapTitleColor}%
    %\raggedleft%
  }%

Since you may be tempted to ask about what \leavevmode actually does, here's a link to an explanation:

BTW, it's probably better to use xcolor instead of color.

1
  • Thank you @Alan Munn for going above and beyond, the link to the \leavemode thread and advice on xcolor are very welcome.
    – Andreas
    Dec 11, 2012 at 1:21

You must log in to answer this question.

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