ToC 自定义格式

ToC 自定义格式

我正在寻求帮助以创建基于以下格式的自定义目录:

在此处输入图片描述

但是默认的 ToC 不允许我更改设置。我知道有一个titletoc包,但我无法成功理解设置。

以下是 MWE:

\documentclass[11pt,twoside,openany]{book}
\begin{document}
%-------------------------------------------------------------------------------
    \tableofcontents
    \clearpage
    \thispagestyle{empty}
%-------------------------------------------------------------------------------
    \mainmatter
    \chapter{chap1}
    \section{sec1}
    \subsection{subsec1}
    \chapter{chap2}
    \section{sec2}
    \subsection{subsec2}
\end{document}

任何提供的帮助都将受到感激。

答案1

这是一个tocloft基于的解决方案。

  • 请注意,我没有对“目录”标题的外观做任何处理 - 默认是一个名为“目录”的章节式无编号标题 - 因为您没有指定如何排版此标题。

  • 我提供了一些代码来展示如何在目录中排版未编号的节级标题。

  • 我假设子节级标题及以下部分不应显示在目录中,因为您的屏幕截图没有显示任何此类标题。如果此假设不合适,请指出子节级标题应如何在目录中显示。

在此处输入图片描述

\documentclass[11pt,twoside,openany]{book}
\usepackage[letterpaper,margin=1in]{geometry} % make some suitable choices
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{xcolor}

\usepackage{tocloft}
\tocloftpagestyle{empty} % don't show page numbers in ToC
\renewcommand{\cftdot}{} % no dot leaders
\renewcommand{\cftchapfont}{\sffamily\bfseries}
\newlength\mylen
\settowidth\mylen{\cftchapfont CHAPTER 1\quad} % calculate width of indentation
\setlength{\cftchapnumwidth}{\mylen}
\renewcommand{\cftchappagefont}{\cftchapfont}  % same font as for chapter headers
\renewcommand{\cftchappresnum}{\color{gray}CHAPTER } % use 'gray' 
\renewcommand{\cftchapafterpnum}{\vspace{1ex}} % add'l vertical whitespace

\cftsetindents{section}{\mylen}{2em} % indentation for section-level headers
%%\cftsetindents{subsection}{\dimexpr\mylen+2em\relax}{3em}
\setcounter{tocdepth}{1} % Don't show subsection-level headers and below in TOC

\begin{document}
    \frontmatter % use lower-case roman numerals for page numbers in frontmatter
    \tableofcontents
    \clearpage
    \thispagestyle{empty}

    \mainmatter % switch to arabic numerals for page numbers

    \chapter{Fundamental Operations with Numbers}
    \section{Four Operations}
    \section{System of Real Numbers}
    \section{Graphical Representation of Real Numbers}
    \subsection{subsec1}
    \section*{Solved Problems}
    \addcontentsline{toc}{section}{\hspace{2em}Solved Problems}
    \section*{Supplementary Problems}
    \addcontentsline{toc}{section}{\hspace{2em}Supplementary Problems}

    \chapter{Other Operations}
    \section{Section \thesection}
    \subsection{subsec2}

\end{document}

答案2

以下是使用包的另一个建议tocbasic

\documentclass[11pt,twoside,openany]{book}
%\usepackage[utf8]{inputenc}% only needed with older TeX-Distributions
\usepackage[T1]{fontenc}
\usepackage{xcolor}

\usepackage{scrextend}% pagestyle empty for interleaf pages
\usepackage{tocbasic}
\addtotoclist[\jobname]{toc}
\renewcommand\tableofcontents{\listoftoc[{\contentsname}]{toc}}% \addxcontentsline can be used
\setuptoc{toc}{numberline}% indent unnumbered entries added with \addxcontentsline

\newcommand\chapterentryformat[1]{\bfseries\sffamily #1}
\newcommand\usechapterprefix[1]{\textcolor{gray}{\MakeUppercase{\chaptername}~#1}}
\newlength\mylen
\settowidth{\mylen}{\chapterentryformat{\usechapterprefix{9}\quad}}

\DeclareTOCStyleEntry[
  entryformat=\chapterentryformat,
  entrynumberformat=\usechapterprefix,
  numwidth=\mylen
]{tocline}{chapter}

\DeclareTOCStyleEntry[
  indent=\mylen,
  linefill=\hfill,% no dots before page number
  onstarthigherlevel=\addvspace{.5em plus 1pt}\LastTOCLevelWasLower% add vertical space before first section in chapter
]{tocline}{section}
\setcounter{tocdepth}{1}

\begin{document}
\frontmatter
\tableofcontents

\mainmatter
\chapter{Fundamental Operations with Numbers}
\section{Four Operations}
\section{System of Real Numbers}
\section{Graphical Representation of Real Numbers}
\subsection{subsec1}
\section*{Solved Problems}
\addxcontentsline{toc}{section}{Solved Problems}% use \addxcontentsline
\section*{Supplementary Problems}
\addxcontentsline{toc}{section}{Supplementary Problems}% use \addxcontentsline

\chapter{Other Operations}
\section{Section \thesection}
\subsection{subsec2}
\end{document}

在此处输入图片描述

答案3

希望以下标签能帮到您:

\documentclass[11pt,twoside,openany]{book}
\usepackage{titlesec}
\usepackage{titletoc}

\titlecontents{chapter}
[0pt]
{\addvspace{1pc}%
}%
{\contentsmargin{0pt}%
\bfseries
{\huge\chaptername~\thecontentslabel\enspace}%
\large}
{\contentsmargin{0pt}%
\large}
{\hfill\contentspage}
[\addvspace{.5pc}]

\titlecontents{section}
              [114pt]
              {}
              {{\thecontentslabel}\enskip}
              {}
              {\titlerule*[.5pc]{.}\contentspage}

\setcounter{tocdepth}{1}
\begin{document}
%-------------------------------------------------------------------------------
    \tableofcontents
    \clearpage
    \thispagestyle{empty}
%-------------------------------------------------------------------------------
    \mainmatter
    \chapter{chap1}
    \section{sec1}
    \subsection{subsec1}
    \chapter{chap2}
    \section{sec2}
    \subsection{subsec2}
\end{document}

相关内容