如何在LaTeX中實現以下目錄格式

toc1.jpg

作者查閱了很多包的說明,勉強做出一點形式,但是并沒有完全解決這個問題,現在給出一種定制的目錄格式,涉及到etoolbox包的一些打補丁掛鉤子的方法:

\documentclass[openany]{ctexbook}

\usepackage{multicol,etoolbox}
\usepackage{titletoc,titlesec,xcolor}
\usepackage[hidelinks]{hyperref}
\usepackage[centering,
           top=2.54cm,bottom=2.54cm,right=2.9cm,left=2.9cm,
           headsep=25pt,headheight=20pt]{geometry}
\usepackage{xcolor}
\usepackage{pgfornament}
\definecolor{myred}{RGB}{127,0,0}
\definecolor{myyellow}{RGB}{169,121,69}
\definecolor{gray75}{gray}{0.75}

\titleformat{\chapter}% Command
  [hang]% Shape
  {\Huge\bfseries\color{myred}}% Format
  {\protect{\color{myyellow}\thechapter\hfil}}% Label
  {1em}% Sep
  {}% Before-Code
  [\ifnum\value{chapter}>0 \addtocontents{toc}{\protect\begin{multicols}{2}}\fi]% After-Code

\pretocmd{\chapter}{\ifnum\value{chapter}>0 \addtocontents{toc}{\protect\end{multicols}}\fi}{}{}
\AtEndDocument{\ifnum\value{chapter}>0 \addtocontents{toc}{\protect\end{multicols}}\fi}

\renewcommand\contentsname{\centerline{目錄}}
\usepackage{tocloft}
\renewcommand\cftsecnumwidth{1em}
\usepackage{tikz}

\definecolor{doc}{RGB}{0,60,110}
\definecolor{myblue}{RGB}{63,200,244}
\titlecontents{chapter}[8em]
{\filcenter
\begin{tikzpicture}[remember picture, overlay]%
\draw[fill=myblue,draw=myblue, sharp corners] (-\textwidth,-.2) rectangle (\textwidth,.5);%
\pgftext[left,x=-2.7cm,y=0.2cm]{\color{black}\Large \heiti\thecontentslabel};%
\end{tikzpicture}\color{black}\large\heiti\bfseries}%
{}
{}
{}%

\begin{document}
\begin{tikzpicture}[remember picture,overlay]
\node at([shift=(-114:6)]current page.north){\pgfornament[scale=0.3,color=blue!20]{78}};
\node at([shift=(-66:6)]current page.north){\pgfornament[scale=0.3,symmetry=v,color=blue!20]{78}};
\end{tikzpicture}
\tableofcontents

\chapter{章節}
\section{bar1}
\section{bar2}
\section{bar3}
\section{bar4}

\chapter{BAR}
\section{foo1}
\section{foo2}
\section{foo3}
\section{foo4}

\end{document}

未解決的問題是無法做到將目錄中的章名稱居中用帶顏色的盒子包住,退而求其次,畫了一整條帶子。

toc2.png

選自: https://yuxtech.github.io/2019/09/23/toc/