设置自定义文本代替特定章节的章节前缀

设置自定义文本代替特定章节的章节前缀

我遇到的情况是引言而书中的另一章则没有编号。我知道如何确保这些章节的条目出现在目录中,正如您将从下面发布的代码中看到的那样。

我设计了章节标题,以便显示第 章章节名称, 如下:

章节标题样式

未编号的截图引言如下图所示:

未编号的导言章节

其目录如下图所示:

目录

然而,我希望有这样一种情况,引言,我用“简介”代替“章节号”,用“这就是一切的开始”代替“章节名称”,这本书设计如下图所示:

特定章节编号的自定义文本

请参阅相应的目录,其中包含介绍替换章节前缀,以及自定义文本用于章节名称:

目录包含自定义章节文本


这是我的代码:

\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}

我已经检查过KOMA 脚本docs 并在网上搜索了解决方案,但一无所获。我发现最接近解决方案的方法是执行以下操作(上面代码中的第 90 行),如 KOMA-Script 文档中所述。

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

谢谢。

答案1

您可以为分段命令的可选参数定义一个新键,然后重新定义\chapterlineswithprefixformat和修补\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}

在此处输入图片描述

在此处输入图片描述

在此处输入图片描述

相关内容