[Tex/LaTex] underline and background color for section or subsection

backgroundsunderline

Please , help me to get a section or subsection with an underline and background color as the same figure below ?

Here is the code of my document with ordinary titles :

\documentclass[11pt]{report}
\usepackage[utf8]{inputenc}
\usepackage[francais,english]{babel}
\usepackage[T1]{fontenc}
\usepackage{anysize}
\marginsize{22mm}{14mm}{12mm}{25mm}
\begin{document}
\tableofcontents
\chapter{Chapitre 1}
\section{Section 1 }
\subsection{Subsection 1}
\chapter{Chapitre2}
\end{document}

enter image description here

Best Answer

Something like this?

\documentclass[11pt, french, english]{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage[left=22mm, right=14mm, top=12mm, bottom=25mm, showframe]{geometry}
\usepackage[table, svgnames]{xcolor}
\usepackage{titlesec}

\titleformat{\section}{\sffamily\Large\bfseries\rlap{\color{RoyalBlue!90}\rule[-0.5ex]{\linewidth}{3ex}\vspace{-3ex}}\sffamily\Large\bfseries\color{white}}{\thesection}{1em}{}

\titleformat{\subsection}{\sffamily\large\bfseries\color{RoyalBlue}}{\thesection}{1em}{}[{\color{Tomato}\titlerule[1.5pt]}]

\usepackage{lipsum}

\begin{document}

\tableofcontents
\chapter{Chapitre 1}

\section{Résumé du stage}
\lipsum[1]
\subsection{Contexte}
\lipsum[2
]
\chapter{Chapitre2}

\end{document}

enter image description here

Unrelated: as I have not installed anysize (which is considered obsolete), I replaced it with geometry, but I'm not sure the word ‘margin’ denotes exactly the same lengths, so the parameters I copied from your code might have to be adapted. Also, since version 3.10, the language options for babel should be loaded with the document class, so that all language-dependent packages be informed.

Edit:

In case a section title requires several lines, the above code does not work anymore. A slightly longer code settles the problem. It requires the explicit option of titlesec and the block shape. Here a demo:

\documentclass[11pt, french, english]{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage[left=22mm, right=14mm, top=12mm, bottom=25mm, showframe]{geometry}
\usepackage[table, svgnames]{xcolor}
\usepackage[explicit]{titlesec}

\titleformat{\section}[block]{\sffamily\Large\bfseries\color{white}}%
{}{0em}{\colorbox{RoyalBlue!90}{\parbox{\dimexpr\linewidth-2\fboxsep}{\thesection\enspace #1}}}

\titleformat{\subsection}{\sffamily\large\bfseries\color{RoyalBlue}}{\thesection}{1em}{#1}[{\color{Tomato}\titlerule[1.5pt]}]

\usepackage{lipsum}

\begin{document}

\tableofcontents
\chapter{Chapitre 1}

\section{Résumé du stage : \\
Il laisse aller le marteau – qui tombe, qui tombe, qui tombe,\\
Attache au clou la ficelle – longue, longue, longue, \\
Et, au bout, le hareng saur – sec, sec, sec.}
\lipsum[1]
\subsection{Contexte : lecture des poèmes de Charles Cros}
\lipsum[2
]
\chapter{Chapitre2}

\end{document}

enter image description here