如何使书中目录中的部分内容具有彩色背景?

如何使书中目录中的部分内容具有彩色背景?

我想获取目录中的彩色背景部分。

梅威瑟:

\documentclass[lang=en,11pt]{elegantbook}
\usepackage{mathbang} % to write bengali digits in math mode
%--------------------------------------------------
%Bengali Font in Document
%--------------------------------------------------

%\usepackage[banglamainfont=Kalpurush, banglattfont=Siyam Rupali, feature=0, changecounternumbering=0]{latexbangla} % to write bengali in latex
%.................................................................................


\usepackage{xcolor} % Required for specifying colors by name
\definecolor{ocre}{RGB}{243,102,25} % Define the orange color used for highlighting



%----------------------------------------------------------------------------------------
%   BIBLIOGRAPHY AND INDEX
%----------------------------------------------------------------------------------------

%\usepackage[style=numeric,citestyle=numeric,sorting=nyt,sortcites=true,autopunct=true,babel=hyphen,hyperref=true,abbreviate=false,backref=true,backend=biber]{biblatex}
%\addbibresource{bibliography.bib} % BibTeX bibliography file
%\defbibheading{bibempty}{}

\usepackage{calc} % For simpler calculation - used for spacing the index letter headings correctly

\usepackage{makeidx} % Required to make an index
\makeindex % Tells LaTeX to create the files required for indexing


%create an .ist style file named as myheader.ist and tell makeindex to use it. My style file is as follows:
%headings_flag 1
%heading_prefix "{\\textbf{"
%heading_suffix "}}\\nopagebreak\n"

%Running the following commands to make index:
%pdflatex myfile.tex
%makeindex -s myheader.ist elegantbook-en.idx
%pdflatex myfile.tex

%----------------------------------------------------------------------------------------
%   MAIN TABLE OF CONTENTS
%----------------------------------------------------------------------------------------

\usepackage{titletoc} % Required for manipulating the table of contents

\contentsmargin{0cm} % Removes the default margin

% Part text styling (this is mostly taken care of in the PART HEADINGS section of this file)
\titlecontents{part}
    [0cm] % Left indentation
    {\addvspace{20pt}\bfseries} % Spacing and font options for parts
    {}
    {}
    {}

% Chapter text styling
\titlecontents{chapter}
    [1cm] % Left indentation
    {\addvspace{12pt}\large\sffamily\bfseries} % Spacing and font options for chapters
    {\color{blue!80!ocre}\contentslabel[\Large\thecontentslabel]{1.25cm}\color{blue!80!ocre}} % Formatting of numbered sections of this type
    {\color{blue!80!ocre}} % Formatting of numberless sections of this type
    {\color{blue!80!ocre}\normalsize\;\titlerule*[.5pc]{.}\;\thecontentspage} % Formatting of the filler to the right of the heading and the page number

% Section text styling
\titlecontents{section}
    [2cm] % Left indentation
    {\addvspace{3pt}\sffamily\bfseries} % Spacing and font options for sections
    {\color{blue!60!red}\contentslabel[\Large\thecontentslabel]{1.50cm}\color{blue!60!red}} % Formatting of numbered sections of this type
    {\color{blue!60!red}} % Formatting of numberless sections of this type
    {\color{blue!60!red}\normalsize\;\titlerule*[.5pc]{.}\;\thecontentspage} % Formatting of the filler to the right of the heading and the page number

% Subsection text styling
\titlecontents{subsection}
    [3cm] % Left indentation
{\addvspace{1pt}\sffamily\bfseries} % Spacing and font options for sections
    {\color{blue!40!orange}\contentslabel[\Large\thecontentslabel]{2cm}\color{blue!40!orange}} % Formatting of numbered sections of this type
    {\color{blue!40!orange}} % Formatting of numberless sections of this type
    {\color{blue!40!orange}\normalsize\; \titlerule*[.5pc]{.}\;\thecontentspage} % Formatting of the filler to the right of the heading and the page number



% Figure text styling
\titlecontents{figure}
    [1.25cm] % Left indentation
    {\addvspace{1pt}\sffamily\small} % Spacing and font options for figures
    {\thecontentslabel\hspace*{1em}} % Formatting of numbered sections of this type
    {} % Formatting of numberless sections of this type
    {\ \titlerule*[.5pc]{.}\;\thecontentspage} % Formatting of the filler to the right of the heading and the page number

% Table text styling
\titlecontents{table}
    [1.25cm] % Left indentation
    {\addvspace{1pt}\sffamily\small} % Spacing and font options for tables
    {\thecontentslabel\hspace*{1em}} % Formatting of numbered sections of this type
    {} % Formatting of numberless sections of this type
    {\ \titlerule*[.5pc]{.}\;\thecontentspage} % Formatting of the filler to the right of the heading and the page number

%----------------------------------------------------------------------------------------
%   MINI TABLE OF CONTENTS IN PART HEADS
%----------------------------------------------------------------------------------------

% Chapter text styling
\titlecontents{lchapter}
    [0em] % Left indentation
    {\addvspace{15pt}\large\sffamily\bfseries} % Spacing and font options for chapters
    {\color{ocre}\contentslabel[\Large\thecontentslabel]{1.25cm}\color{ocre}} % Chapter number
    {}  
    {\color{ocre}\normalsize\sffamily\bfseries\;\titlerule*[.5pc]{.}\;\thecontentspage} % Page number

% Section text styling
\titlecontents{lsection}
    [0em] % Left indentation
    {\sffamily\small} % Spacing and font options for sections
    {\contentslabel[\thecontentslabel]{1.25cm}} % Section number
    {}
    {}

% Subsection text styling (note these aren't shown by default, display them by searchings this file for tocdepth and reading the commented text)
\titlecontents{lsubsection}
    [.5em] % Left indentation
    {\sffamily\footnotesize} % Spacing and font options for subsections
    {\contentslabel[\thecontentslabel]{1.25cm}}
    {}
    {}

%--------------------------------------------------
%Header Ornamenty
%--------------------------------------------------
\fancyhead[R]{\color{structurecolor}\thepage}
\usepackage{fourier-orns}
\renewcommand\headrule{\hrulefill
    \raisebox{-2.1pt}[10pt][10pt]{\color{structurecolor}\quad\decofourleft\decoone\decofourright\quad}\hrulefill}

%.................................................................................


\title{This is  Sample Title}
\subtitle{Classic Book}

\author{Author's Name}
\institute{Author's  Institution}
\date{\today}
\version{3.10}
\bioinfo{Bio}{Information}

\extrainfo{Victory won\rq t come to us unless we go to it. }

\logo{logo-blue.png}
\cover{cover.jpg}

\begin{document}

\maketitle

\frontmatter
\tableofcontents

\mainmatter
\chapter{This is first Chapter}
Chapter contents here.

\newpage 
\section{First section}
Section  contents here.

\newpage 
\subsection{First section}
Subsection  contents here.


Some text.\index{Apple}\index{Apricot}\index{Avocado}\index{Banana}
\index{Bilberry}\index{Blackberry}\index{Blackcurrant}\index{Blueberry}


\newpage 
\subsubsection{First section}
Subsection  contents here


%\printbibliography[heading=bibempty,type=book]

%----------------------------------------------------------------------------------------
%   INDEX
%----------------------------------------------------------------------------------------

\cleardoublepage % Make sure the index starts on an odd (right side) page
\phantomsection
\setlength{\columnsep}{0.75cm} % Space between the 2 columns of the index
\addcontentsline{toc}{chapter}{\textcolor{ocre}{Index}} % Add an Index heading to the table of contents
\printindex % Output the index

%----------------------------------------------------------------------------------------


\end{document}

输出:
在此处输入图片描述

想要得到: 在此处输入图片描述

答案1

在此处输入图片描述

\documentclass{book}
\usepackage{tocloft,xcolor}

\newcommand{\defineauthorcolor}[2]{%
  \colorlet{author#1}{#2}% Create an author colour
  \expandafter\def\csname authoredby#1\endcsname{% Create author colour settings
    \renewcommand{\cftchapfont}{\bfseries\color{author#1}}% Chapter colour
    \renewcommand{\cftsecfont}{\color{author#1}}% Section colour
    \renewcommand{\cftsubsecfont}{\color{author#1}}}% Subsection colour
}
\makeatletter
\newcommand{\authoredby}[1]{\addtocontents{toc}{\protect\@nameuse{authoredby#1}}}%
\makeatother

\defineauthorcolor{A}{red}% Author A will be coloured red
\defineauthorcolor{B}{blue}% Author B will be coloured blue

\begin{document}

\tableofcontents

\authoredby{A}
\chapter{A chapter}
\authoredby{B}
\section{A section}
\subsection{A subsection}
\authoredby{A}
\section{Another section}
\subsection{Another subsection}
\authoredby{B}
\subsection{Yet another subsection}

\chapter{Another chapter}
\section{First section}
\section{Second section}
\section{Last section}

\authoredby{A}
\chapter{Last chapter}

\end{document}

部件号的中心标题

在此处输入图片描述

\documentclass{book}
\usepackage{tocloft}
\usepackage{tcolorbox}

\makeatletter
\renewcommand*\l@part[2]{%
  \ifnum \c@tocdepth >-2\relax
    \addpenalty{-\@highpenalty}%
    \addvspace{2.25em \@plus\p@}%
    \setlength\@tempdima{3em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \begin{tcolorbox}[colback=orange!10, right=0.5ex, left=0cm, sharpish corners, boxrule=0mm, width=1.01\textwidth]
      {\leavevmode
       \large \bfseries \centering \vbox{\sffamily\bfseries#1}%
      \hb@[email protected]{\hss #2}
       }
       \end{tcolorbox}
       \vspace*{1ex}
       \nobreak
         \global\@nobreaktrue
         \everypar{\global\@nobreakfalse\everypar{}}%
    \endgroup
  \fi}
 \makeatother
\begin{document}
\tableofcontents

\part{Part 1}
\chapter{Lorem ipsum}
\section{Dolor sit amet}

\end{document}

相关内容