1

I'm aware that the book class implements the \@mainmatter variable which can be used in \if \fi conditionals.

On a separate topic, I'm trying to achieve different chapter titles for front/backmatter and mainmatter while using the package titlesec. I want the first ones to be centered while the later to be aligned to the right.

This is the code I was using for mainmatter, which I really like (taken from this site, but didn't write down the answer ID).

\titleformat{\chapter}[display]
  {\bfseries\Large\color{\mychapterrulecolor}}
  {\filright\MakeUppercase{\chaptertitlename} %
      \textcolor{\chaptertextcolor}{\Huge\thechapter}}
  {1ex}
  {\titlerule\vspace{2ex}\makeatletter\filleft}
  [\textcolor{\mychapterrulecolor}{\vspace{2ex}\titlerule}]

This produces the following output

which is ugly for not numbered chapters (sorry, didn't take a pic)

If I change it to

\titleformat{\chapter}[display]
  {\bfseries\Large\color{\mychapterrulecolor}}
  {\filright\MakeUppercase{\chaptertitlename} %
      \textcolor{\chaptertextcolor}{\Huge\thechapter}}
  {1ex}
  {\titlerule\vspace{2ex}\makeatletter\filcenter}
  [\textcolor{\mychapterrulecolor}{\vspace{2ex}\titlerule}]

I get what I want for not numbered chapters

but numbered chapters are kind of ugly

So I tried something like

\makeatletter
\titleformat{\chapter}[display]
  {\bfseries\Large\color{\mychapterrulecolor}}
  {\filright\MakeUppercase{\chaptertitlename} %
      \textcolor{\chaptertextcolor}{\Huge\thechapter}}
  {1ex}
  {\titlerule\vspace{2ex}\makeatletter\filcenter\if\@mainmatter\filright\fi}
  [\textcolor{\mychapterrulecolor}{\vspace{2ex}\titlerule}]
\makeatother

But that results in a centered layout with "mainmatter" (textually) preceding the first word of the chapter name (and some compilations errors, of course).

I'd like to find a fix to deal with this in titlesec (without touching book class primitives). One obvious solution is to rename my unnumbered chapters to chapter* and manually add them to the TOC. But I'm interested in a simple solution that doesn't require to change things anything else (my titlesec commands are in their own "mystyle.tex"). I couldn't find anything in titlesec documentation.

I'm obviously way over my abilities here (I think that usage of \makeatletter before calling titleformat is an indication of that).

Does someone knows how to achieve this?

Cheers,

2

The conditional is \if@mainmatter; the \makeatletter inside \titleformat does nothing.

\documentclass[oneside]{book} % oneside is just for easier comparison
\usepackage{xcolor,titlesec}

\definecolor{mychapterrulecolor}{rgb}{0,0,1}
\definecolor{mychaptertextcolor}{rgb}{0,0,1}

\makeatletter
\titleformat{\chapter}[display]
  {\bfseries\Large\color{mychapterrulecolor}}
  {\MakeUppercase{\chaptertitlename} \Huge\thechapter\filright}
  {1ex}
  {\titlerule\vspace{2ex}\filleft\if@mainmatter\else\filright\fi
   \color{mychaptertextcolor}}
  [\color{mychapterrulecolor}\vspace{2ex}\titlerule]
\makeatother

\begin{document}

\frontmatter

\chapter{Introduction}

Some text just to see

\mainmatter

\chapter{A longer chapter title}

Some text just to see

\end{document}

enter image description here

  • Oops, that's the problem of copy&paste. I had tried \makeatletter inside \titleformat as a last resource and stuck in the copy&paste. I've tried with \makeatletter outside first, of course, and couldn't make it work. Will try your example and report back. Thanks a lot!!!! – Sabian Feb 28 '17 at 22:22
  • Confirmed. That worked. I was so scared of making a LaTeXcide with \makeatletter before \titleformat (thought it was protected or something) that I didn't check for correct usage of conditional. Works like a charm. Thanks a lot @egreg! – Sabian Feb 28 '17 at 22:33
1

If think that titlesec automatically replaces \@mainmatter by ttl@label

Does this works for you ?

\documentclass{book}

\usepackage{xcolor}

\usepackage{titlesec}

\makeatletter
\titleformat{\chapter}[display]
  {\bfseries\Large\color{blue}}
  {\filright\MakeUppercase{\chaptertitlename} %
      \textcolor{blue}{\Huge\thechapter}}
  {1ex}
  {\titlerule\vspace{2ex}\makeatletter\ifttl@label\filleft\else\filcenter\fi}
  [\textcolor{blue}{\vspace{2ex}\titlerule}]
\makeatother

\begin{document}

\chapter{Numbered}

\chapter*{Unnumbered}

\end{document}
  • As pointed out by egreg, @mainmatter still works, and ttl@label seems to be the same. – mvienney Feb 28 '17 at 22:15
  • Indeed, @mainmatter worked. Sorry I wasted both you and egreg time with such dumb mistake. – Sabian Feb 28 '17 at 22:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.