
參考這個討論:進一步的 titlesec-etoc 定制,我還有一個問題。考慮一下 Gonzalo Medina 提供給我的良好工作代碼:
\documentclass[a4paper,11pt]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{titlesec}
\usepackage{etoc}
\renewcommand{\thechapter}{\scshape\roman{chapter}}
\renewcommand\thesection{\arabic{section}}
\titleformat{\chapter}[display]
{\normalfont\fontsize{11}{12}\selectfont}
{\scshape\thechapter}
{0pt}
{}
\titleformat{\section}
{\normalfont\fontsize{11}{12}\selectfont}
{\thesection}
{1em}
{}
\titleformat{\subsection}
{\normalfont\fontsize{11}{12}\selectfont\itshape}
{}
{1em}
{}
\titlespacing{\chapter}
{0pt}{0pt}{3cm}
\etocsetstyle{chapter}
{}
{\addvspace{2ex}\leavevmode\leftskip 0cm\relax}
{\llap{\makebox[1.5cm][r]{\mdseries\etocpage}\hspace{.5cm}}%
\normalsize\makebox[1cm][r]{\etocnumber\phantom{.}}\hspace{10pt}%
\parbox[t]{\dimexpr\linewidth-1cm-27pt\relax}{\etocname\par}\hfill\par
}
{}
\etocsetstyle{section}
{}
{\leavevmode\leftskip 0cm\relax}
{\llap{\makebox[1.5cm][r]{\mdseries\etocpage}\hspace{.5cm}}%
\normalsize\makebox[1cm][r]{\etocnumber.}\hspace{10pt}%
\parbox[t]{\dimexpr\linewidth-1cm-27pt\relax}{\etocname\par}\hfill\par
}
{}
\begin{document}
\tableofcontents
\chapter{Chapter One}
\section{Section One}
\section{Section Two}
\section{Section Three}
\section{Section Four}
\chapter{Chapter Two}
\section{Section One}
\section{Section Two}
\section{Section Three}
\section{Section Four}
\section{Section Five}
\setcounter{page}{99}% just for the example
\chapter{Chapter Three}
\section{Section One}
\section{Section Two}
\section{Section Three}
\end{document}
如果我不需要放置章節編號,因為章節均以相應的第 1 節開頭,我可以透過對原始程式碼進行很小的更改來刪除它們,即
\etocsetstyle{chapter}
{}
{\addvspace{2ex}\leavevmode\leftskip 0cm\relax}
{\llap{\makebox[1.5cm][r]{\mdseries\phantom\etocpage}\hspace{.5cm}}%
\normalsize\makebox[1cm][r]{\etocnumber\phantom{.}}\hspace{10pt}%
\parbox[t]{\dimexpr\linewidth-1cm-27pt\relax}{\etocname\par}\hfill\par
}
{}
然而,這樣我就失去了參考書目的頁碼(我認為還有索引,如果有的話),因為它們是帶有星號的章節。那麼,有沒有辦法保留這些頁碼呢? (我在另一種情況下使用 解決了這個問題titletoc/titlesec
,但當前程式碼的情況並非如此etoc
)。謝謝
2018 年 10 月附錄
使用 xelates 編譯的相同程式碼會在第一個標題章節中產生異常突出,我不明白為什麼:
\documentclass[a4paper,twoside,11pt]{report}
\usepackage{fontspec}
\setmainfont{EB Garamond}
\usepackage{polyglossia}
\setmainlanguage[babelshorthands=true]{italian}
\PolyglossiaSetup{italian}{indentfirst=false}
\usepackage{titlesec}
\usepackage{etoc}
\renewcommand{\thechapter}{\scshape\roman{chapter}}
\renewcommand\thesection{\arabic{section}}
\titleformat{\chapter}[display]
{\normalfont\fontsize{11}{12}\selectfont}
{\scshape\thechapter}
{0pt}
{}
\titleformat{\section}
{\normalfont\fontsize{11}{12}\selectfont}
{\thesection}
{1em}
{}
\titleformat{\subsection}
{\normalfont\fontsize{11}{12}\selectfont\itshape}
{}
{1em}
{}
\titlespacing{\chapter}
{0pt}{0pt}{3cm}
\etocsetstyle{chapter}
{}
{\addvspace{2ex}\leavevmode\leftskip 0cm\relax}
{\ifShowPage % conditional deciding the display or not of the page number
\llap{\makebox[1.5cm][r]{\mdseries\etocpage}\hspace{.5cm}}%
\fi
\normalsize\makebox[1cm][r]{\etocnumber\phantom{.}}\hspace{10pt}%
\parbox[t]{\dimexpr\linewidth-1cm-27pt\relax}{\etocname\par}\hfill\par
}
{}
\etocsetstyle{section}
{}
{\leavevmode\leftskip 0cm\relax}
{\llap{\makebox[1.5cm][r]{\mdseries\etocpage}\hspace{.5cm}}%
\normalsize\makebox[1cm][r]{\etocnumber.}\hspace{10pt}%
\parbox[t]{\dimexpr\linewidth-1cm-27pt\relax}{\etocname\par}\hfill\par
}
{}
\newif\ifShowPage
\begin{document}
\tableofcontents
\chapter{Chapter One}
\section{Section One}
\section{Section Two}
\section{Section Three}
\section{Section Four}
\chapter{Chapter Two}
\section{Section One}
\section{Section Two}
\section{Section Three}
\end{document}
答案1
我不確定你的具體情況,所以只能給出一般建議。
您提到\chapter*
因此沒有任何內容寫入.toc
文件。人們通常在命令\addcontentsline{toc}{chapter}{<Chapter title>}
之後使用命令\chapter*
來獲取目錄中的條目。
為了獲得所需的效果,一種方法是使用布林值\ifShowPage
(我在這裡使用標準 TeX 的布林值,您可能更喜歡由諸如電子工具箱.) 然後指示chapter
樣式檢查此佈林值的當前狀態並採取適當的行動
因此,在\addcontentsline{toc}{chapter}{<bibliography name>}
你也做之前
\addtocontents{toc}{\protect\ShowPagetrue}
並etoc
會採取適當的行動。
範例程式碼(更新使用 \bibname
並避免添加額外的章節):
\documentclass[a4paper,11pt]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{titlesec}
\usepackage{etoc}
\renewcommand{\thechapter}{\scshape\roman{chapter}}
\renewcommand\thesection{\arabic{section}}
\titleformat{\chapter}[display]
{\normalfont\fontsize{11}{12}\selectfont}
{\scshape\thechapter}
{0pt}
{}
\titleformat{\section}
{\normalfont\fontsize{11}{12}\selectfont}
{\thesection}
{1em}
{}
\titleformat{\subsection}
{\normalfont\fontsize{11}{12}\selectfont\itshape}
{}
{1em}
{}
\titlespacing{\chapter}
{0pt}{0pt}{3cm}
\etocsetstyle{chapter}
{}
{\addvspace{2ex}\leavevmode\leftskip 0cm\relax}
{\ifShowPage % conditional deciding the display or not of the page number
\llap{\makebox[1.5cm][r]{\mdseries\etocpage}\hspace{.5cm}}%
\fi
\normalsize\makebox[1cm][r]{\etocnumber\phantom{.}}\hspace{10pt}%
\parbox[t]{\dimexpr\linewidth-1cm-27pt\relax}{\etocname\par}\hfill\par
}
{}
\etocsetstyle{section}
{}
{\leavevmode\leftskip 0cm\relax}
{\llap{\makebox[1.5cm][r]{\mdseries\etocpage}\hspace{.5cm}}%
\normalsize\makebox[1cm][r]{\etocnumber.}\hspace{10pt}%
\parbox[t]{\dimexpr\linewidth-1cm-27pt\relax}{\etocname\par}\hfill\par
}
{}
\newif\ifShowPage
\begin{document}
\tableofcontents
\chapter{Chapter One}
\section{Section One}
\section{Section Two}
\section{Section Three}
\section{Section Four}
\chapter{Chapter Two}
\section{Section One}
\section{Section Two}
\section{Section Three}
\section{Section Four}
\section{Section Five}
\setcounter{page}{99}% just for the example
\chapter{Chapter Three}
\section{Section One}
\section{Section Two}
\section{Section Three}
\cleardoublepage
\setcounter{page}{139}% just for the example
\begin{thebibliography}{99}
\addtocontents{toc}{\protect\ShowPagetrue}
\addcontentsline{toc}{chapter}{\bibname}
\bibitem{me2015} user41603, \emph{The Key to the Universe}, Cosmos Press, to appear.
\end{thebibliography}
\end{document}
更新問題的答案(2018 年 10 月)
至於後續問題,解釋很簡單:目錄是透過遵循縮排的段落來排版的。除了 TOC 中的第一章條目之外,因為該文件使用的indentfirst=false
選項意味著遵守盎格魯-撒克遜文檔的標準 LaTeX 習慣,即在章節標題後不縮進第一段。這裡 TOC 的標題「索引」負責扮演這樣的角色。
因此,縮排框被插入到除第一行之外的所有行上。
要解決這個問題有很多方法,例如
\makeatletter
\etocsetstyle{chapter}
{\@afterindenttrue}
{..}
{..}
{..}
\makeatother
用於設定 MWE 中 TOC 中章節標題樣式的 etoc 巨集可以(應該?)規定設定\parindent
to0pt
以避免此類問題。人們也可以使用一些\etociffirst{...}{...}
來重新插入寬度的空白\parindent
。