タイトルの背景色

タイトルの背景色

セクションのタイトルをフォーマットするためにパッケージを使用しています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} 

ここに画像の説明を入力してください

関連情報