定制 titlesec-etoc(进一步的问题)

定制 titlesec-etoc(进一步的问题)

参考此讨论:进一步的 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}

使用 etoc 的目录

更新问题的答案(2018 年 10 月)

至于后续问题,解释很简单:目录是通过遵循缩进的段落排版的。除了目录中的第一个章节条目,因为文档使用了选项,indentfirst=false这意味着遵循盎格鲁-撒克逊文档的标准 LaTeX 习惯,即在章节标题后不缩进第一个段落。这里目录的标题“索引”负责这样做。

因此,除第一行之外的所有行都会插入缩进框。

要解决这个问题,有很多方法,例如

\makeatletter
\etocsetstyle{chapter}
    {\@afterindenttrue}
    {..}
    {..}
    {..}
\makeatother

MWE 中用于设置目录中章节标题样式的 etoc 宏可以(应该?)设置到\parindent0pt避免此类问题。也可以使用一些来重新插入宽度\etociffirst{...}{...}的空白。\parindent

相关内容