4

I'm trying to define a \preface command that would be identical to \chapter except that

  • It should not increment \thechapter counter.
  • In the TOC, "Preface" should go where "Chapter N" would normally go.
  • In the footers, I want "Preface - {chapter title}" instead of "Chapter N - {chapter title}"

Since I'm using titlesec anyway, I figured I'd try out titleclass for the first time. The moment I added the preface titleclass, however, my section level entries disappeared from the TOC!

After a few hours of debugging, I'm not making any headway. Any idea what's going on here?

Here's a MWE:

\documentclass[oneside]{book}

\usepackage{blindtext}
\usepackage{titlesec}
\usepackage{titletoc}
\usepackage{hyperref}
\usepackage{xcolor}

% use arabic numbering throughout
\makeatletter
\renewcommand{\frontmatter}{\cleardoublepage \@mainmatterfalse}
\renewcommand{\mainmatter}{\cleardoublepage \@mainmattertrue}
\makeatother

%%%% begin preface defs %%%%

% see http://tex.stackexchange.com/a/17278/2041 and the titlesec manual
\titleclass{\preface}{top}[\part]

\newcounter{preface} % required when defining a new titleclass; accessible via \thepreface

\titleformat{\preface}
    {\huge\bfseries}{}{0em}{}

% TODO verify that these dimensions make sense
\titlespacing*{\preface}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}

% see autoref in hyperref manual
\newcommand{\prefaceautorefname}{preface}

%%%% end preface defs %%%%

%\setcounter{secnumdepth}{2} % I'm not sure if this is appropriate

\setcounter{tocdepth}{1} % only go down to the section level in TOC

%%%% begin TOC formatting %%%%

\titlecontents{chapter}
              [3pt]
              {\addvspace{1pc}}%
              {\contentsmargin{0pt}%
               \bfseries\large%
               \llap{\makebox[1.5in]{%
                   {\color{gray}Chapter~\thecontentslabel}\hfill\textbf{\thecontentspage}\hspace{8pt}%
               }}%
              }
              {\contentsmargin{0pt}\large}
              {}
              [\addvspace{.5pc}]

\titlecontents{preface}
              [3pt]
              {\addvspace{1pc}}%
              {\contentsmargin{0pt}%
               \bfseries\large%
               \llap{\makebox[1.5in]{%
                   {\color{gray}Preface}\hfill\textbf{\thecontentspage}\hspace{8pt}%
               }}%
              }
              {\contentsmargin{0pt}\large}
              {}
              [\addvspace{.5pc}]

\titlecontents{section}
              [3pt]
              {\addvspace{1pc}}%
              {\contentsmargin{0pt}%
               \large
               \llap{\makebox[1.5in]{%
                   \hfill\textbf{\thecontentspage}\hspace{8pt}%
               }}%
              }
              {\contentsmargin{0pt}\large}
              {}
              [\addvspace{.5pc}]

%%%% end TOC formatting %%%%


\begin{document}

\frontmatter

\tableofcontents

\mainmatter

\preface{Will This Work?}
%\chapter{Perhaps This Will Work}

\Blindtext[2]

\section{Under The Preface}

\Blindtext[3]

\subsection{To The Subway}

\Blindtext[2]

\subsection{Then To A Submarine}

\Blindtext[2]

\chapter{My First Chapter}

\blindtext[1]

\section{First Section Under A Chapter}

\Blindtext[2]

\section{Secondi}

\Blindtext[2]

\section{Triad}

\Blindtext[3]

\chapter{How About Another Chapter}

\section{Woot}

\Blindtext[2]

\end{document}
splicer
  • 265

1 Answers1

5

I've always found \titleclass very mysterious.

You can solve this in a different way, by setting the preface in the frontmatter (but it's just for logical placement) as \chapter*, adding manually the needed bits.

\documentclass[oneside]{book}

\usepackage{blindtext}
\usepackage{xcolor}
\usepackage{titlesec}
\usepackage{titletoc}
\usepackage{hyperref}

% use arabic numbering throughout
\makeatletter
\renewcommand{\frontmatter}{\cleardoublepage \@mainmatterfalse}
\renewcommand{\mainmatter}{\cleardoublepage \@mainmattertrue}
\makeatother

%%%% begin preface defs %%%%
\makeatletter
\newcommand{\preface}[1]{%
  \chapter*{#1}%
  \addcontentsline{toc}{chapter}{\protect\numberline{\protect\preface}#1}%
  \chaptermark{#1}%
%  \vspace*{-6ex}% add if really wanted
}
\makeatother
\newcommand{\chapterorpreface}{%
  \expandafter\ifx\expandafter\preface\thecontentslabel
    Preface
  \else
    Chapter~\thecontentslabel
  \fi
}
%%%% end preface defs %%%%

\setcounter{tocdepth}{1} % only go down to the section level in TOC

%%%% begin TOC formatting %%%%

\titlecontents{chapter}
  [3pt]
  {\addvspace{1pc}}%
  {\contentsmargin{0pt}%
   \bfseries\large
   \makebox[0pt][r]{\makebox[1.5in]{%
     \textcolor{gray}{\chapterorpreface}\hfill\textbf{\thecontentspage}\hspace{8pt}%
   }}%
  }
  {\contentsmargin{0pt}\large}
  {}
  [\addvspace{.5pc}]

\titlecontents{section}
  [3pt]
  {\addvspace{1pc}}%
  {\contentsmargin{0pt}%
   \large
   \makebox[0pt][r]{\makebox[1.5in]{%
     \hfill\textbf{\thecontentspage}\hspace{8pt}%
   }}%
  }
  {\contentsmargin{0pt}\large}
  {}
  [\addvspace{.5pc}]

%%%% end TOC formatting %%%%

\begin{document}

\frontmatter

\tableofcontents

\preface{Will This Work?}

\Blindtext[2]

\section{Under The Preface}

\Blindtext[3]

\subsection{To The Subway}

\Blindtext[2]

\subsection{Then To A Submarine}

\Blindtext[2]

\mainmatter

\chapter{My First Chapter}

\blindtext[1]

\section{First Section Under A Chapter}

\Blindtext[2]

\section{Secondi}

\Blindtext[2]

\section{Triad}

\Blindtext[3]

\chapter{How About Another Chapter}

\section{Woot}

\Blindtext[2]

\end{document}

The \chapterorpreface macro examines the current \thecontentslabel and, if it is \preface, it just prints “Preface”.

enter image description here


A solution with \titleclass, where \preface is made the same level as \chapter, namely 0. I added also the support for bookmarks and headers.

\documentclass[oneside]{book}

\usepackage{blindtext}

\usepackage{xcolor}
\usepackage{titlesec}
\usepackage{titletoc}
\usepackage{hyperref}
\usepackage{bookmark} % better using this

% use arabic numbering throughout
\makeatletter
\renewcommand{\frontmatter}{\cleardoublepage \@mainmatterfalse}
\renewcommand{\mainmatter}{\cleardoublepage \@mainmattertrue}
\makeatother

%%%% begin preface defs %%%%

% see http://tex.stackexchange.com/a/17278/2041 and the titlesec manual
\titleclass{\preface}[0]{top}%[\part]

\newcounter{preface} % required when defining a new titleclass; accessible via \thepreface

\titleformat{\preface}{\huge\bfseries}{}{0em}{}

% TODO verify that these dimensions make sense
\titlespacing*{\preface}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}

% see autoref in hyperref manual
\newcommand{\prefaceautorefname}{preface}
\makeatletter
\def\toclevel@preface{0}
\makeatother
\renewcommand{\prefacemark}[1]{%
  \markboth{\MakeUppercase{Preface}}{\MakeUppercase{Preface}}% or whatever
}
%%%% end preface defs %%%%

%\setcounter{secnumdepth}{2} % I'm not sure if this is appropriate

\setcounter{tocdepth}{1} % only go down to the section level in TOC

%%%% begin TOC formatting %%%%

\titlecontents{chapter}
              [3pt]
              {\addvspace{1pc}}%
              {\contentsmargin{0pt}%
               \bfseries\large%
               \llap{\makebox[1.5in]{%
                   {\color{gray}Chapter~\thecontentslabel}\hfill\textbf{\thecontentspage}\hspace{8pt}%
               }}%
              }
              {\contentsmargin{0pt}\large}
              {}
              [\addvspace{.5pc}]

\titlecontents{preface}
              [3pt]
              {\addvspace{1pc}}%
              {\contentsmargin{0pt}%
               \bfseries\large%
               \llap{\makebox[1.5in]{%
                   {\color{gray}Preface}\hfill\textbf{\thecontentspage}\hspace{8pt}%
               }}%
              }
              {\contentsmargin{0pt}\large}
              {}
              [\addvspace{.5pc}]

\titlecontents{section}
              [3pt]
              {\addvspace{1pc}}%
              {\contentsmargin{0pt}%
               \large
               \llap{\makebox[1.5in]{%
                   \hfill\textbf{\thecontentspage}\hspace{8pt}%
               }}%
              }
              {\contentsmargin{0pt}\large}
              {}
              [\addvspace{.5pc}]

%%%% end TOC formatting %%%%


\begin{document}

\frontmatter

\tableofcontents

\mainmatter

\preface{Will This Work?}
%\chapter{Perhaps This Will Work}

\Blindtext[2]

\section{Under The Preface}

\Blindtext[3]

\subsection{To The Subway}

\Blindtext[2]

\subsection{Then To A Submarine}

\Blindtext[2]

\chapter{My First Chapter}

\blindtext[1]

\section{First Section Under A Chapter}

\Blindtext[2]

\section{Secondi}

\Blindtext[2]

\section{Triad}

\Blindtext[3]

\chapter{How About Another Chapter}

\section{Woot}

\Blindtext[2]

\end{document}
egreg
  • 1,121,712
  • Great! Any idea why specifically was going wrong with my code? I'm more interested in learning than achieving the target output. – splicer Sep 06 '16 at 03:42
  • Also, in your second approach, it seems that commenting out the prefaceautorefname, toclevel@preface, and prefacemark stuff still produces the same result. What exactly does it accomplish? – splicer Sep 06 '16 at 04:23
  • 1
    @splicer The definition of \prefaceautorefname doesn't seem to be essential; on the other hand, \toclevel@preface avoids a warning and \prefacemark fixes the headers (try without it and look at the headers). I just changed the \titleclass declaration, but I don't really get what problem there was, as changing tocdepth to 2 also listed subsections in the TOC. – egreg Sep 06 '16 at 08:07
  • @Javier Bezos - Is this a known bug in titlesec/titletoc? – splicer Sep 06 '16 at 22:17
  • In your first solution, why wrap \chapter* and addcontentsline in a group? – splicer Sep 07 '16 at 02:46
  • @splicer The remains of an experiment, I forgot to remove them – egreg Sep 07 '16 at 07:51
  • Ah. No worries :) – splicer Sep 07 '16 at 07:55