Titlesec 和 Titletoc:危險的關係

Titlesec 和 Titletoc:危險的關係

請考慮以下程式碼:

\documentclass[a4paper]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{cfr-lm}

\usepackage{titlesec}
\renewcommand{\thechapter}{\scshape\roman{chapter}}
\titleformat{\chapter}[display]
  {\normalfont\fontsize{11pt}{12pt}\selectfont}{\thechapter}{0pt}{}
%%{\normalfont\fontsize{11pt}{12pt}\selectfont}{\chaptertitlename\space \thechapter}{11pt}{}
\renewcommand\thesection{\arabic{section}}
\titleformat{\section}
  {\normalfont\fontsize{11pt}{12pt}\selectfont}{\thesection}{1em}{}
\titleformat{\subsection}
  {\normalfont\fontsize{11pt}{12pt}\selectfont\itshape}{}{1em}{}
\titlespacing{\chapter}{0pt}{0pt}{3cm}

\usepackage{titletoc}
\titlecontents{chapter}
[1.5em] % ie, 1.5em (chapter) + 2.3em
{}
{\contentslabel{2.3em}}
{\hspace*{-2.3em}}
{}
%{\titlerule*[1pc]{.}\contentspage}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% codice Pignalberi
\contentsmargin{0pt}
\titlecontents{chapter}[4pc]
{\contentsmargin{0pt}\makebox[0pt][r]{\thecontentspage\quad\makebox[30pt][l]%
{\textsc{\romannumeral\thecontentslabel\ quad}}}}
{}
{}
{}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\tableofcontents

\chapter{Chapter 1 title}
Some text some text some text some text some text

\section{Section a title}
Some text some text some text some text some text

\section{Section b title}
Some text some text some text some text some text

\chapter{Chapter 2 title}
Some text some text some text some text some text

\section{Section c title}
Some text some text some text some text some text

\section{Section d title}
Some text some text some text some text some text

\chapter{Chapter 3 title}
Some text some text some text some text some text

\section{Section e title}
Some text some text some text some text some text

\section{Section f title}
Some text some text some text some text some text
\end{document}

我嘗試更改一下,根據我的需要,索引格式是可以的。第二步,我想將頁碼放在左側的目錄中。但我發現的程式碼(在我的 MWE“codice Pignalberi”中)幹擾了前一個程式碼。因為我無法理解,按照 titlesec/titletoc 手冊,在哪裡以及如何協調此程式碼的兩側,我希望得到您的幫助。

==================================================== = =

更新

感謝 Gonzalo 的代碼,現在一切都運作得很好。我改進了兩個方面(在章節標題之前添加更多空間並刪除章節編號,只留下章節的編號:現在看起來對讀者更有用)。程式碼如下:

<pre>\usepackage{titlesec}
\usepackage[rightlabels,dotinlabels]{titletoc}

\renewcommand{\thechapter}{\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}

\titlecontents{chapter}[-3pc]
  {\addvspace{0.8pc}}
  {\makebox[3pc][r]{\hfill}%
    \textsc{\thecontentslabel}\quad%
  }
  {}
  {}
  {}
\titlecontents{section}[-3pc]
  {\makebox[4pc][r]{\thecontentspage\hfill}%
    \textsc{\thecontentslabel}\quad%
  }
  {}
  {}
  {}</pre>

最後,我想右對齊而不是左對齊所有數字(頁面和部分),因為實際上該部分的標題沒有使用比例字體很好地對齊。考慮到這些titletoc選項rightlabels,dotinlabels在我看來不起作用。非常感謝!您可以在這裡看到問題: 對齊不良

==================================================== = ========

更新2

我嘗試使用 etoc 將您的程式碼複製到我的 .tex 檔案中,但結果很奇怪: etoc奇怪的影響

==================================================== = ========

更新3

上述問題是由於bidi包包引起的,因為在該文件中我也使用希伯來語和腓尼基語。在「正常」檔案中etoc程式碼可以正常運作。只有最後一個問題:我必須閱讀非常好的etoc文檔,該文檔相當大。但是如何才能對齊正確的頁碼呢?謝謝!

答案1

這只是該部分的一個輕微變體貢薩洛·梅迪納的回答它使用etoc。我不知道它與 互動時的表現如何bidi

etoc一旦有了足夠的例子,事情就會變得更容易。

在這裡,我添加了一些圖形幫助來理解佈局,特別是垂直對齊。章節頁碼位於左邊距。章節編號右對齊,1em 與實際標題水平分隔。章節名稱1cm從左邊距開始,節名稱從 開始2cm。長名稱在1cm距文字右邊距 處換行。

更新(在末尾添加)是為了回應有關處理未編號章節並顯示某些選定章節的頁碼的評論。 (兩者都在一個未編號的額外章節中進行了說明)。

等TOC

\documentclass[a4paper]{report}
\usepackage[margin=2cm,paperwidth=14cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{cfr-lm}
\usepackage{titlesec}
\usepackage{etoc}
\usepackage{color}

\renewcommand{\thechapter}{\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}
% FOR VISUALIZING LAYOUT:
  % {\fboxsep-\fboxrule\noindent\normalsize
  %        \fbox{\makebox[1cm][r]{\strut
  %                          \smash{\color{blue}\vrule depth12cm}\hspace{1em}%
  %                          \smash{\color{green}\vrule depth12cm}}}%
  %        \fbox{\makebox[1cm][r]{\color{blue}\strut
  %                          \smash{\color{blue}\vrule depth12cm}\hspace{1em}%
  %                          \smash{\color{green}\vrule depth12cm}}}%
  %        \fbox{\makebox[1cm]{\strut}}(debugging layout)\hfill
  %        \fbox{\makebox[1cm][l]{\strut\smash{\color{red}\vrule
  %              depth12cm}}}%
  %   \hrule}  
  {}% <- uncomment the above and suppress this line if debugging layout
  {\pagebreak[3]\vskip25pt\leftskip 1cm\relax
                          \rightskip 1cm\relax
                          \parindent 0pt\relax
   }
  {\Large\leavevmode
   \llap{\makebox[1cm][r]{\normalsize\etocnumber.\hspace{1em}}}%
   \etocname\par\nopagebreak\medskip }
  {\bigskip}


\etocsetstyle{section}
  {}  
  {\leftskip 2cm\relax}
  {\normalsize\leavevmode
   \llap{\makebox[1cm][r]{\mdseries\etocpage}%
                    \hspace{1cm}%
                    \makebox[1cm][r]{\etocnumber.\hspace{1em}}}%
   \etocname\par\pagebreak[1]\smallskip }
  {}

\begin{document}
\tableofcontents

\chapter{Vita e Opere}
\setcounter{page}{98}% just for the example
Some text some text some text some text some text

\section{Infanzia, studi a Lipsia e soggiorno a Parigi}
Some text some text some text some text some text

\section{A Honovver, al servizio del ducchi Johann Friedrich ed Ernst August}
Some text some text some text some text some text

\section{Viaggio in Italia, morte dell'elettore Erns August ed successione del nuovo elettore, Georg Ludwig}
Some text some text some text some text some text

\section{Della \textit{Dissertazione preliminare} al \textit{Dialogo}}
Some text some text some text some text some text

\section{La concezione delle idee}
Some text some text some text some text some text

\chapter{Seggni (``caratteri'') e idee}
Some text some text some text some text some text

\section{Della \textit{Dissertazione preliminare} al \textit{Dialogo}}
Some text some text some text some text some text

\section{La concezione delle idee}
Some text some text some text some text some text

\section{A Honovver, al servizio del ducchi Johann Friedrich ed Ernst August}
Some text some text some text some text some text

\clearpage

\section{Viaggio in Italia, morte dell'elettore Erns August ed successione del nuovo elettore, Georg Ludwig}
Some text some text some text some text some text

\section{A Honovver, al servizio del ducchi Johann Friedrich ed Ernst August}
Some text some text some text some text some text

\section{Viaggio in Italia, morte dell'elettore Erns August ed successione del nuovo elettore, Georg Ludwig}
Some text some text some text some text some text

\end{document}

等TOC 2

\documentclass[a4paper]{report}
\usepackage[margin=2cm,paperwidth=14cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{cfr-lm}
\usepackage{titlesec}
\usepackage{etoc}
\usepackage{color}

\renewcommand{\thechapter}{\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}


%% ETOC SPECIFICATIONS

% conditional to display chapter page number
% a priori set to false
\newif\ifDisplayInTocChapterPageNumber

\etocsetstyle{chapter}
  {}
  {\pagebreak[3]\vskip25pt\leftskip 1cm\relax
                          \rightskip 1cm\relax
                          \parindent 0pt\relax
   }
  {\Large\leavevmode
   \llap{\ifDisplayInTocChapterPageNumber
             \makebox[1cm][r]{\mdseries\normalsize\etocpage}%
         \fi
         \makebox[1cm][r]{\normalsize\etocifnumbered{\etocnumber.}{}%
                          \hspace{1em}}}%
   \etocname\par\nopagebreak\medskip }
  {\bigskip}


\etocsetstyle{section}
  {}  
  {\leftskip 2cm\relax}
  {\normalsize\leavevmode
   \llap{\makebox[1cm][r]{\mdseries\etocpage}%
                    \hspace{1cm}%
                    \makebox[1cm][r]{\etocnumber.\hspace{1em}}}%
   \etocname\par\pagebreak[1]%\smallskip 
  }
  {}

\begin{document}
\tableofcontents

\chapter{Vita e Opere}
\setcounter{page}{98}% just for the example
Some text some text some text some text some text

\section{Infanzia, studi a Lipsia e soggiorno a Parigi}
Some text some text some text some text some text

\section{A Honovver, al servizio del ducchi Johann Friedrich ed Ernst August}
Some text some text some text some text some text

\section{Viaggio in Italia, morte dell'elettore Erns August ed successione del nuovo elettore, Georg Ludwig}
Some text some text some text some text some text

\section{Della \textit{Dissertazione preliminare} al \textit{Dialogo}}
Some text some text some text some text some text

\section{La concezione delle idee}
Some text some text some text some text some text

\chapter{Seggni (``caratteri'') e idee}
Some text some text some text some text some text

\section{Della \textit{Dissertazione preliminare} al \textit{Dialogo}}
Some text some text some text some text some text

\section{La concezione delle idee}
Some text some text some text some text some text

\section{A Honovver, al servizio del ducchi Johann Friedrich ed Ernst August}
Some text some text some text some text some text

\clearpage

\section{Viaggio in Italia, morte dell'elettore Erns August ed successione del nuovo elettore, Georg Ludwig}
Some text some text some text some text some text

\section{A Honovver, al servizio del ducchi Johann Friedrich ed Ernst August}
Some text some text some text some text some text

\section{Viaggio in Italia, morte dell'elettore Erns August ed successione del nuovo elettore, Georg Ludwig}
Some text some text some text some text some text

\addtocontents{toc}{\protect\DisplayInTocChapterPageNumbertrue}

\chapter*{Un capitolo non numerato e col numero di pagina nel margine dell'indice}
\addcontentsline{toc}{chapter}{Un capitolo non numerato e col numero di
  pagina nel margine dell'indice}
\setcounter{section}{0}

\section{Ultimi anni}
Some text some text some text some text some text


\end{document}

答案2

下面我展示了兩個選項;第一個使用etoc,第二個使用titletoc.最終所需的格式對我來說仍然不完全清楚。問題要求將章節的頁碼放在左側,然後在註釋中提供圖像,其中章節條目左側沒有頁碼?無論如何,下面的範例程式碼允許輕鬆自訂以實現所需的任何格式。

一種使用強大的可能性etoc包裹:

\documentclass[a4paper]{report}
\usepackage[margin=2cm,paperwidth=14cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{cfr-lm}
\usepackage{titlesec}
\usepackage{etoc}

\renewcommand{\thechapter}{\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}
  {}  
  {\vskip25pt\leavevmode\leftskip 0cm\relax}
  {\parbox[t]{.5cm}{\etocnumber.\hfill}%
  \parbox[t]{\dimexpr\linewidth-1cm-15pt\relax}{\Large\etocname}\hfill\par\medskip
  }
  {}
\etocsetstyle{section}
  {}  
  {\leavevmode\leftskip 0cm\relax}
  {\llap{\parbox[t]{1.5cm}{\mdseries\hfill\etocpage}\hspace{.5cm}}%
    \normalsize\parbox[t]{1cm}{\quad\etocnumber.\hfill}%
  \parbox[t]{\dimexpr\linewidth-1cm-15pt\relax}{\etocname\par}\hfill\par
  }
  {}

\begin{document}
\tableofcontents

\chapter{Vita e Opere}
\setcounter{page}{98}% just for the example
Some text some text some text some text some text

\section{Infanzia, studi a Lipsia e soggiorno a Parigi}
Some text some text some text some text some text

\section{A Honovver, al servizio del ducchi Johann Friedrich ed Ernst August}
Some text some text some text some text some text

\section{Viaggio in Italia, morte dell'elettore Erns August ed successione del nuovo elettore, Georg Ludwig}
Some text some text some text some text some text

\section{Della \textit{Dissertazione preliminare} al \textit{Dialogo}}
Some text some text some text some text some text

\section{La concezione delle idee}
Some text some text some text some text some text

\chapter{Seggni (``caratteri'') e idee}
Some text some text some text some text some text

\section{Della \textit{Dissertazione preliminare} al \textit{Dialogo}}
Some text some text some text some text some text

\section{La concezione delle idee}
Some text some text some text some text some text

\section{A Honovver, al servizio del ducchi Johann Friedrich ed Ernst August}
Some text some text some text some text some text

\clearpage

\section{Viaggio in Italia, morte dell'elettore Erns August ed successione del nuovo elettore, Georg Ludwig}
Some text some text some text some text some text

\section{A Honovver, al servizio del ducchi Johann Friedrich ed Ernst August}
Some text some text some text some text some text

\section{Viaggio in Italia, morte dell'elettore Erns August ed successione del nuovo elettore, Georg Ludwig}
Some text some text some text some text some text

\end{document}

在此輸入影像描述

這是另一種使用的可能性titletoc

\documentclass[a4paper]{report}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{cfr-lm}
\usepackage{titlesec}
\usepackage{titletoc}

\renewcommand{\thechapter}{\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}

\titlecontents{chapter}[-4pc]
  {\makebox[2pc][l]{\hfill\thecontentspage}\hspace{2pc}%
    \textsc{\thecontentslabel}\quad%
  }
  {}
  {}
  {}
\titlecontents{section}[-4pc]
  {\makebox[2pc][l]{\hfill\thecontentspage}\hspace{3pc}%
    \textsc{\thecontentslabel}\quad%
  }
  {}
  {}
  {}

\begin{document}
\tableofcontents

\chapter{Chapter 1 title}
\setcounter{page}{98}% just for the example
Some text some text some text some text some text

\section{Section a title}
Some text some text some text some text some text

\section{Section b title}
Some text some text some text some text some text

\chapter{Chapter 2 title}
Some text some text some text some text some text

\section{Section c title}
Some text some text some text some text some text

\section{Section d title}
Some text some text some text some text some text

\chapter{Chapter 3 title}
Some text some text some text some text some text

\section{Section e title}
Some text some text some text some text some text

\section{Section f title}
Some text some text some text some text some text

\end{document}

在此輸入影像描述

答案3

如果我正確理解了您的佈局,這是一個基於以下基礎的基本解決方案titletoc

\newcommand\boxedpage{\makebox[3pc][r]{\thecontentspage}}
\newcommand\boxedtocnum{\makebox[3pc][r]{\thecontentslabel.\quad}}

\titlecontents{chapter}[0pc]
  {\addvspace{0.8pc}}
  {\contentslabel[\boxedpage\boxedtocnum]{6pc}}
  {}{}{}
\titlecontents{section}[3pc]
  {}
  {\contentslabel[\boxedpage\hspace*{3pc}\boxedtocnum]{9pc}}
  {}{}{}

只是玩維度。

編輯。這是一個完整的範例titletoc。請記住,您可以使用 控制編號secnumdepth,並使用 控制目錄中包含的條目tocdepth。還要記住,titletoc您可以更改目錄條目的格式中間如果出於某種原因您需要它,請參閱文件(適用於後續部分)。和以前一樣,只玩dimens。

\documentclass{book}

\renewcommand{\thesection}{\arabic{section}}

\usepackage{titletoc}

\newcommand\boxedpage{\makebox[3pc][r]{\thecontentspage}}
\newcommand\boxedtocnum{\makebox[3pc][r]{\thecontentslabel.\quad}}

\titlecontents{chapter}[3pc]
  {\addvspace{0.8pc}}
  {\contentslabel[\boxedpage\boxedtocnum]{6pc}}
  {\contentslabel[\boxedpage\hspace*{3pc}]{6pc}}
  {}{}
\titlecontents{section}[6pc]
  {}
  {\contentslabel[\boxedpage\hspace*{3pc}\boxedtocnum]{9pc}}
  {\contentslabel[\boxedpage\hspace*{6pc}]{9pc}}
  {}{}

\newcommand*\asection{
\section{A Honovver, text text al servizio del ducchi Johann
  Friedrich ed Ernst August.}
Some text some text some text some text some text\par}

\begin{document}
\tableofcontents

\chapter{Vita e Opere}
\setcounter{page}{98}% just for the example

\asection\asection

\chapter{Seggni (``caratteri'') e idee}

\asection\asection\asection\asection\asection
\asection\asection\asection\asection\asection

% From now on, unnumbered headers, but still in the toc
% down to section
\setcounter{secnumdepth}{-1}
\addtocontents{toc}{\protect\setcounter{tocdepth}{1}\ignorespaces}

\chapter{Seggni (``caratteri'') e idee}

\asection\asection

\end{document}

相關內容