0

It seems to me that titlesec is failing to set \chapter heading styles in documents.

The documentation says that it should have support to set \chapter heading styles.

Here is a minimum reproducible example:

\documentclass[12pt,letterpaper]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage[compact]{titlesec}

\titleformat{\chapter}[hang]
{\centering\bfseries\huge}
{}
{0pt}
{\titlerule[0.8pt]}[]
\titlespacing{\chapter}
{0pt}
{0pt}
{0pt}[0pt]

\titleformat{\section}[hang]
{\centering\Large}
{}
{0pt}
{\titlerule[1.6pt]}[]
\titlespacing{\section}
{0pt}
{0pt}
{0pt}[0pt]

\begin{document}
\chapter{CHAPTER TEXT}\label{chapter-text}
\section{SECTION TEXT}\label{section-text}
\end{document}

This fails both in my local installation of PDFLatex and in Overleaf. screenshot of the error in overleaf

Perhaps I am missing something. Does anyone have a clue as to what might resolve this issue for me? Thank you!

2
  • 2
    the article class has no chapters. Did you try with report or book? Commented Feb 28 at 20:45
  • @UlrikeFischer Using \documentclass{report} or \documentclass{book} does fix the issue! I did not know that the article class has no support for chapters. Thank you! Would you mind writing an answer so that I can mark this question as answered? Commented Feb 28 at 21:18

2 Answers 2

0

The issue was that the article document class does not define \chapter headings, and therefore it is not possible to give them styles.

The solution (Suggested in a comment by @Ulrike Fischer) is to switch to another document class, such as report or book. This works!

I chose to switch to the report document class. Note that this caused a migration issue: my headers (set by \pagestyle) refused to work on the same page as the \chapter heading. I found the solution to this issue in another forum post here: https://tex.stackexchange.com/a/19741/315958

0

You could instead wrap your definition for \chapter inside an \@ifundefined (note to enclose this with \makeatletter ... \makeatother).

\makeatletter
\@ifundefined{chapter}{}{
    \titleformat{\chapter}[hang]
    {\centering\bfseries\huge}
    {}
    {0pt}
    {\titlerule[0.8pt]}[]
    \titlespacing{\chapter}
    {0pt}
    {0pt}
    {0pt}[0pt]
}
\makeatother

This way, you can keep the \chapter style independently of the documentclass.


Clarification: This solution was never meant to provide the \chapter command for article class, but to keep the \chapter style redefinitions regardless of the document class used. As @UlrikeFischer mentioned, article does not provide \chapter, and this is for a reason, which also justifies the existence of book and report. I personally find it very wrong to provide chapters in articles.

However, as OP asked, one can use titlesec and titletoc to define a \chapter command. This could be done as follows (I tried to be close to the original, might not be perfect):

\documentclass{article}

\usepackage{titlesec}
\usepackage{titletoc}

\newcommand{\chaptername}{Chapter}
\makeatletter
\newcommand{\@chapapp}{\chaptername}
\makeatother

\titleclass{\chapter}{top}[\part]
\newcounter{chapter}

\titleformat{\chapter}[display]
    {\bfseries\huge}
    {\chaptertitlename\space\thechapter}
    {20pt}
    {\Huge\bfseries}

\titlespacing*{\chapter}
    {0pt}
    {50pt}
    {40pt}

\titlecontents{chapter}
    [1.5em]
    {\vspace*{10pt}\bfseries}
    {\contentslabel{1.3em}}
    {\hspace*{-1.3em}}
    {\hfill\contentspage}

\AddToHook{cmd/appendix/before}{
    \setcounter{chapter}{0}
    \gdef{\thechapter}{\Alph{chapter}}
}

\begin{document}

\chapter{A}

\end{document}

Note however that this is wrong on many levels. For example, the book and report classes use \chapter* to automatically issue the headings of \tableofcontents, \begin{thebibliography}{...}, \printindex, etc., while the article class uses \section* for this. Moreover, I have not accounted for markings which are used in the header of each page (similar to other information like page number). The book and report classes define \chaptermark for this, but obviously article does not have that.

2
  • This is not a solution for the question asked. The code here does not implement the \chapter heading styles in a document class where \chapter is missing. This code checks if the \chapter command exists before declaring styles for it, but does not implement \chapter otherwise. So the problem persists - there is still no \chapter heading in the article document class so \chapter is unable to be styled. Please update your answer to include a way to "keep the \chapter style independently of the documentclass." or change the claims of your post to make it correct. Commented Mar 1 at 21:42
  • This is useful when creating a preamble that could be used for any document class, but again, it does not solve the problem that was given. Maybe update the answer to say that. Commented Mar 1 at 21:47

You must log in to answer this question.

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