[Tex/LaTex] customizing section formatting using memoir class (color and numbering)

memoirsectioning

I see in memoir that \section is implemented like so:

\newcommand{\section}{%
  \sechook%
  \@startsection{section}{1}%  level 1
      {\secindent}%            heading indent
      {\beforesecskip}%        skip before the heading
      {\aftersecskip}%         skip after the heading
      {\normalfont\secheadstyle}} % font
\newcommand{\sechook}{}
\newcommand{\setsechook}[1]{\renewcommand{\sechook}{#1}}
\newlength{\secindent}
\newcommand{\setsecindent}[1]{\setlength{\secindent}{#1}}
  \setsecindent{\z@}
\newskip\beforesecskip
\newcommand{\setbeforesecskip}[1]{\setlength{\beforesecskip}{#1}}
  \setbeforesecskip{-3.5ex \@plus -1ex \@minus -.2ex}
\newskip\aftersecskip
\newcommand{\setaftersecskip}[1]{\setlength{\aftersecskip}{#1}}
  \setaftersecskip{2.3ex \@plus .2ex}
\newcommand{\secheadstyle}{}
\newcommand{\setsecheadstyle}[1]{\renewcommand{\secheadstyle}{#1}}
%%%  \setsecheadstyle{\Large\bfseries\raggedright}
  \setsecheadstyle{\Large\bfseries\memRTLraggedright}

I thought I'd try color'izing my section numbers, and first tried:

\renewcommand{\sechook}{\color{blue}}

This didn't work, since the scope of the blue was everything after and including the section. Next I tried:

\setsecheadstyle{\color{blue}\Large\bfseries\memRTLraggedright}

This is better, but color'izes the section number and the section title. I can't see in the \section implementation above where the counter is, so I think I'm looking in the wrong place.

Here's a MWE for my busted attempt:

\documentclass[openany]{memoir}
\usepackage{color}

\setsecheadstyle{\color{blue}\Large\bfseries\memRTLraggedright}

\begin{document}

\chapter{Foo}
\section{blah}
\section{stuff}
\subsection{more stuff}
\subsubsection{even more stuff}

\end{document}

This produces

attempt at colorizing

I also see that I've lost subsection and subsubsection numbers (which I had with the book class). So two questions:

  • How can I get back my sub and subsub section numbers?
  • How would one color just the section numbers (and subsection numbers once I get them back)?

Best Answer

To control the level up to which sectional units will be numbered, you can use \setsecnumdepth{<sectional unit>}; to color the sectional unit numbers, you can use \setsecnumformat:

\documentclass[openany]{memoir}
\usepackage{xcolor}

\setsecnumformat{\color{blue}\csname the#1\endcsname\quad}
\setsecnumdepth{subsubsection}

\begin{document}

\chapter{Foo}
\section{blah}
\section{stuff}
\subsection{more stuff}
\subsubsection{even more stuff}

\end{document}

enter image description here

Related Question