我需要更改章节、子章节等的样式。我做到了。问题是我需要目录保留默认样式。
我的新样式中的文本颜色为白色,因此目录似乎没有章节标题。并且不会tocloft
\renewcommand\cftsecfont{\color{black}}
覆盖目录的更改。
\documentclass{article}
\usepackage{xcolor}
\usepackage{tcolorbox}
\usepackage{tocloft}
\renewcommand\cftsecfont{\color{black}}
\newcommand*{\newSection}[1]{
\begin{tcolorbox}[
center title,
left=0pt,
right=0pt,
top=0pt,
bottom=0pt,
colback=blue!75,
colframe=white,
width=\dimexpr\textwidth\relax,
enlarge left by=0mm,
boxsep=5pt,
arc=0pt,outer arc=0pt]
\section{\textcolor{white}{#1}}
\end{tcolorbox}
}
\let\oldthesection\thesection
\renewcommand{\thesection}{\textcolor{white}{\oldthesection}}
\begin{document}
\tableofcontents
\newSection{Section}
\newSection{Another Section}
\newSection{And One More}
\end{document}
答案1
该软件包titlesec
使得更改章节标题的格式变得更加容易,而不会影响目录或其他位置:
\documentclass{article}
\usepackage{xcolor}
\usepackage{tcolorbox}
\usepackage{tocloft}
\usepackage{titlesec}
\newtcolorbox{sectionbox}{
center title,
left=0pt,
right=0pt,
top=0pt,
bottom=0pt,
colback=blue!75,
colframe=white,
width=\dimexpr\textwidth\relax,
enlarge left by=0mm,
boxsep=5pt,
arc=0pt,
outer arc=0pt
}
\titleformat{\section}[block]
{\begin{sectionbox}\normalfont\Large\bfseries\color{white}}
{\thesection}
{1em}
{}
[\end{sectionbox}]
\begin{document}
\tableofcontents
\section{Section}
\section{Another Section}
\section{And One More}
\end{document}
答案2
不使用tocloft
或titlesec
,仅提供正确的选项处理和重新定义,\@seccntformat
这对于更改章节标题的编号格式来说是首选。
\documentclass{article}
\usepackage{xcolor}
\usepackage[most]{tcolorbox}
\makeatletter
\let\@old@seccntformat\@seccntformat
\NewDocumentCommand{\newSection}{O{#2}mO{}}{
\begin{tcolorbox}[
enhanced,
frame hidden,
center title,
left=0pt,
right=0pt,
top=0pt,
bottom=0pt,
colback=blue!75,
colframe=white,
boxsep=5pt,
sharp corners,
#3
]
\begingroup
\renewcommand{\@seccntformat}[1]{%
\ifnum0=\pdfstrcmp{##1}{section}%
\textcolor{white}{\@old@seccntformat{##1}}%
\else
\@old@seccntformat{##1}%
\fi
}
\section[\color{black}#1]{\textcolor{white}{#2}}%
\endgroup
\end{tcolorbox}
}
\makeatother
\begin{document}
\tableofcontents
\newSection{Section}
\newSection{Another Section}
\newSection{And One More}
\newSection{Don't screw up the layout}
\end{document}