答案1
像这样吗?
\documentclass{book}
\usepackage{tikz}
\usepackage{titlesec}
\usepackage{lipsum}
\makeatletter
\newcommand*{\@chaptercolor}{red!50}
\newcommand*{\chaptercolor}[1]{%
\renewcommand*{\@chaptercolor}{#1}%
}
\titleformat{\chapter}{\sffamily\bfseries\Large}{%
\smash{%
\begin{tikzpicture}[baseline=0]
\useasboundingbox (-1,-1) rectangle (1,1);
\draw[white, line width=4pt, fill=\@chaptercolor] (-1,-1) rectangle (1,1);
\draw (-1,-1) rectangle (1,1);
\node at (0,0) {\Huge\thechapter};
\node at (0,1.3) {Chapter};
\draw[overlay, line width=.2pt] (1.1,0) -- (\dimexpr\linewidth-1cm,0);
\end{tikzpicture}%
}%
}{0mm}{\newline\hspace*{2mm}}
\makeatother
\begin{document}
\chapter{Analysis of Algorithms}
\lipsum[1]
\chaptercolor{blue!50}
\chapter{Another chapter}
\lipsum[2]
\end{document}
您可以将框的颜色更改为 Ti 理解的任何颜色钾Z 和\chaptercolor{<color>}
。例如,上面的代码还会输出以下内容。
新颜色将保持不变,直到您再次更改它。