11

How could I adjust the size and placement of chapter heading in my custom class? I am using report class as my base. When I used titlesec package inside my custom class, it threw error

! Package titlesec Error: Not allowed in `easy' settings.

tex
  • 3,445

2 Answers2

13

The error message indicates that you were trying to use the "easy setup" with \titleformat* in a wrong way. For substantial formatting modifications, use the non-starred variant \titleformat and for changing the spacing, use \titlespacing*. Below are the default settings, which you can change according to your needs:

\usepackage{titlesec}

\titleformat{\chapter}[display]
  {\normalfont\huge\bfseries}{\chaptertitlename\ \thechapter}{20pt}{\Huge}
\titlespacing*{\chapter}
  {0pt}{50pt}{40pt}

For example, to center the title, reduce the space before from 50pt to 30pt and the space after from 40pt to 20pt, you can do:

\documentclass{report}
\usepackage{titlesec}
\usepackage{lipsum}

\titleformat{\chapter}[display]
  {\normalfont\huge\bfseries\filcenter}{\chaptertitlename\ \thechapter}{20pt}{\Huge}
\titlespacing*{\chapter}
  {0pt}{30pt}{20pt}

\begin{document}
\chapter{Test Chapter}
\lipsum[4]
\end{document}

enter image description here

Gonzalo Medina
  • 505,128
  • How could I place it at Center? – tex Sep 19 '13 at 04:51
  • @user3984 please see my updated answer. – Gonzalo Medina Sep 19 '13 at 04:53
  • Why does the default use pt instead of em. shouldn't ems be used to scale in case of a huge paper sheet? – Mario S. E. Nov 01 '13 at 00:33
  • @GonzaloMedina Is the first snippet really the default? If I have a chapter followed by a section, I get different vertical spacing with and without that snippet. – LaRiFaRi Jul 15 '15 at 07:42
  • @LaRiFaRi That's what the titlesec documentation says (page 23). However, you are right. Even with regular text after the title, there's a slight difference in vertical spacing between the default settings and what titlesec states is the configuration thet replicates the standard settings. – Gonzalo Medina Jul 15 '15 at 13:13
  • But also points out there is no way to catch the original \chapter and therefore one similar to standard classes is used (ie, not necessarily the same). On the first page, the manual also says "Since the sectioning commands are rewritten, their behaviour could be somewhat different in some cases". This also imposes some other restrictions when redefining \chapter (as explained in the manual, which could require using \titleclass). Vertical spacing could be slightly different from standard classes because titlesec adds a couple of struts in titles. – Javier Bezos Jul 22 '15 at 10:27
  • @JavierBezos Yes, you are right. I think, however, that a little reminder about these differences on section 9.2. Standard Classes would be helpful. Right now it just says "Now follows, for your records, how sectioning commands of standard classes could be defined." which could imply that the definitions will produce exactly the definitions on the standard classes. – Gonzalo Medina Jul 22 '15 at 15:33
  • @GonzaloMedina Fair point. – Javier Bezos Jul 22 '15 at 17:06
3

The regular (numbered) chapter header in report is set by \@makechapterhead:

\def\@makechapterhead#1{%
  \vspace*{50\p@}%
  {\parindent \z@ \raggedright \normalfont
    \ifnum \c@secnumdepth >\m@ne
        \huge\bfseries \@chapapp\space \thechapter
        \par\nobreak
        \vskip 20\p@
    \fi
    \interlinepenalty\@M
    \Huge \bfseries #1\par\nobreak
    \vskip 40\p@
  }}

You see a 50\p@ (or 50pt) vertical gap before setting \@chapapp\space \thechapter in \huge\bfseries; then another 20\p@ gap and the title in \Huge\bfseries, and yet another 40\p@. Starred chapters are set using \@makeschapterhead:

\def\@makeschapterhead#1{%
  \vspace*{50\p@}%
  {\parindent \z@ \raggedright
    \normalfont
    \interlinepenalty\@M
    \Huge \bfseries  #1\par\nobreak
    \vskip 40\p@
  }}

You can change these values to your liking.

Werner
  • 603,163
  • Should one use this code in a .cls file? –  Jul 25 '19 at 12:50
  • 1
    You could. But if you want to use it in your regular document, you need to put it inside a \makeatletter...\makeatother pair. See What do \makeatletter and \makeatother do? – Werner Jul 25 '19 at 15:08
  • Thanks for the info! –  Jul 26 '19 at 01:13
  • The \makeatletter ... \makeatother pair seems to be rather old fashioned, isn't it? I am quite curious to master such techniques, but not sure where to start from. Again, thanks for the link. I have seen such pair in .cls files and lots of answers this site; I can only use it as a black box but can never(!) write my own whenever it may be useful. It seems to me this is an advanced (La)TeX tool that is more straightforward (in some sense?) than packages. –  Jul 26 '19 at 01:29
  • 1
    @Jack: You don't need \makeatletter...\makeatother within a package or class. But in your preamble you would need it if any of your macro names include an @. You see that I'm defining macros \@makechapterhead and \@makeschapterhead and within them they also contain macros with @ in them. It's not really that advanced. – Werner Jul 26 '19 at 16:28