如何更改目录的样式和颜色?

如何更改目录的样式和颜色?

我想更改目录的样式和颜色以匹配以下内容:

在此处输入图片描述

请注意,我使用该minitoc包按章节创建目录。

答案1

我不太清楚minitoc,但这里有一种方法可以使用titletoc包;一旦在序言中定义了所有设置,所有工作都使用单个\PartialToC命令完成:

\documentclass{book}
\usepackage{xcolor}
\usepackage{mdframed}
\usepackage{titletoc}

\definecolor{secnum}{RGB}{13,151,225}
\definecolor{ptcbackground}{RGB}{212,237,252}
\definecolor{ptctitle}{RGB}{0,177,235}

\titlecontents{lsection}
  [5.8em]{\sffamily}
  {\color{secnum}\contentslabel{2.3em}\normalcolor}{}
  {\titlerule*[1000pc]{.}\contentspage\\\hspace*{-5.8em}\vspace*{5pt}%
    \color{white}\rule{\dimexpr\textwidth-15.5pt\relax}{1pt}}

\newcommand\PartialToC{%
\startcontents[chapters]%
\begin{mdframed}[backgroundcolor=ptcbackground,hidealllines=true]
\printcontents[chapters]{l}{1}{\colorbox{ptctitle}{%
  \parbox[t]{\dimexpr\textwidth-2\fboxsep\relax}{%
    \strut\color{white}\bfseries\sffamily\makebox[5em]{%
      Chapter~\thechapter\hfill}Contents}}\vskip5pt}
\end{mdframed}%
}

\begin{document}

\tableofcontents
\chapter{Chapter One}
\PartialToC
\section{Section One One}
\section{Section One Two}
\section{Section One Three}
\section{Section One Four}

\chapter{Chapter Two}
\PartialToC
\section{Section Two One}
\section{Section Two Two}
\section{Section Three Three}

\end{document}

部分目录的一些图片:

在此处输入图片描述

在此处输入图片描述

在对答案的评论中,已要求为一般目录生成类似的格式;在这种情况下,必须做一些额外的工作:

\documentclass{book}
\usepackage{xcolor}
\usepackage{mdframed}
\usepackage{titletoc}
\usepackage{etoolbox}


\definecolor{secnum}{RGB}{13,151,225}
\definecolor{ptcbackground}{RGB}{212,237,252}
\definecolor{ptctitle}{RGB}{0,177,235}

\pretocmd{\tableofcontents}{\begin{mdframed}[backgroundcolor=ptcbackground,hidealllines=true]}{}{}
\apptocmd{\tableofcontents}{\end{mdframed}}{}{}
\patchcmd{\tableofcontents}{\contentsname}{\color{ptctitle}\contentsname}{}{}

\titlecontents{section}
  [4em]{\sffamily}
  {\color{secnum}\contentslabel{2.3em}\normalcolor}{}
  {\titlerule*[1000pc]{.}\contentspage\\\hspace*{-3em}\vspace*{2pt}%
    \color{white}\rule{\dimexpr\textwidth-20pt\relax}{1pt}}

\titlecontents{lsection}
  [5.8em]{\sffamily}
  {\color{secnum}\contentslabel{2.3em}\normalcolor}{}
  {\titlerule*[1000pc]{.}\contentspage\\\hspace*{-5.8em}\vspace*{2pt}%
    \color{white}\rule{\dimexpr\textwidth-15.5pt\relax}{1pt}}

\makeatletter
\renewcommand*\l@chapter[2]{%
  \ifnum \c@tocdepth >\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip 1.0em \@plus\p@
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      \colorbox{ptctitle}{\strut%
        \makebox[\dimexpr\textwidth-2\fboxsep-7pt\relax][l]{%
          \color{white}\bfseries\sffamily#1%
          \nobreak\hfill\nobreak\hb@xt@\@pnumwidth{\hss #2}}}\par\smallskip
      \penalty\@highpenalty
    \endgroup
  \fi}
\makeatother
\newcommand\PartialToC{%
\startcontents[chapters]%
\begin{mdframed}[backgroundcolor=ptcbackground,hidealllines=true]
\printcontents[chapters]{l}{1}{\colorbox{ptctitle}{%
  \parbox[t]{\dimexpr\textwidth-2\fboxsep\relax}{%
    \strut\color{white}\bfseries\sffamily\makebox[5em]{%
      Chapter~\thechapter\hfill}Contents}}\vskip5pt}
\end{mdframed}%
}

\begin{document}

\tableofcontents

\chapter{Chapter One}
\PartialToC
\section{Section One One}
\section{Section One Two}
\section{Section One Three}
\section{Section One Four}

\stopcontents[chapters]
\chapter{Chapter Two}
\PartialToC
\section{Section Two One}
\section{Section Two Two}
\section{Section Two Three}

\stopcontents[chapters]
\chapter{Chapter Three}
\PartialToC
\section{Section Three One}
\section{Section Three Two}
\section{Section Three Three}
\section{Section Three Four}
\section{Section Three Five}

\end{document}

通用目录的图片:

在此处输入图片描述

部分 ToC 之一的图像:

在此处输入图片描述

相关内容