1

I am using the titlesec package to format the titles of sections. I would like to change the background color of the frame shape. Concretely, I would like to obtain a solid (that is, the same color for the frame and background) blue frame and white text.

I tried the naive approach of using \colorbox as a format in\titleformat but it does not seem to work:

\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}

I would be very grateful if someone could explain how I can obtain the desired result.

2

Is it something like this you want?

\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} 

enter image description here

  • yes, this is the idea, thank you, but without the section numbering (I guess it is enough to change \thesection by \sectionname in the code). I see that you use the option display, so I guess that there is no solid option for frame, right? – suitangi Dec 3 '19 at 11:05
  • Not sure what you mean. Unnumbered sections? – Bernard Dec 3 '19 at 11:09
  • I have a doubt. Do you want a frame, distinct from the background, but of the same colour? – Bernard Dec 3 '19 at 11:12
  • I just don't want the numbers to appear, but this is already fixed changing \thesection by \sectionname in the code. – suitangi Dec 3 '19 at 11:25
  • 1
    Without the section number, the code should be simpler: use the block style, and as last argument, {\colorbox{RoyalBlue!80}{\parbox{\textwidth}{\centering\strut #1\vskip 0.5ex}}}. On the other hand, the frame is fard-coded in tha package, and I don't see how to patch it to obtain a coloured background. – Bernard Dec 3 '19 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.