1

I have a situation where the Introductory Chapter and another chapter of a book are un-numbered. I know how to ensure that the entries for these chapters appear in the Table of Contents, as you'll see from the code posted below.

I have designed the Chapter Heading such that it shows Chapter № and The Name of The Chapter, as below:

Chapter Heading Style

A screenshot of the un-numbered Introductory Chapter is as shown below:

Un-numbered Introductory Chapter

The table of contents is as shown below:

Table of Contents

However, I would like to have a situation where, for the Introductory Chapter, I have "Introduction" in Place of "Chapter №", and "This is where it all Begins" for the "Chapter Name", the way this book was designed, as per screenshots below:

Custom Text for Specific Chapter Number

See the corresponding Table of Contents, with Introduction replacing the Chapter prefix, and Custom Text used for the Chapter Name:

Table of Contents Incorporating the custom chapter text


Here is my Code:

\documentclass[english, 12pt, twoside, a5paper]{scrbook}

\usepackage{scrlayer-scrpage}
\automark[chapter]{chapter}
\setkomafont{pageheadfoot}{\scriptsize}
\pagestyle{scrheadings}
\cehead{\scshape{The Foobar Chronicles}}
\cohead{\leftmark}
\cfoot{\thepage}
\ihead{}
\ohead{}
\ifoot{}
\ofoot{}

\usepackage{blindtext}

% ---- TOC customization to change styling of frontmatter chapter

\makeatletter
\newcommand\matter@switch{}
\addtokomafont{chapterentry}{\matter@switch}
\g@addto@macro\frontmatter{%
  \addtocontents{toc}{%
    \protect\renewcommand\protect\matter@switch{\normalfont\itshape}%
  }%
}
\g@addto@macro\mainmatter{%
  \addtocontents{toc}{%
    \protect\renewcommand\protect\matter@switch{}%
  }%
}


\setkomafont{disposition}{\normalfont}
\addtokomafont{chapter}{\normalcolor\itshape}
\addtokomafont{section}{\normalcolor\scshape}

\begin{document}

\frontmatter

\pagestyle{empty}

% Dedication

\vspace*{10cm}
\begin{center}
{\textit{To all the lovely folks on tex.stackexchange.com}}
\end{center}

\cleardoublepage

\pagenumbering{roman}

\rmfamily
\normalfont

\pagestyle{scrheadings}
\tableofcontents

\addchap{Preface}

\blindtext


\addchap{Acknowledgements}

\blindtext

\mainmatter

% Chapter Style

\KOMAoption{chapterprefix}{true}
\renewcommand\raggedchapter{\raggedleft}

\setkomafont{chapterprefix}{\normalfont\normalsize\scshape}

\renewcommand*{\chapterformat}{%
  \enskip \chapappifchapterprefix{\nobreakspace}\thechapter\autodot%
}

\renewcommand\chapterlineswithprefixformat[3]{%
  \MakeUppercase{#2}\par%
  #3\par\bigskip
  \rule{\textwidth}{.5pt}}

\RedeclareSectionCommand[beforeskip=30pt, afterskip=1.5\baselineskip]{chapter}

\addchap[Introduction]{This is where it all Begins}
\label{sec:chapter0}

\Blindtext

\chapter{What Is the Whole Point?}
\label{sec:chapter1}

\Blindtext

\chapter{The Point of It All}
\label{sec:chapter2}

\Blindtext

\addchap{Outro}
\label{sec:chapter3}

\blindenumerate
\blinditemize
\blinddescription

\clearpage

\backmatter


\end{document}

I have checked the KOMA-Script docs and searched the web for solutions to this but I could't find anything. The closest I've found to arriving at the solution has been to do the following (line 90 in the code above), as documented in the KOMA-Script docs.

\addchap[Introduction]{This is where it all Begins}

Thank You.

1 Answer 1

2

You could define a new key for the optional argument of the sectioning commands and redefine \chapterlineswithprefixformat and patch \addchaptertocentry:

\documentclass[english, 12pt, twoside, a5paper,
  headings=optiontoheadandtoc,% <- added (activates advanced functionality of the optional argument of the sectioning commands)
]{scrbook}
\usepackage{babel}
\usepackage{blindtext}

\usepackage{scrlayer-scrpage}
\automark[chapter]{chapter}
\setkomafont{pageheadfoot}{\scriptsize}
\clearpairofpagestyles
\cehead{\scshape{The Foobar Chronicles}}
\cohead{\leftmark}
\cfoot*{\pagemark}

\setkomafont{disposition}{\normalfont}
\addtokomafont{chapter}{\normalcolor\itshape}
\addtokomafont{section}{\normalcolor\scshape}
\setkomafont{chapterprefix}{\normalfont\normalsize\scshape}

\DeclareTOCStyleEntry[
  dynnumwidth,
  numsep=1.5em,
  beforeskip=.5ex plus .5pt
]{chapter}{chapter}

\usepackage{xpatch}
\makeatletter
\newcommand\matter@switch{}
\addtokomafont{chapterentry}{\matter@switch}
\xapptocmd\frontmatter
  {%
    \addtocontents{toc}{\protect\renewcommand\protect\matter@switch{\normalfont\itshape}}%
  }
  {}{\frontmatterPatchFailed}
\xapptocmd\mainmatter
  {%
    \addtocontents{toc}{\protect\renewcommand\protect\matter@switch{}}%
    \KOMAoption{chapterprefix}{true}%
    \renewcommand\raggedchapter{\raggedleft}%
    \RedeclareSectionCommand[beforeskip=30pt, afterskip=1.5\baselineskip]{chapter}%
  }
  {}{\mainmatterPatchFailed}

% define a new key for the optional argument of the sectioning commands
\providecommand*\@specialprefix{}
\FamilyStringKey[.section]{KOMAarg}{specialprefix}{\@specialprefix}

\renewcommand\chapterlineswithprefixformat[3]{%
  \ifstr{#1}{chapter}
    {%
      \ifstr{\@specialprefix}{}
        {\MakeUppercase{#2}}
        {\MakeUppercase{%
          {\usekomafont{chapterprefix}{%
            \@specialprefix
            \setlength{\@tempskipa}{\scr@chapter@innerskip}%
            \chapterheadmidvskip%
          }}%
      }}%
      #3\par\bigskip
      \rule{\textwidth}{.5pt}%
    }{%
      #2#3% original defininition for other levels with style=chapter
  }%
  \gdef\@specialprefix{}% reset the key
}
\xpatchcmd\addchaptertocentry
  {\addtocentrydefault{chapter}{#1}{#2}}
  {%
    \ifstr{\@specialprefix}{}
      {%
        \ifstr{#1}{}
          {\addtocentrydefault{chapter}{#1}{#2}}
          {\addtocentrydefault{chapter}
            {\chaptername
              \texorpdfstring
                {\protect\quad\protect\makebox[1ex][r]{#1}\protect\hfill}
                {~#1}%
            }
            {#2}%
          }%
      }
      {\addtocentrydefault{chapter}{\@specialprefix}{#2}}%
  }
  {}{\addchaptertocentryPatchFailed}
\makeatother

\usepackage[numbered]{bookmark}
\hypersetup{hypertexnames=false}

\begin{document}

\thispagestyle{empty}
\vspace*{10cm}
\begin{center}
\textit{To all the lovely folks on tex.stackexchange.com}
\end{center}

\frontmatter
\tableofcontents
\addchap{Preface}
\blindtext
\addchap{Acknowledgements}
\blindtext

\mainmatter
\addchap[specialprefix=Introduction]{This is where it all Begins}
\label{sec:chapter0}
\Blindtext
\chapter{What Is the Whole Point?}
\label{sec:chapter1}
\Blindtext
\chapter{The Point of It All}
\label{sec:chapter2}
\Blindtext
\addchap{Outro}
\label{sec:chapter3}
\blindenumerate
\blinditemize
\blinddescription
\end{document}

enter image description here

enter image description here

enter image description here

4
  • Thanks @esdd for the awesome solution! I noticed that the pdf bookmarks generated by the hyperref package are in the form Chapter [1ex][r] № ..., which is rather awkward. This is caused by the code {\chaptername\protect\quad\protect\makebox[1ex][r]{#1}\protect\hfill}. Removing the \makebox[1ex][r] solved the problem! Apr 16, 2018 at 2:39
  • You are using package bookmark with option numbered? See my updated answer.
    – esdd
    Apr 16, 2018 at 7:12
  • Thanks once again @esdd, I was using the hyperref package, and this code snippet illustrates how I used it. Even after replacing it with \usepackage[numbered]{bookmark} and \hypersetup{hypertexnames=false}, the resulting bookmarks are still the same! Apr 16, 2018 at 10:06
  • 1
    I have changed the patch of \addchaptertocentry too. Note the usage of\texorpdfstring ;-)
    – esdd
    Apr 16, 2018 at 11:53

You must log in to answer this question.

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