修改精美目錄

修改精美目錄

我正在根據 @pluton 的問題創建一個精美的目錄如何使用 TikZ 自訂目錄?

儘管這段程式碼運作正常,但在使用附錄時似乎表現不佳。Appendix A它沒有添加條目,而是添加了Chapter A.

另外,當添加未編號的章節時,我寧願不使用 aChapter作為條目,而是使用彩色框或根本不使用任何內容。

我的程式碼是

\documentclass[11pt,a4paper]{book}

\usepackage{tikz,pgf}
\usepackage{tocloft,titletoc,titlesec}
\definecolor{doc}{RGB}{0,60,110}
\definecolor{myblueii}{RGB}{63,200,244}
\usepackage{lipsum}

\contentsmargin{0cm}
\titlecontents{chapter}[0pc]
{\addvspace{30pt}%
\begin{tikzpicture}[remember picture, overlay]%
\draw[fill=myblueii,draw=myblueii, rounded corners] (-4,-.1) rectangle (-0.15,.5);%
\pgftext[left,x=-2.7cm,y=0.2cm]{\color{white}\Large \chaptertitlename\ \thecontentslabel};%
\end{tikzpicture}\color{myblueii}\large\bfseries}%
{}
{}
{\hspace*{6pt}\titlerule\hspace*{6pt}\large\bfseries \thecontentspage
\begin{tikzpicture}[remember picture, overlay]
\draw[fill=doc!25,draw=myblueii, rounded corners=0pt] (2pt,0) rectangle (6,0.1pt);
\end{tikzpicture}}%
\titlecontents{section}[2.4pc]
{\addvspace{1pt}}
{\contentslabel[\thecontentslabel]{2.4pc}}
{}
{\hfill\small \thecontentspage}
[]
\titlecontents{subsection}[4.8pc]
{\addvspace{1.0pt}}
{\contentslabel[\thecontentslabel]{2.4pc}}
{}
{\hfill\small\thecontentspage}
[]

\makeatletter
\renewcommand{\tableofcontents}{%
\chapter*{%
\vspace*{-20\p@}%
\begin{tikzpicture}[remember picture, overlay]%
\pgftext[right,x=15cm,y=0.2cm]{\color{myblueii}\Huge \contentsname};%
\draw[fill=myblueii,draw=myblueii, rounded corners=15pt] (13,-.75) rectangle (20,1);%
\clip (13,-.75) rectangle (20,1);
\pgftext[right,x=15cm,y=0.2cm]{\color{white}\Huge \contentsname};%
\end{tikzpicture}}%
\@starttoc{toc}}
\makeatother

\begin{document}
 \tableofcontents
 \chapter{First Chapter}
 \lipsum[1]
 \chapter{Second Chapter}
 \lipsum[1]
 \chapter{Third Chapter}
 \lipsum[1]
 \appendix
 \chapter{First Appendix Chapter}
 \lipsum[1]
 \chapter{Second Appendix Chapter}
 \lipsum[1]
 \chapter*{Chapter}
 \addcontentsline{toc}{chapter}{Chapter}
 \lipsum[1]
\end{document}

我的輸出是

在此輸入影像描述

有沒有辦法實現這兩個要求呢?

答案1

我認為這是目錄生成方式的疏忽,甚至是錯誤。

在乳膠文件中,\appendix命令從 變為\@chapapp\chaptername\appendixname用於在章節和附錄標題中獲得正確的標題。不幸的是,此資訊不會傳遞到目錄檔案 (toc)。目前,例如在 book.cls 中,章節或附錄的條目透過以下方式寫入 toc 檔案:

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

這意味著執行時,toc 文件中的章節和附錄條目看起來完全相同\tableofcontents,因此 toc 文件無法區分章節和附錄(無需進行一些修改)。如果還有的話就更好了

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

行-雖然你不能用來\@chapapp產生這些行,因為 的值\@hapapp是依賴語言的......所以程式碼不會那麼優雅:(

\chaptertitlename為了解決這個問題,一種解決方案是將Pluton 程式碼中的使用替換為,\tocchaptername然後將以下程式碼新增至檔案頂部:

\let\tocchaptername\chaptertitlename
\let\originalAppendix\appendix
\renewcommand\appendix{\originalAppendix%
    \addtocontents{toc}{\let\string\tocchaptername\string\appendixname}%
 }

有了這個,附錄就被標記為OP想要的了。 (使用\addtocontents是一種在不使用 的情況下將一些行寫入 toc 檔案的技巧\makeatletter\@writefile{toc}...\makeatother。)

在此輸入影像描述

實際上,上圖需要的內容比我到目前為止所說的要稍微多一點。首先,由於「附錄」比「章節」多一個字符,因此間距需要進行少量的額外調整。 (為了讓章節和附錄居中,這些數字需要更仔細地調整:)

更嚴重的是,使用\chapter*最後一個“附錄”會導致藍色“附錄”標籤,這可能不是您想要的。正確修復這個問題會很痛苦(例如,處理文件中存在交替\chapter{..}和命令的情況)。\chapter*{...}假設OP從此時開始想要空標籤(如上圖所示),那麼您只需新增以下行

\addtocontents{toc}{\let\string\tocchaptername\string\relax}

之前\chapter*{}。如果有人想交替使用有星號和沒有星號的章節/附錄,那麼他們需要輸入

\addtocontents{toc}{\let\string\toccaptername\string\chaptertitlename}

讓目錄文件知道發生了什麼(使用附錄中明顯的附錄變體)。

這是我使用的實際程式碼:

\documentclass[11pt,a4paper]{book}

\usepackage{tikz,pgf}
\usepackage{tocloft,titletoc,titlesec}
\definecolor{doc}{RGB}{0,60,110}
\definecolor{myblueii}{RGB}{63,200,244}
\usepackage{lipsum}

\let\tocchaptername\chaptertitlename
\let\originalAppendix\appendix
\renewcommand\appendix{\originalAppendix\addtocontents{toc}{\let\string\tocchaptername\string\appendixname}}

\contentsmargin{0cm}
\titlecontents{chapter}[0pc]
{\addvspace{30pt}%
\begin{tikzpicture}[remember picture, overlay]%
\draw[fill=myblueii,draw=myblueii, rounded corners] (-4,-.1) rectangle (0.10,.5);%
\pgftext[left,x=-2.7cm,y=0.2cm]{\color{white}\Large \tocchaptername\ \thecontentslabel};%
\end{tikzpicture}\color{myblueii}\large\bfseries\quad}%
{}
{}
{\hspace*{6pt}\titlerule\hspace*{6pt}\large\bfseries \thecontentspage
\begin{tikzpicture}[remember picture, overlay]
\draw[fill=doc!25,draw=myblueii, rounded corners=0pt] (2pt,0) rectangle (6,0.1pt);
\end{tikzpicture}}%
\titlecontents{section}[2.4pc]
{\addvspace{1pt}}
{\contentslabel[\thecontentslabel]{2.4pc}}
{}
{\hfill\small \thecontentspage}
[]
\titlecontents{subsection}[4.8pc]
{\addvspace{1.0pt}}
{\contentslabel[\thecontentslabel]{2.4pc}}
{}
{\hfill\small\thecontentspage}
[]

\makeatletter
\renewcommand{\tableofcontents}{%
\chapter*{%
\vspace*{-20\p@}%
\begin{tikzpicture}[remember picture, overlay]%
\pgftext[right,x=15cm,y=0.2cm]{\color{myblueii}\Huge \contentsname};%
\draw[fill=myblueii,draw=myblueii, rounded corners=15pt] (13,-.75) rectangle (20,1);%
\clip (13,-.75) rectangle (20,1);
\pgftext[right,x=15cm,y=0.2cm]{\color{white}\Huge \contentsname};%
\end{tikzpicture}}%
\@starttoc{toc}}
\makeatother

\begin{document}
 \tableofcontents
 \chapter{First Chapter}
 \lipsum[1]
 \chapter{Second Chapter}
 \lipsum[1]
 \chapter{Third Chapter}
 \lipsum[1]
 \appendix
 \chapter{First Appendix Chapter}
 \lipsum[1]
 \chapter{Second Appendix Chapter}
 \lipsum[1]
 \addtocontents{toc}{\let\string\tocchaptername\string\relax}
 \chapter*{Chapter}
 \addcontentsline{toc}{chapter}{Chapter}
 \lipsum[1]
\end{document}

編輯 - 未編號的章節

最後,為了回答您在評論中的問題,為了擺脫未編號章節的空藍色標記,請將上面命令tikzpicture中的環境替換\titlecontents{chapter}為以下變體(實際上,我同時改進了代碼) :

\begin{tikzpicture}[remember picture, overlay]%
  \ifx\tocchaptername\relax\relax%
  \else%
    \draw(-0.15,0.15)node[anchor=east,align=center,fill=myblueii,text=white,rounded corners,
              rectangle, minimum width=8.5em]{\Large \tocchaptername\ \thecontentslabel};
  \fi%
\end{tikzpicture}\color{myblueii}\large\bfseries}%

使用它,輸出現在看起來像:

在此輸入影像描述

如果編號的章節/附錄之間有許多未編號的章節/附錄,那麼您可能需要定義以下指令:

\newcommand\unnumberedchapter[1]{%
   \addtocontents{toc}{\let\string\tocchaptername\string\relax}%
   \chapter*{#1}%
   \addtocontents{toc}{\let\string\tocchaptername\string\chaptertitlename}%
}

然後您只需\unnumberedchapter{Chapter title}在需要時鍵入即可。

相關內容