2

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

4
  • I did not do anything, I'm still looking for the order that makes the section in color or underlined?
    – YounessGI
    May 5 '18 at 12:58
  • I edited the Question , you will find the code inside .. please help mee
    – YounessGI
    May 5 '18 at 13:10
  • Okey , I will wait .
    – YounessGI
    May 5 '18 at 13:16
  • You may want to take a look at the dedicated tool: package titlesec.
    – Bernard
    May 5 '18 at 13:28
5

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

2
  • Thaaank's , that's help me ! very good
    – YounessGI
    May 6 '18 at 12:41
  • Bernard , i still have a problem when i have a section name much long , i can't see the last part of text !
    – YounessGI
    May 6 '18 at 13:32

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.