我读过这篇文章: 漂亮的目录
有人能告诉我如何为我的 \part 制作那个圆圈吗?我尝试复制一些代码,但显然没有用。
\documentclass[twoside]{book}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{geometry}
\usepackage{fancyhdr,calc,blindtext}
\usepackage{xwatermark,wallpaper,grffile}
\usepackage{bm,graphicx,empheq,etoolbox}
\usepackage{units,cancel,mathtools,lipsum,lmodern}
\usepackage{varwidth,verbatim,setspace,lmodern}
\usepackage{anyfontsize,rotating,framed,titletoc}
\usepackage{amsmath,tocloft, amssymb,fancyhdr,color,contour}
\usepackage{booktabs,caption,multirow,marginnote}
\usepackage{xcolor}
\usepackage[explicit]{titlesec}
\usepackage[listings]{tcolorbox}
\tcbuselibrary{listings,theorems,skins,breakable
,theorems,many,raster}
\usepackage{hyperref}
\hypersetup{colorlinks=true, linkcolor=black, anchorcolor=black, citecolor=black, urlcolor=black,pdfstartpage=5}
\usepackage{listings, float, wasysym}
\floatstyle{boxed}
\setlength\parindent{0pt}
\DeclareMathAlphabet{\mathpzc}{OT1}{pzc}{m}{it}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{fadings,decorations.text}
\setcounter{tocdepth}{3}
\addtocontents{toc}{\cftpagenumbersoff{chapter}}
\setlength{\cftsubsecindent}{\cftsecnumwidth}
\patchcmd{\tableofcontents}{\contentsname}{\sffamily\contentsname}{}{}
\setlength{\cftbeforechapskip}{6ex}
\setlength{\cftbeforesecskip}{0ex}
%---this doesn't work!--------------
\newcommand\mycircle[1]{
\begin{tikzpicture}%
\node[draw=yellow,circle, text width=18pt,line width=1pt,align=center] {#1};
\end{tikzpicture}}
\makeatletter
\patchcmd{\@part}
{\addcontentsline{toc}{part}{\thepart\hspace{1em}#1}}
{\addtocontents{toc}{\protect\addvspace{20pt}}
\addcontentsline{toc}{part}{\huge{\protect\color{yellow}%
\setlength\fboxrule{2pt}\protect\mycircle{%
\hfil\thepart\hfil%
}%
}\\[2ex]\color{red}\sffamily#1}}{}{}
%----------------------------
\begin{document}
\tableofcontents
\part{First Part}
\chapter{My first}
\section{section 1}
\subsection{stuff1}
\subsubsection{substuff1}
\subsection{stuff2}
\subsection{stuff3}
\chapter{My second}
\section{section 2}
-----------
\part{Second Part}
\chapter{My third}
\section{section 3}
\end{document}
答案1
hyperref
重新定义\@part
等相当多,所以\patchcmd{...}
使用后就hyperref
太晚了。补丁必须在hyperref
加载之前完成。
tikzpicture
将像命令这样脆弱的东西写入.aux
文件.toc
很困难,最好对\mycircle
命令进行强化!
\documentclass[twoside]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{geometry}
\usepackage{fancyhdr}
\usepackage{calc}
\usepackage{blindtext}
\usepackage{xwatermark}
\usepackage{wallpaper}
\usepackage{grffile}
\usepackage[explicit]{titlesec}
\usepackage{bm,graphicx,empheq}
\usepackage{units,cancel,mathtools,lipsum,lmodern}
\usepackage{varwidth,verbatim,setspace,lmodern}
\usepackage{anyfontsize,rotating,framed,titletoc}
\usepackage{amsmath,amssymb,contour}
\usepackage{booktabs,caption,multirow,marginnote}
\usepackage{tocloft}
\usepackage{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{etoolbox}
\usepackage{xpatch}
\usepackage{listings}
\usepackage{wasysym}
\usepackage{float}
\makeatletter
\newcommand\mycircle[1]{%
\begin{tikzpicture}%
\node[draw=blue,circle, text width=18pt,line width=1pt,align=center] {#1};
\end{tikzpicture}
}
\robustify{\mycircle}
\xpatchcmd{\@part}{%
\addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
}{%
\addcontentsline{toc}{part}{\huge\texorpdfstring{\hfil\protect\color{blue}%
\mycircle{%
\thepart}\hfil%%
\\[2ex]\color{red}\sffamily#1}{#1}}%
}{\typeout{Success}}{\typeout{Failed}}
%----------------------------
\makeatother
\usepackage{hyperref}
\hypersetup{colorlinks=true, linkcolor=black, anchorcolor=black, citecolor=black, urlcolor=black,pdfstartpage=5}
%\floatstyle{boxed}
\setlength\parindent{0em}
%\DeclareMathAlphabet{\mathpzc}{OT1}{pzc}{m}{it}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{fadings,decorations.text}
\setcounter{tocdepth}{3}
\addtocontents{toc}{\cftpagenumbersoff{chapter}}
\setlength{\cftsubsecindent}{\cftsecnumwidth}
\patchcmd{\tableofcontents}{\contentsname}{\sffamily\contentsname}{}{}
\setlength{\cftbeforechapskip}{6ex}
\setlength{\cftbeforesecskip}{0ex}
%---this doesn't work!--------------
\begin{document}
\tableofcontents
\part{First Part}
\chapter{My first}
\section{section 1}
\subsection{stuff1}
\subsubsection{substuff1}
\subsection{stuff2}
\subsection{stuff3}
\chapter{My second}
\section{section 2}
-----------
\part{Second Part}
\chapter{My third}
\section{section 3}
\end{document}