自定义目录

自定义目录

我正在尝试自定义目录。我想为每个部分设置一种颜色,如下图所示。紫色代表活动,青色代表课程,蓝色代表练习。

在此处输入图片描述

我只是在 l@section 的定义中添加了一个计数器来做到这一点:

\documentclass[11pt,a4paper,french]{book}
\usepackage{xstring}
\usepackage{titlesec}
\usepackage{tikz}

\newcounter{colorCounter}

\makeatletter

\renewcommand*\l@section[2]{%
\refstepcounter{colorCounter}
\ifnum\c@tocdepth>\m@ne
\addpenalty{-\@highpenalty}%
\vskip 1.0em \@plus\p@
\setlength\@tempdima{1.5em}%
\begingroup
   \parindent \z@ \rightskip \@pnumwidth
   \parfillskip -\@pnumwidth
   \leavevmode \bfseries \large
   \advance\leftskip\@tempdima
   \hskip -\leftskip
      \IfStrEq{\thecolorCounter}{1}{
      \def\@linkcolor{purple!75}}{}
      \IfStrEq{\thecolorCounter}{2}{
      \def\@linkcolor{teal!75}}{}
      \IfStrEq{\thecolorCounter}{3}{
      \def\@linkcolor{blue!75}}{}
          \begin{tikzpicture}
             \node[fill=\@linkcolor,text=white] {};
          \end{tikzpicture}%}
     \color{\@linkcolor}#1
     \nobreak\
     \leaders\hbox{$\m@th
     \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
     mu$}\hfil\nobreak\hb@xt@\@pnumwidth{\hss
     \color{\@linkcolor}#2}\par
     \penalty\@highpenalty
     \ifnum\value{colorCounter}=3 \setcounter{colorCounter}{0}\fi
\endgroup
\medskip
\fi}   

\def\section{\secdef\@section\@ssection}

\def\@section[#1]#2{%
\addcontentsline{toc}{section}{#2}}

\makeatother

\begin{document}
\tableofcontents
\newpage
\chapter{Probability}
\section{Activity}
bla
\section{Cours}
bla
\section{Exercices}
bla
\chapter{Tableur}
\section{Exercices}
bla
\end{document}

除此之外,如果我没有“活动”部分或“课程”部分,它就不再是好颜色了!

答案1

基于更改自定义目录中的图像

这基本上是将颜色变化说明偷偷放入目录中:

\documentclass[11pt,a4paper]{book}
\usepackage{xstring}
\usepackage{titlesec}
\usepackage{tikz}

\makeatletter

\renewcommand*\l@section[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 \bfseries \large
   \advance\leftskip\@tempdima
   \hskip -\leftskip
     \sectoccolor\rule{1.7ex}{1.7ex}~#1
     \nobreak\
     \leaders\hbox{$\m@th
     \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
     mu$}\hfil\nobreak\hb@xt@\@pnumwidth{\hss
     \sectoccolor#2}\par
     \penalty\@highpenalty
\endgroup
\medskip
\fi}   

\def\section{\secdef\@section\@ssection}

\def\@section[#1]#2{%
\addcontentsline{toc}{section}{#2}}

\makeatother

\newcommand*{\setsectoccolor}[1]{\addtocontents{toc}{\gdef\string\sectoccolor{\protect#1}}}  

\setsectoccolor{\color{black}} 

\newcommand{\Activity}{%
    \setsectoccolor{\color{purple!75}}%
    \section{Activity}%
    \setsectoccolor{\color{black}}%
}

\newcommand{\Course}{%
    \setsectoccolor{\color{teal!75}}%
    \section{Course}%
    \setsectoccolor{\color{black}}%
}

\newcommand{\Exercices}{%
    \setsectoccolor{\color{blue!75}}%
    \section{Exercices}%
    \setsectoccolor{\color{black}}%
}

\begin{document}
\tableofcontents
\newpage
\chapter{Probability}

\Activity
bla
\Course
bla
\Exercices
bla
\chapter{Tableur}
\Exercices
bla

\setsectoccolor{\color{orange!75}}%
\section{some other section}
\setsectoccolor{\color{black}}%

\end{document}

在此处输入图片描述

(也许将颜色名称偷偷放入目录中就足够了,但由于上述方法似乎有效,所以我没有进一步调查)

相关内容