
@plutonの質問に基づいて派手なToCを作成していますTikZ を使用して目次をカスタマイズするにはどうすればよいですか?
このコードは正常に動作しますが、付録を使用する場合は誤動作しているようです。エントリを追加する代わりに、Appendix A
を追加しますChapter A
。
Chapter
また、番号なしの章を追加するときは、エントリとして ではなく、色付きのボックスか、何も表示しないことをお勧めします。
私のコードは
\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}
そして私の出力は
これら 2 つの要件を満たす方法はありますか?
答え1
これは、目次の生成方法における見落とし、あるいはバグであると思います。
LaTeX ファイル内では、コマンドがからに\appendix
変更され、これは章や付録の見出しに適切なタイトルを取得するために使用されます。残念ながら、この情報は目次ファイル (toc) に渡されません。現在、たとえば book.cls では、章や付録のエントリは次のように toc ファイルに書き込まれます。\@chapapp
\chaptername
\appendixname
\addcontentsline{toc}{chapter}{\protect\numberline{\thechapter}#1}
これは、tocファイル内の章と付録のエントリが、を実行するとまったく同じに見えるため\tableofcontents
、tocファイルは章と付録を区別できないことを意味します(ハッキングなしで)。
\addcontentsline{toc}{appendix}{\protect\numberline{\thechapter}#1}
行 -- ただし、\@chapapp
の値は\@hapapp
言語に依存するため、これらの行を生成するために を使用することはできません...したがって、コードはそれほどエレガントではありません:(
この問題を回避する 1 つの解決策は、\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
。)
実際、上の画像では、これまで述べたことよりも少しだけ多くのことが必要です。まず、「付録」には「章」よりも 1 文字多いため、間隔を少し調整する必要があります。(章と付録を中央に配置するには、これらの数字をより慎重に調整する必要があります:)
さらに深刻なことに、最後の「付録」に を使用すると、\chapter*
青い「付録」ラベルが表示されますが、これはおそらくあなたが望んでいることではありません。これを適切に修正するのは面倒です(たとえば、ドキュメントに\chapter{..}
と\chapter*{...}
コマンドが交互に存在する場合に対処するため)。OP がこの点以降に空のラベルを望んでいると仮定すると(上の画像のように)、次の行を追加するだけで済みます。
\addtocontents{toc}{\let\string\tocchaptername\string\relax}
をコマンドの前に\chapter*{}
入力します。星印の付いた章や付録と星印の付いていない章や付録を交互に使いたい場合は、次のように入力します。
\addtocontents{toc}{\let\string\tocchaptername\string\chaptertitlename}
toc ファイルに何が起こっているかを知らせます (付録の明らかな付録バリエーションを使用)。
実際に使用したコードは次のとおりです。
\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}
そうすれば、必要なときに入力するだけです。