使用 titletoc/xelatex 处理部分(书籍类)

使用 titletoc/xelatex 处理部分(书籍类)

参考这个帖子:处理部分(书类)titletoc ,我有一个新文件,这次是用 XeLaTeX 编译的(不再用 pdfLaTeX)。

如果我使用相同的代码titletoc,章节标题将不再正确对齐,我的意思是居中。这在第一部分的标题中非常明显,如下图所示:

在此处输入图片描述

代码:

\documentclass[a4paper,11pt]{book}
\usepackage{fontspec}
\usepackage{polyglossia}
\usepackage{titlesec,titletoc}
\usepackage{etoolbox}

\setmainfont{EB Garamond}
\setmainlanguage[babelshorthands=true]{italian}
\PolyglossiaSetup{italian}{indentfirst=false}
\frenchspacing

\makeatletter
\patchcmd\@part
  {\thepart}
  {\protect\numberline{\thepart}}
  {}{\FAIL}
\makeatother

\makeatletter
\contentsmargin[-1.55em]{1.55em}

\titlecontents{part}
  [0em]
  {\addvspace{1.5pc}\filcenter}
  {\large\partname~\thecontentslabel\endgraf\bfseries}
  {\large}
  {}
  [\addvspace{.5pc}]

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

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

\titlecontents*{subsection}
  [4.525em]
  {\footnotesize}
  {}
  {}
  {, \thecontentspage}
  [~--~]
  []
\makeatletter\@addtoreset{chapter}{part}\makeatother

\begin{document}

\tableofcontents

\part{Title}

\chapter{First Chapter}

\chapter{Second Chapter}

\part{Long Title of the Second Part}

\chapter{First Chapter}

\chapter{Second Chapter}

\part{Very Long Title of the Third Part}

\chapter{First Chapter}

\chapter{Second Chapter}

\end{document}

问题可能是什么原因造成的?谢谢!

答案1

书籍代码插入了一个 \hspace,您也应该将其删除。请注意,EB Garamond 没有粗体版本,因此 \bfseries 不起作用。

\documentclass[a4paper,11pt]{book}
\usepackage{fontspec}
\usepackage{polyglossia}
\usepackage{titlesec,titletoc}
\usepackage{etoolbox}

\setmainfont{EB Garamond}
\setmainlanguage[babelshorthands=true]{italian}
\PolyglossiaSetup{italian}{indentfirst=false}
\frenchspacing

\makeatletter
\patchcmd\@part
  {\thepart\hspace{1em}} %<----------
  {\protect\numberline{\thepart}}
  {}{\FAIL}
\makeatother

\makeatletter
%\contentsmargin[-1.55em]{1.55em}

\titlecontents{part}
  [0em]
  {\addvspace{1.5pc}\filcenter}
  {\large\partname~\thecontentslabel\endgraf\bfseries}
  {\large}
  {}
  [\addvspace{.5pc}]

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

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

\titlecontents*{subsection}
  [4.525em]
  {\footnotesize}
  {}
  {}
  {, \thecontentspage}
  [~--~]
  []
\makeatletter\@addtoreset{chapter}{part}\makeatother

\begin{document}

\tableofcontents

\part{Title}

\chapter{First Chapter}

\chapter{Second Chapter}

\part{Long Title of the Second Part}

\chapter{First Chapter}

\chapter{Second Chapter}

\part{Very Long Title of the Third Part}

\chapter{First Chapter}

\chapter{Second Chapter}

\end{document}

在此处输入图片描述

答案2

由于标准类中处理非标准方式\part,您必须titlesec使用选项加载并使用 `\titleformat ènewparttoc重新定义布局。所以我建议使用以下代码:\part

\documentclass[a4paper,11pt]{book}
\usepackage[showframe]{geometry}
\usepackage{fontspec}
\usepackage{polyglossia}
\usepackage[newparttoc]{titlesec}
\usepackage{titletoc}
\usepackage{etoolbox}

\setmainfont{EB Garamond}
\setmainlanguage[babelshorthands=true]{italian}
\PolyglossiaSetup{italian}{indentfirst=false}
\frenchspacing

%\makeatletter
%\patchcmd\@part
% {\thepart}
% {\protect\numberline{\thepart}}
% {}{\FAIL}
%\makeatother
%
\titleformat{\part}[display]{\bfseries\filcenter}{\partname~\thepart}{5ex}{\Huge}

\makeatletter
\contentsmargin[-1.55em]{1.55em}
 \titlecontents{part}
 [0em]
 {\addvspace{1.5pc}\filcenter\large}
 {\makebox[\linewidth]{\partname~\thecontentslabel}\endgraf\bfseries\makebox[\linewidth]}
 {\large}
 {}
 [\addvspace{.5pc}]
\titlecontents{chapter}
  [4em]
  {\vspace*{\baselineskip}\mdseries}
  {\contentslabel[\hfill\thecontentslabel]{3em}\enspace}
  {\hspace*{0.5em}}
  {}
  []

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

\titlecontents*{subsection}
  [4.525em]
  {\footnotesize}
  {}
  {}
  {, \thecontentspage}
  [~--~]
  []
\makeatletter\@addtoreset{chapter}{part}\makeatother

\begin{document}

\tableofcontents

\part{Title}

\chapter{First Chapter}

\chapter{Second Chapter}

\part{Long Title of the Second Part}

\chapter{First Chapter}

\chapter{Second Chapter}

\part{Very Long Title of the Third Part}

\chapter{First Chapter}

\chapter{Second Chapter}

\end{document} 

在此处输入图片描述

相关内容