我怎样才能制作这样的目录?

我怎样才能制作这样的目录?

我希望制作像这样的目录:

在此处输入图片描述

这是我迄今为止所取得的成就(仍然缺少- I -\newline\fbox{\thechapter}):

在此处输入图片描述

我怎样才能制作这样的目录?

下面是 MWE。

\documentclass{book}

\usepackage{titletoc}

\titlecontents{part}
    [0em]
    {\addvspace{1.5pc}\filcenter}
    {\partname~\thecontentslabel}
    {\uppercase}
    {} % without page number
    [\addvspace{.5pc}]
\titlecontents{chapter}
    [2em] % i.e., 0em (part) + 2em
    {\addvspace{.5pc}}
    {\contentslabel{2em}}
    {\hspace*{-2em}}
    {\hfill\contentspage}
\titlecontents{section}
    [4em] % i.e., 2em (chapter) + 2em
    {}
    {\contentslabel{2em}}
    {\hspace*{-2em}}
    {\titlerule*[1em]{.}\contentspage}

\titlecontents{subsection}
    [7em] % i.e., 4em (section) + 3em
    {}
    {\contentslabel{3em}}
    {\hspace*{-3em}}
    {\titlerule*[1em]{.}\contentspage}

\renewcommand{\thesection}{\arabic{section}}
\renewcommand{\thesubsection}{\arabic{section}.\arabic{subsection}}

\def\quad{\ \ }
\def\qquad{\ \ \ \ }

\usepackage[hidelinks,linktoc=all]{hyperref}

\begin{document}

\ttfamily

%% Hard coded
%%=====================================
% \begin{center}
%     - I - \\FIRST PART
% \end{center}
% \fbox{1}\quad First chapter \hfill 1\\
% \null\qquad 1 \quad First section\ \ . . . . . . . . . . . . . . . . . .\hfill 1\\
% \null\qquad 2 \quad Second section . . . . . . . . . . . . . . . . . .\hfill 3\\[.5pc]
% \fbox{2}\quad Second chapter \hfill 4\\
% \null\qquad 1 \quad First section\ \ . . . . . . . . . . . . . . . . . .\hfill 4\\
% \null\qquad\qquad 1.1 \quad First subsection\ \ \,. . . . . . . . . . . . . .\hfill 4\\
% \null\qquad 2 \quad Second section . . . . . . . . . . . . . . . . . .\hfill 5\\

\tableofcontents

\part{First part}
\chapter{First chapter}
\section{First section}
\section{Second section}
\chapter{Second chapter}
\section{First section}
\subsection{First subsection}
\section{Second section}

\end{document}

答案1

这里有一个tocloft实施以实现您的目标:

在此处输入图片描述

\documentclass{book}

\usepackage{tocloft,etoolbox}
\usepackage[hidelinks,linktoc=all]{hyperref}

% \parts in ToC
\renewcommand{\cftpartfont}{\large\bfseries\ttfamily}
\makeatletter
% \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
\patchcmd{\H@old@part}% <cmd>
  {\thepart\hspace{1em}#1}% <search>
  {\protect\setpartToC{\thepart}{#1}}% <replace>
  {}{}% <success><failure>
\makeatother
\newcommand{\setpartToC}[2]{%
  \texorpdfstring{\parbox{\linewidth}{\centering -\space #1\space- \\ #2}}{#1\space #2}%
}
\renewcommand{\cftpartfillnum}[1]{\par}
% \chapters in ToC
\renewcommand{\cftchapfont}{\bfseries\ttfamily}
\renewcommand{\cftchappresnum}{\fbox}
\addtolength{\cftchapnumwidth}{0.5em}
\renewcommand{\cftchappagefont}{\cftchapfont}
% \sections in ToC
\renewcommand{\cftsecfont}{\ttfamily}
\addtolength{\cftsecindent}{0.5em}% Because of \cftchapnumwidth increment
\renewcommand{\cftsecpagefont}{\cftsecfont}
% \subsections in ToC
\renewcommand{\cftsubsecfont}{\ttfamily}
\addtolength{\cftsubsecindent}{0.5em}% Because of \cftchapnumwidth increment
\renewcommand{\cftsubsecpagefont}{\cftsubsecfont}

% Update numbering of \section and \subsection (remove \chapter reference)
\renewcommand{\thesection}{\arabic{section}}
\renewcommand{\thesubsection}{\arabic{section}.\arabic{subsection}}

\begin{document}

\tableofcontents

\part{First part}
\chapter{First chapter}
\section{First section}
\section{Second section}
\chapter{Second chapter}
\section{First section}
\subsection{First subsection}
\section{Second section}

\end{document}

答案2

基于以下补丁这个答案,我终于可以实现了:

在此处输入图片描述

完整代码:

\documentclass{book}

% book -- titletoc patch: https://tex.stackexchange.com/a/454553/134574
\usepackage{etoolbox}
\makeatletter
\patchcmd\@part% change the part style
  {\addcontentsline{toc}{part}{\thepart\hspace{1em}#1}}
  {\addcontentsline{toc}{part}{\thepart\\\texorpdfstring{\MakeUppercase{#1}}{#1}}}
  {}{\FAIL}
\patchcmd\@part% let titletoc works normally
  {\thepart}
  {\protect\numberline{- \thepart\ -}}
  {}{\FAIL}
\patchcmd\@chapter% change the chapter style
  {\protect\numberline{\thechapter}}
  {\protect\numberline{\fbox{\thechapter}}}
  {}{\FAIL}
\makeatother

\usepackage{titletoc}

\titlecontents{part}
    [0em]
    {\addvspace{1.5pc}\filcenter}
    {\thecontentslabel}
    {}
    {} % without page number
    [\addvspace{.5pc}]
\titlecontents{chapter}
    [2em] % i.e., 0em (part) + 2em
    {\addvspace{.5pc}}
    {\contentslabel{2em}}
    {\hspace*{-2em}}
    {\hfill\contentspage}
\titlecontents{section}
    [4em] % i.e., 2em (chapter) + 2em
    {}
    {\contentslabel{2em}}
    {\hspace*{-2em}}
    {\titlerule*[1em]{.}\contentspage}

\titlecontents{subsection}
    [7em] % i.e., 4em (section) + 3em
    {}
    {\contentslabel{3em}}
    {\hspace*{-3em}}
    {\titlerule*[1em]{.}\contentspage}

\renewcommand{\thesection}{\arabic{section}}
\renewcommand{\thesubsection}{\arabic{section}.\arabic{subsection}}

\usepackage[hidelinks,linktoc=all]{hyperref}

\begin{document}

\ttfamily

\tableofcontents

\part{First part}
\chapter{First chapter}
\section{First section}
\section{Second section}
\chapter{Second chapter}
\section{First section}
\subsection{First subsection}
\section{Second section}

\end{document}

相关内容