我需要创建一个新样式的节(例如章节),但在目录中应将其设置为部分。
这是一个最小的非工作示例:
\documentclass[a4paper,9pt]{extbook}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{titlesec}
\usepackage{tocloft}
\usepackage{xcolor}
\usepackage{hyperref}
%%Style of Chapter
\titleformat{\chapter}
{\color{red}\bfseries\Huge}{Chapter \thechapter:\ }{0em}{}
\titlespacing{\chapter}{0pt}{0pt}{6pt}
%%To create the new section
\titleclass{\customchapter}[0]{top}
\newcounter{customchapter}
\renewcommand{\thecustomchapter}{\Alph{customchapter}}
%With the same style of chapter
\titleformat{\customchapter}
{\color{red}\bfseries\Huge}{Custom Chapter \thecustomchapter:\ }{0em}{}
\titlespacing{\customchapter}{0pt}{0pt}{6pt}
%%%%%Style on ToC%%%%%
%%PART%%
\renewcommand{\cftpartfont}{\bfseries\color{green}} %Green style
%%Capitolo%%
\renewcommand{\cftchapfont}{\color{red}} %Style
\begin{document}
\tableofcontents
\part{Part 1}
\chapter{Default Chapter}
\chapter{Another Default Chapter}
\customchapter{Custom Chapter}That should start the counter from 0 (or A)
\end{document}
我曾经想过类似的事情\customchapter*{Custom Chapter}\addcontentsline{toc}{part}{Custom Chapter \Alph{customchapter}: Custom Chapter}
,但是这样一来,所有的柜台都会被破坏。
请告诉我,谢谢!
答案1
我并不特别热衷于使用titlesec
/titletoc
包(实际上,我得说我相当笨),但是,由于已经 7 个小时没有发布答案,所以我会尝试写一个。正如我所说,我将使用titletoc
, 而不是tocloft
。
这里的主要问题是,\part
命令不是向.toc
文件写入 形式的条目\numberline{...}...
,就像\chapter
命令那样;这会弄乱使用命令(重新)定义的 ToC 条目的格式\titlecontents
。我的解决方法是使用粗暴的 hack。
事实上,newparttoc
该titlesec
包的选项正是为解决这个问题而设计的,但它只有在\part
重新定义命令时才有效,而我们不想这样做。
这是我的代码(文本是但丁·阿利吉耶里的引文地狱, 当然):
\documentclass[a4paper]{book} % I'm using 10-point size for this example
\usepackage[T1]{fontenc}
% \usepackage[utf8]{inputenc} % I prefer to stick to 7-bit ASCII
\usepackage[italian]{babel} % I'm Italian too! (;-)
\usepackage{titlesec}
\usepackage{titletoc}
\usepackage{xcolor}
\usepackage{hyperref}
%%Style of Chapter
\titleformat{\chapter}
[display] % shape
{\color{red}\bfseries\Huge} % format
{\chaptertitlename~\thechapter:\ } % label
{0em} % sep
{} % before-code
\titlespacing{\chapter} % unstarred = indentfirst
{0pt}{0pt}{6pt} % left- / before- / after-sep
%%To create the new section
\titleclass{\customchapter}{top}[\part]
\newcounter{customchapter}
\renewcommand*{\thecustomchapter}{\Alph{customchapter}}
\newcommand*{\customchaptername}{Capitolo jolly}
%With the same style of chapter
\titleformat{\customchapter}
[display] % shape
{\color{red}\bfseries\Huge} % format
{\customchaptername~\thecustomchapter:\ } % label
{0em} % sep
{} % before-code
[\markboth{}] % after-code
\titlespacing{\customchapter}
{0pt}{0pt}{6pt} % left- / before- / after-sep
\makeatletter
\@ifpackageloaded{hyperref}{
\def\toclevel@customchapter{0}
}
\makeatother
%%%%%Style on ToC%%%%%
%% Part %%
% \renewcommand{\cftpartfont}{\bfseries\color{green}} %Green style
% The toc line format for part must be tweaked in order to account for the
% special format of the line contained in the ".toc" file:
\titlecontents{part}
[0pt] % left-indent should be zero
{% above-code
\medbreak % for example
\large\bfseries
\color{green}%
}{% numbered-entry-format
% never used, however a fallback definition is provided
\contentslabel{0pc}% should be equal to left-indent, see above
}{%numberless-entry-format
% leave empty
}{% page-format
\hfill\contentspage
}[% below-code
\nobreak\medskip
]
%% Chapter %%
% \renewcommand{\cftchapfont}{\color{red}} %Style
\titlecontents{chapter}
[1.5em] % left-indent; change as you see fit
{% above-code
\color{red}%
}{% numbered-entry-format
\contentslabel{1.5em}% should be equal to left-indent, see above
}{%numberless-entry-format
\hspace*{1.5em}% same remark as preceding argument applies
}{% page-format
\hfill\contentspage
}
%% Custom Chapter %%
\titlecontents{customchapter}
[0pt] % left-indent; change as you see fit
{% above-code
\medbreak % for example
\large\bfseries
\color{green}%
}{% numbered-entry-format
\def\numberline##1{##1\hspace{1em}}% >>> CRUDE HACK <<<
}{%numberless-entry-format
% leave empty
}{% page-format
\hfill\contentspage
}
\begin{document}
\tableofcontents
\part{Inferno}
\chapter{Prologo}
Nel mezzo del cammin di nostra vita mi ritrovai per una selva oscura, ch\'{e}
la diritta via era smarrita.
\chapter{Dubbi di Dante}
Lo giorno se ne andava, e l'aere bruno toglieva gli animai che sono in terra
dalle fatiche loro.
\customchapter{Il primo capitolo jolly}
Dovrebbe cominciare la numerazione da~<<A>>.
\chapter{Ingresso all'Inferno}
Per me si va nella citt\`{a} dolente, per me si va nell'etterno dolore, per me
si va tra la perduta gente.
\section{Ignavi}
Non ragioniam di lor, ma guarda e passa.
\section{Caronte}
Ed ecco verso noi venir per nave un vecchio, bianco per antico pelo, gridando
<<Guai a voi, anime prave!>>
\customchapter{Un altro capitolo jolly}
Deve essere numerato~<<B>>.
\chapter{Limbo}
Ruppemi l'alto sonno nella testa un greve truono, s\'{\i} ch'io mi riscossi,
come persona ch'\`{e} per forza desta.
\end{document}
添加
根据 OP 的要求,修改后的代码:
\documentclass[a4paper]{book} % I'm using 10-point size for this example
\usepackage[T1]{fontenc}
% \usepackage[utf8]{inputenc} % I prefer to stick to 7-bit ASCII
\usepackage[italian]{babel} % I'm Italian too! (;-)
\usepackage{titlesec}
\usepackage{titletoc}
\usepackage{xcolor}
\usepackage{hyperref}
%%Style of Chapter
\titleformat{\chapter}
[display] % shape
{\color{red}\bfseries\Huge} % format
{\chaptertitlename~\thechapter:\ } % label
{0em} % sep
{} % before-code
\titlespacing{\chapter} % unstarred = indentfirst
{0pt}{0pt}{6pt} % left- / before- / after-sep
%%To create the new section
\titleclass{\customchapter}{top}[\part]
\newcounter{customchapter}
\renewcommand*{\thecustomchapter}{\Alph{customchapter}}
\newcommand*{\customchaptername}{Capitolo jolly}
%With the same style of chapter
\titleformat{\customchapter}
[display] % shape
{\color{red}\bfseries\Huge} % format
{\customchaptername~\thecustomchapter:\ } % label
{0em} % sep
{} % before-code
[\markboth{}] % after-code
\titlespacing{\customchapter}
{0pt}{0pt}{6pt} % left- / before- / after-sep
\makeatletter
\@ifpackageloaded{hyperref}{
\def\toclevel@customchapter{0}
}
\makeatother
%%%%%Style on ToC%%%%%
%% Part %%
% \renewcommand{\cftpartfont}{\bfseries\color{green}} %Green style
% The toc line format for part must be tweaked in order to account for the
% special format of the line contained in the ".toc" file:
\titlecontents{part}
[0pt] % left-indent should be zero
{% above-code
\medbreak % for example
\large\bfseries
\color{green}%
}{% numbered-entry-format
% never used, however a fallback definition is provided
\partname~\contentslabel{0pc}% should be equal to left-indent, see above
}{%numberless-entry-format
\FormatPartToCLine % or just "\partname~", sparing one macro!
}{% page-format
\hfill\contentspage
}[% below-code
\nobreak\medskip
]
\newcommand*{\FormatPartToCLine}[1]{\partname~#1}
%% Chapter %%
% \renewcommand{\cftchapfont}{\color{red}} %Style
\titlecontents{chapter}
[1.5em] % left-indent; change as you see fit
{% above-code
\color{red}%
}{% numbered-entry-format
\contentslabel{1.5em}% should be equal to left-indent, see above
}{%numberless-entry-format
\hspace*{1.5em}% same remark as preceding argument applies
}{% page-format
\hfill\contentspage
}
%% Custom Chapter %%
\titlecontents{customchapter}
[0pt] % left-indent; change as you see fit
{% above-code
\medbreak % for example
\large\bfseries
\color{green}%
}{% numbered-entry-format
\def\numberline##1{% >>> CRUDE HACK <<<
\customchaptername~##1:\hspace{1em}%
}%
}{%numberless-entry-format
% leave empty
}{% page-format
\hfill\contentspage
}
\begin{document}
\tableofcontents
\part{Inferno}
\chapter{Prologo}
Nel mezzo del cammin di nostra vita mi ritrovai per una selva oscura, ch\'{e}
la diritta via era smarrita.
\chapter{Dubbi di Dante}
Lo giorno se ne andava, e l'aere bruno toglieva gli animai che sono in terra
dalle fatiche loro.
\customchapter{Il primo capitolo jolly}
Dovrebbe cominciare la numerazione da~<<A>>.
\chapter{Ingresso all'Inferno}
Per me si va nella citt\`{a} dolente, per me si va nell'etterno dolore, per me
si va tra la perduta gente.
\section{Ignavi}
Non ragioniam di lor, ma guarda e passa.
\section{Caronte}
Ed ecco verso noi venir per nave un vecchio, bianco per antico pelo, gridando
<<Guai a voi, anime prave!>>
\customchapter{Un altro capitolo jolly}
Deve essere numerato~<<B>>.
\chapter{Limbo}
Ruppemi l'alto sonno nella testa un greve truono, s\'{\i} ch'io mi riscossi,
come persona ch'\`{e} per forza desta.
\end{document}