titlesec 中的背景顏色

titlesec 中的背景顏色

我正在使用該titlesec包來格式化各部分的標題。我想更改框架形狀的背景顏色。具體來說,我想獲得一個純色(即框架和背景顏色相同)藍色框架和白色文字。

\colorbox我嘗試了使用作為格式的幼稚方法,\titleformat但它似乎不起作用:

\documentclass{article}
\usepackage{xcolor}
\usepackage{titlesec}

\setcounter{secnumdepth}{0}

\newcommand{\mybox}[1]{
\colorbox{blue}{\textcolor{white}{#1}}
}

\titleformat{\section}
[frame]
{
\vspace{.8ex}%
\color{blue}\Large\normalfont\sffamily\mybox
}
{\thesection.}
{.5em}
{}

\begin{document}
\section{First section}
The first section begins here\ldots
\section{Second section}
The second section begins here\ldots

\end{document}

如果有人能解釋我如何獲得所需的結果,我將不勝感激。

答案1

你想要這樣的東西嗎?

\documentclass{article}
\usepackage[svgnames]{xcolor}
\usepackage[explicit]{titlesec}


\titleformat{\section}
[display]
{\filcenter
\Large\normalfont\sffamily\color{white}
}
{}
{0pt}
{\colorbox{RoyalBlue!80}{\parbox{\textwidth}{\centering\thesection\strut\\[1ex] #1\vskip 0.5ex}}}

\begin{document}
\section{First section}
The first section begins here\ldots
\section{Second section}
The second section begins here\ldots

\end{document} 

在此輸入影像描述

相關內容