7

Like the question title says, is it possible to customize only the section and subsection numbers or titles in memoir? I know how to change the font for the entire block, but I would like to make the section numbers a bit smaller compared to the section title and to keep the subsection numbers upright while having the subsection title slanted.

Also, I would like to be able to do some stuff to the title without affecting the numbers.

2 Answers 2

9

It's sufficient to define suitably the "secnumformat"; its usual setup is

\setsecnumformat{\csname the#1\endcsname\quad}

so we can modify in such a way that it executes different macros, for which I choose the names \gablinsection, \gablinsubsection and so on.

\documentclass{memoir}

\setsecnumformat{\csname gablin#1\endcsname\quad}
\newcommand{\gablinsection}{{\small\thesection}}
\newcommand{\gablinsubsection}{{\footnotesize\thesubsection}}
\newcommand{\gablinsubsubsection}{{\scriptsize\thesubsubsection}}
\setsecnumdepth{subsubsection}

\begin{document}
\section{Test me}
\subsection{Another test}
\subsubsection{Test me too}
\end{document}

The idea comes from UK TUG FAQ.

2
  • A much better idea!
    – Ant
    Sep 27, 2011 at 13:38
  • That is a good idea
    – daleif
    Sep 27, 2011 at 13:55
4

You can define the secnumformat separately for each section level by adding a hook to the relevant sectioning command:

\headstyles{bringhurst}
% I don't know what style you're using but chose this one since it
% has italic subsection titles - the same principles apply to any 
% set of heading styles.
\setsecnumdepth{subsubsection}
% Make sure subsections are numbered

\let\oldsection=\section
\let\oldsubsection=\subsection
% Save the existing sectioning commands

\renewcommand{\section}[1]{{%
  \setsecnumformat{\footnotesize\thesection\quad}%
  \oldsection{#1}}}
\renewcommand{\subsection}[1]{{%
  \setsecnumformat{{\upshape\thesubsection\quad}}%
  \oldsubsection{#1}}}
% New sectioning commands use \setsecnumformat to
% redefine the number font locally. Note the extra
% pair of curly brackets to make sure that the
% \secnumformat returns to normal after the (sub)section
% definition.

Then

\begin{document}
\section{Test me}
\subsection{Another test}
\subsubsection{Test me too}
\end{document}

gives

a set of headings with the required numbering fonts

(the final subsubsection is to show that we haven't changed the numbering font globally).

(On a typographic note, though, it's possible that what you actually want for section numbers is not small numerals but "old style" ones - if they're available in the font you use.)

Edit

@daleif has pointed out that the cunning extra groups have the unfortunate effect of breaking cross-references. An alternative that avoids this is

\makeatletter
\let\oldsection=\section
\let\oldsubsection=\subsection
\renewcommand{\section}[1]{%
  \let\oldsecnumformat=\@seccntformat%
  \setsecnumformat{{\footnotesize\thesection\quad}}%
  \oldsection{#1}%
  \let\@seccntformat=\oldsecnumformat}
\renewcommand{\subsection}[1]{%
  \let\oldsecnumformat=\@seccntformat%
  \setsecnumformat{{\upshape\thesubsection\quad}}%
  \oldsubsection{#1}%
  \let\@seccntformat=\oldsecnumformat}
\makeatother

where, instead of defining a group within which the secnumformat is changed, the old format is explicitly saved and then restored, so that cross-references still work.

I note also that, if you don't care about restoring the old format (i.e., if you're happy to explicitly define a format for every section level you use) an easier method than the first one I showed would be

\setsechook{\setsecnumformat{{\footnotesize\thesection\quad}}}
\setsubsechook{\setsecnumformat{{\upshape\thesection\quad}}}
3
  • 1
    I'll add this to my todo list. But the solution above may have a side effect, it will break \label because the data \label use is locked inside the extra group
    – daleif
    Sep 27, 2011 at 10:52
  • True! I guess the best thing to do in that case is explicitly save the old secnumformat and restore it afterwards. I'll edit to suggest this.
    – Ant
    Sep 27, 2011 at 11:36
  • 1
    Unfortunately you'll lose all the facilities regarding optional arguments to \section and siblings.
    – egreg
    Sep 27, 2011 at 12:17

You must log in to answer this question.

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