如何用不同的顏色為節號和節名著色?

如何用不同的顏色為節號和節名著色?

我想在\documentclass{book}使用sectsty包時對節號和節名稱使用不同的顏色。部分編號必須放入彩盒中。給我一個主意。

答案1

我認為sectsty不太適合這項任務。我建議你titlesec包代替。一個小例子(依照你的喜好調整顏色):

\documentclass{book}
\usepackage{titlesec}
\usepackage{xcolor}

\colorlet{sectitlecolor}{red!60!black}
\colorlet{sectboxcolor}{cyan!30}
\colorlet{secnumcolor}{orange}

\titleformat{\section}
  {\normalfont\Large\bfseries\color{sectitlecolor}}{\colorbox{sectboxcolor}{\textcolor{secnumcolor}{\thesection}}}{1em}{}

\begin{document}

\chapter{A test chapter}
\section{A test section}

\end{document}

在此輸入影像描述

在評論中,要求將節號移至左邊距:

\documentclass{book}
\usepackage{titlesec}
\usepackage{xcolor}

\colorlet{sectitlecolor}{red!60!black}
\colorlet{sectboxcolor}{cyan!30}
\colorlet{secnumcolor}{orange}

\titleformat{\section}
  {\normalfont\Large\bfseries\color{sectitlecolor}}{\llap{\makebox[3em][l]{\colorbox{sectboxcolor}{\textcolor{secnumcolor}{\thesection}}}}}{0em}{}

\begin{document}

\chapter{A test chapter}
\section{A test section}

\end{document}

答案2

是的,您可以使用 來做到這一點sectsty,但是您需要更改 的定義\@seccntformat,它負責列印節號。

請注意,這也會使小節和小節標題的顏色相同;當然可以為它們定義不同的顏色或裝箱。

我使用了與貢薩洛相同的設定。

\documentclass{book}

\usepackage{sectsty,xcolor}

\colorlet{sectitlecolor}{red!60!black}
\colorlet{sectboxcolor}{cyan!30}
\colorlet{secnumcolor}{orange}

\sectionfont{\color{sectitlecolor}}

\makeatletter
\renewcommand\@seccntformat[1]{%
  \colorbox{sectboxcolor}{\textcolor{secnumcolor}{\csname the#1\endcsname}}%
  \quad
}
\makeatother

\begin{document}

\chapter{A test chapter}
\section{A test section}

\end{document}

在此輸入影像描述

相關內容