titletoc 和 titlesec 的艰难合作

titletoc 和 titlesec 的艰难合作

感谢不同的线程和 tex.stackexchange 的帮助,我找到了目录和文本标题的令人满意的格式。但我不明白 titlesec 和 titletoc 之间的协同逻辑。我希望有:

1.目录: 在此处输入图片描述

2.用于正文标题 在此处输入图片描述

但是,将这两个代码简单合并会产生不理想的结果。Titlesec 和 titletoc 相互影响。这是全局代码:

\documentclass[a4paper]{report}
\usepackage{fontspec}
\usepackage{polyglossia}
\setmainfont[Ligatures=TeX,Numbers=OldStyle]{EB Garamond}
\setmainlanguage[babelshorthands=true]{italian}
\PolyglossiaSetup{italian}{indentfirst=false}
\frenchspacing

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{titlesec,titletoc,etoc}

%%% TABLE OF CONTENTS

\titlecontents{chapter}
 [3em]
 {\vspace*{\baselineskip}\mdseries}
 {\contentslabel[\hfill\thecontentslabel\phantom{.}]{3em}\enspace}
 {\hspace*{0.5em}}
 {}
 []

\titlecontents{section}
  [3em]
  {\vspace*{\baselineskip}\mdseries}
  {\contentslabel[\hfill\thecontentslabel]{3em}\enspace}
  {}
  {\hfill\llap{\makebox[\dimexpr\linewidth-1.3pc][l]{\thecontentspage}}}
  []

\titlecontents*{subsection}
  [3.525em]
  {\footnotesize}
  {}
  {}
  {,~\thecontentspage}
  [~--~]
  []

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%% BODY OF TEXT TITLES

\titleformat{\chapter}[display]
  {\normalfont\fontsize{11}{12}\selectfont}
  {\scshape\thechapter}
  {0pt}
  {}
\titleformat{\section}
  {\normalfont\fontsize{11}{12}\selectfont}
  {\thesection}
  {1em}
  {}
\titleformat{\subsection}
  {\filleft\normalfont\fontsize{11}{12}\selectfont\itshape}
  {}
  {1em}
  {}

\titlespacing{\chapter}
  {0pt}{0pt}{3cm}

\makeatletter
\etocsetstyle{chapter}
  {\@afterindenttrue}
  {\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
  }
  {}
\makeatother

\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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\renewcommand{\thechapter}{\scshape\roman{chapter}}
\renewcommand{\thesection}{\arabic{section}.}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\begin{document}

\tableofcontents

\chapter{A Very Short History of Rhetoric}

\section{The Greek World}
\subsection{Aristoteles}
\subsection{The hellenistic Age}
\subsection{The Latin World}
\subsection{Aristoteles}
\subsection{The hellenistic Age}
\subsection{The Latin World}
\subsection{Aristoteles}
\subsection{The hellenistic Age}
\subsection{The Latin World}
\subsection{Aristoteles}
\subsection{The hellenistic Age}
\subsection{The Latin World}

\end{document}

这两个部分在不同的文件中运行良好,但放在一起就很糟糕了。显然我无法让它们共存……

升级

按照你的建议,我尝试了一个没有 的新代码etoc。它似乎运行良好,除了一个奇怪的小细节:

\documentclass[a4paper]{report}
\usepackage{fontspec}
\usepackage{polyglossia}
\setmainfont[Ligatures=TeX,Numbers=OldStyle]{EB Garamond}
\setmainlanguage[babelshorthands=true]{italian}
\PolyglossiaSetup{italian}{indentfirst=false}
\frenchspacing
\usepackage{lipsum}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{titletoc}

\titlecontents{chapter}
[3em]
{\vspace*{\baselineskip}\mdseries}
{\contentslabel[\hfill\thecontentslabel\phantom{.}]{3em}\enspace}
{\hspace*{0.5em}}
{}
[]

\titlecontents{section}
  [3em]
  {\vspace*{\baselineskip}\mdseries}
  {\contentslabel[\hfill\thecontentslabel\phantom{.}]{3em}\enspace}
  {}
{\hfill\llap{\makebox[\dimexpr\linewidth-1.3pc][l]{\thecontentspage}}}
  []

\titlecontents*{subsection}
  [3.525em]
  {\footnotesize}
  {}
  {}
  {,~\thecontentspage}
  [~--~]
  []

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{titlesec}

\titleformat{\chapter}[display]
  {\normalfont\fontsize{11pt}{12pt}\selectfont}{\thechapter}{0pt}{}
\renewcommand\thesection{\arabic{section}}
\titleformat{\section}
  {\normalfont\fontsize{11pt}{12pt}\selectfont}{\thesection}{1em}{}
\titleformat{\subsection}
  {\normalfont\fontsize{11pt}{12pt}\selectfont\itshape}{\phantom{\thesection}}{1em}{}
\titlespacing{\chapter}{0pt}{0pt}{3cm}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\renewcommand{\thechapter}{\scshape\roman{chapter}}
\renewcommand{\thesection}{\arabic{chapter}.\arabic{section}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\tableofcontents

\chapter{A Very Short History of Rhetoric}
\section{The Greek World}
\subsection{Aristoteles}
\subsection{Aristoteles}
\subsection{Aristoteles}
\subsection{Aristoteles}
\subsection{Aristoteles}
\subsection{Aristoteles}
\subsection{Aristoteles}
\subsection{Aristoteles}
\subsection{Aristoteles}
\subsection{Aristoteles}
\lipsum[1]

\section{The Greek World}
\subsection{Aristoteles}
\lipsum[1]

\chapter{A Very Short History of Rhetoric}
\section{The Greek World}
\subsection{Aristoteles}
\lipsum[1]

\section{The Greek World}
\subsection{Aristoteles}
\lipsum[1]

\chapter{A Very Short History of Rhetoric}
\section{The Greek World}
\subsection{Aristoteles}
\lipsum[1]

\section{The Greek World}
\subsection{Aristoteles}
\lipsum[1]
\subsection{The hellenistic Age}
\lipsum[1]

\subsection{The Latin World}
\lipsum[1]

\end{document}

标题完全对齐,但章节和节数却不对齐。如何才能将它们全部右对齐?

相关内容