如何在圖書類別的目錄(章節)中加入標題行文字的前綴和換行?

如何在圖書類別的目錄(章節)中加入標題行文字的前綴和換行?

我正在尋找一種方法,將文本「章節」作為圖書類目錄中章節編號的前綴,並將章節標題換行到目錄列表的最左邊。我正在將其構建為一個公共資源,目前分為兩部分,一個作為僅包含包的類文件,另一個作為設置 .tex 文件,即序言中的 \input 。我在設定檔中嘗試了兩種不同的方法。下面的第一個沒有正確捕獲所需的補丁。

\documentclass{book}

\usepackage{xpatch}
\usepackage{hyperref}

\makeatletter
\xpatchcmd{\@chapter}
    {\addcontentsline{toc}{chapter}{\numberline {#1}}}
    {\addcontentsline{toc}{chapter}{\numberline {Chapter~\thechapter{}.~#1}}}
    {\typeout{Success}}
    {\typeout{Failed!}}
\makeatother

\begin{document}

\tableofcontents

\chapter{This is First}

Some day this will work.

\chapter{This is Second}

Perhaps soon!

\end{document}

第二種方法將章節單字放在目錄中,但較長的第 2 章標題中的文字未依目錄行中的要求換行。

\documentclass{book}

\usepackage{tocloft}

\begin{document}

\newlength{\tocchapwidth}
\settowidth{\tocchapwidth}{\chaptername\ }
\addtolength{\cftchapnumwidth}{\tocchapwidth}
\renewcommand{\cftchappresnum}{\chaptername\ }
\renewcommand{\cftchapaftersnum}{.}
\renewcommand{\cftchapaftersnumb}{~}
\renewcommand{\cftchapdotsep}{\cftdotsep}

\tableofcontents

\chapter{This is First}

Some day this will work.

\chapter{This is Second, a Rather Long and Windy Road Leading to a Door Going to Nowhere Fast}

Perhaps soon!

\end{document}

這是為了滿足論文/論文文件的格式要求。我們目前有一個粗略的解決方法 - 我們輸入包含前綴的章節標題,例如 \chapter{Chapter 2.~This is the Second ...} (並使用 titlesec 包抑制 ToC 中的章節號輸出)。

另外,雖然我沒有將其包含在第二個範例中,但該文件確實需要 hyperref 套件。我從另一篇文章中推斷,我可能需要修補其 Hy@... 章節命令,但即使我嘗試這樣做也失敗了。

我對 LaTeX3 不太熟悉,但很樂意合併這樣的修改。最後,我希望提供的任何選項都能在 Overleaf 上輕鬆運行,因為大量使用該軟體包的人可能會完成他們的工作。

答案1

您的第一種方法有幾個問題。

首先,連原來的定義都book.cls沒有:

\addcontentsline{toc}{chapter}{\numberline {#1}}

在 的定義中\@chapter,但是:

\addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}

因此,缺少一個\protect正確的參數,\numberline並且第一個參數\@chapter使用時不帶大括號。

第二個問題是載入後hyperref定義的\@chapter變化。所以補丁必須是載入中hyperref.

順便說一句:要查看原始定義,只需\show\@chapter在 之前添加即可\xpatchcmd。這將\@chapter在修補文件之前輸出當前定義log

第三個問題是,更換:

\addcontentsline{toc}{chapter}{\protect\numberline {Chapter~\thechapter{}.~#1}}

不僅會Chapter~<number>.放入數字參數,還會放入標題。所以它至少應該是:

\addcontentsline{toc}{chapter}{\numberline {Chapter~\thechapter{}.}#1}

但對於此定義,您還需要進行變更\l@chapter以增加為該號碼保留的空間。也許您不想保留數位空間,而是喜歡使用:

\addcontentsline{toc}{chapter}{Chapter~\thechapter{}.~#1}

所以:

\documentclass{book}

\usepackage{xpatch}

\makeatletter
\xpatchcmd{\@chapter}
  {\addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}}
  {\addcontentsline{toc}{chapter}{Chapter~\thechapter{}.~#1}}
  {\typeout{Success}}
  {\typeout{Failed!}}
\makeatother

\usepackage{hyperref}

\begin{document}

\tableofcontents

\chapter{This is First}

Some day this will work.

\chapter{This is Second}

Perhaps soon!

\end{document}

你得到:

第 1 章 這是…

使用Chapter <number>.作為參數的替代方法\numberline是,例如:

\documentclass{book}

\usepackage{xpatch}

\makeatletter
\xpatchcmd{\@chapter}
  {\addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}}
  {\addcontentsline{toc}{chapter}{\protect\numberline{Chapter~\thechapter{}.}#1}}
  {\typeout{\string\@chapter\space patch success}}
  {\typeout{\string\@chapter\space patch failed!}}
\xpatchcmd{\l@chapter}
  {\setlength\@tempdima{1.5em}}
  {\setlength\@tempdima{6em}}% increase reserved number width
  {\typeout{\string\l@chapter\space patch success}}
  {\typeout{\string\l@chapter\space patch failed!}}
\makeatother

\usepackage{hyperref}

\begin{document}

\tableofcontents

\chapter{This is First}

Some day this will work.

\chapter{This is Second}

Perhaps soon!

\end{document}

單行條目的結果與第一個建議類似。若要查看差異,請嘗試包含多行的條目,例如

\chapter{This is Second Chapter with very long Titel Needing more than one
  Line in the Table of Contents}

所以恕我直言,我的第二個方法就是你想要的。


第二種方法的問題是,在增加 時,您沒有考慮數字後面的點.和空格。這是您的設定的一種可能的更正:~\cftchapnumwidth

\documentclass{book}

\usepackage{tocloft}

\begin{document}

\newlength{\tocchapwidth}
\renewcommand{\cftchappresnum}{\chaptername\ }
\settowidth{\tocchapwidth}{\cftchappresnum}
\renewcommand{\cftchapaftersnum}{.~}
\newlength{\tocchapafternumwidth}
\settowidth{\tocchapafternumwidth}{\cftchapaftersnum}
\addtolength{\cftchapnumwidth}{\tocchapwidth}
\addtolength{\cftchapnumwidth}{\tocchapafternumwidth}
\renewcommand{\cftchapdotsep}{\cftdotsep}

\tableofcontents

\chapter{This is First}

Some day this will work.

\chapter{This is Second, a Rather Long and Windy Road Leading to a Door Going to Nowhere Fast}

Perhaps soon!

\end{document}

使用托克洛夫特

但是,如果您仔細研究此建議,它仍然不正確,因為您正在測量當前文檔字體中的文本,而不是章節條目所使用的字體。所以前面的程式碼只是看起來是正確的,但事實並非如此。為了獲得正確的程式碼\cftchapfont,在測量文字寬度時必須考慮到這一點。 (現在,如果沒有前綴Chapter和後綴的數字寬度.已經足夠了,那麼您不需要也增加空格的寬度)。所以更好的程式碼應該是這樣的:

\documentclass{book}

\usepackage{tocloft}

\newlength{\tocchapwidth}
\newlength{\tocchapafternumwidth}
\renewcommand{\cftchappresnum}{\chaptername\ }
\renewcommand{\cftchapaftersnum}{.}
\renewcommand{\cftchapdotsep}{\cftdotsep}
\AtBeginDocument{% final font setup is done in \begin{document} so we delay all text measuring
  \settowidth{\tocchapwidth}{\cftchapfont\cftchappresnum}%
  \settowidth{\tocchapafternumwidth}{\cftchapfont\cftchapaftersnum}%
  \addtolength{\cftchapnumwidth}{\tocchapwidth}%
  \addtolength{\cftchapnumwidth}{\tocchapafternumwidth}%
}

\begin{document}
\tableofcontents

\chapter{This is First}

Some day this will work.

\chapter{This is Second, a Rather Long and Windy Road Leading to a Door Going to Nowhere Fast}

Perhaps soon!

\end{document}

附錄問題

注意:這兩個範例都不適合附錄的文檔,因為在這種情況下,Chapter附錄中的術語不是正確的。

在不適合附錄的文件的情況下提出推薦建議tocloft會很容易。您只需替換硬編碼Chapter\@chapapp增加寬度:

\documentclass{book}

\usepackage{xpatch}

\makeatletter
\xpatchcmd{\@chapter}
  {\addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}}
  {\addcontentsline{toc}{chapter}{\protect\numberline{\@chapapp~\thechapter{}.}#1}}
  {\typeout{\string\@chapter\space patch success}}
  {\typeout{\string\@chapter\space patch failed!}}
\xpatchcmd{\l@chapter}
  {\setlength\@tempdima{1.5em}}
  {\setlength\@tempdima{7em}}% increase reserved number width
  {\typeout{\string\l@chapter\space patch success}}
  {\typeout{\string\l@chapter\space patch failed!}}
\makeatother

\usepackage{hyperref}

\begin{document}

\tableofcontents

\chapter{This is First}

Some day this will work.

\chapter{This is Second}

Perhaps soon!

\appendix
\chapter{This is a Rather Long and Windy Road Leading to a Door Going
  to Nowhere Fast}

\end{document}

對於tocloft解決方案,您可以使用類似以下內容:

\documentclass{book}

\usepackage{tocloft}

\newlength{\tocchapwidth}
\newlength{\tocappwidth}
\newlength{\tocchapafternumwidth}
\renewcommand{\cftchappresnum}{\chaptername\ }
\renewcommand{\cftchapaftersnum}{.}
\renewcommand{\cftchapdotsep}{\cftdotsep}
\AtBeginDocument{%
  \settowidth{\tocchapwidth}{\cftchapfont\chaptername}%
  \settowidth{\tocappwidth}{\cftchapfont\appendixname}%
  \ifdim\tocappwidth>\tocchapwidth
    \addtolength{\cftchapnumwidth}{\tocappwidth}%
  \else
    \addtolength{\cftchapnumwidth}{\tocchapwidth}%
  \fi
  \settowidth{\tocchapafternumwidth}{\cftchapfont\cftchapaftersnum}%
  \addtolength{\cftchapnumwidth}{\tocchapafternumwidth}%
}
\AddToHook{cmd/appendix/after}{%
  \addtocontents{toc}{%
    \protect\renewcommand{\protect\cftchappresnum}{\protect\appendixname{} }%
  }%
}
\begin{document}
\tableofcontents

\chapter{This is First}

Some day this will work.

\chapter{This is Second, a Rather Long and Windy Road Leading to a Door Going to Nowhere Fast}

Perhaps soon!

\appendix
\chapter{A Rather Long Appendix Heading and Windy Road Leading to a Door Going to Nowhere Fast}


\end{document}

相關內容