I’m trying to adapt one of the answers to this question so that only the section text, and not the section number, has a coloured background. Unfortunately the use of \parbox seems to result in a non-uniform baseline (if you look closely, you can see that “This is the title” is rendered on a lower baseline than “0.1”):

What I’m after is something like the text underneath, but with the shading extending all the way to the right. The use of \parbox seems to be the problem (see here for example) but I don’t know how to make the shading work otherwise.

\documentclass[a4paper]{scrbook}
\usepackage{xcolor,lipsum}
\usepackage{titlesec}

\titleformat{name=\section}[block]
{\sffamily\large}
{}
{0pt}
{\colorsection}
\titlespacing*{\section}{0pt}{\baselineskip}{\baselineskip}

\newcommand{\colorsection}[1]{%
\thesection\ \colorbox{blue!20}{\parbox{\dimexpr\textwidth-2\fboxsep}{#1}}}

\begin{document}

\section{This is the title}
\noindent{\sffamily\large 0.1 \colorbox{blue!20}{This is the title}}
\end{document}

• Not good since the blue box is too wide. Load \usepackage{showframe} to see. Jan 3 '20 at 18:26
• You have to use \parbox[t] (otherwise test with a very long section title). But you still have to take care of box width. Jan 3 '20 at 18:28

First off, don't use titlesec with scrbook. They are incompatible and support for titlesec will be dropped in a near future.

The trick to use is \parbox[t]. You also have to take care of the space for the section number.

\documentclass[a4paper]{book}
\usepackage{xcolor,lipsum}
\usepackage{titlesec}

\titleformat{name=\section}[block]
{\sffamily\large}
{}
{0pt}
{\colorsection}
\titlespacing*{\section}{0pt}{\baselineskip}{\baselineskip}

\newlength{\secnumberwd}

\newcommand{\colorsection}[1]{%
\settowidth\secnumberwd{\thesection\ }%
\thesection\ \colorbox{blue!20}{\parbox[t]{\dimexpr\textwidth-2\fboxsep-\secnumberwd}{#1}}%
}

\begin{document}

\section{This is the title}

\noindent{\sffamily\large 0.1 \colorbox{blue!20}{This is the title}}

\end{document}


• Grr. I didn’t spot that the other question used scrbook, which I’m not interested in. It would have been better to use article for the MWE, but I won’t change it now. Thanks for the answer - I hadn’t spotted the need for -\secnumberwd but that makes sense.
– Roly
Jan 4 '20 at 10:11

Here is a suggestion using scrbook without package titlesec, because titlesec should not be used with a KOMA-Script class. You can redefine \sectionlinesformat:

\documentclass{scrbook}
%\providecommand*{\Ifstr}{\ifstr}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif

\usepackage{xcolor}

\RedeclareSectionCommand[
beforeskip=\baselineskip,
afterskip=\baselineskip,
afterindent=false,
font=\large\mdseries
]{section}
\renewcommand*\sectionformat{\thesection\ }
\colorlet{sectionbgcolor}{blue!20}

\newcommand*\originalsectionlinesformat{}
\let\originalsectionlinesformat\sectionlinesformat
\renewcommand*\sectionlinesformat[4]{%
\Ifstr{#1}{section}
{\colorsectionlinesformat{#1}{#2}{#3}{#4}}% for level section
{\originalsectionlinesformat{#1}{#2}{#3}{#4}}% original definition for other levels using style=section
}

\newlength{\secnumberwd}
\makeatletter
\newcommand{\colorsectionlinesformat}[4]{%
\settowidth\secnumberwd{\hskip #2#3}%
\@hangfrom{\hskip #2#3}{%
\colorbox{#1bgcolor}{%
\parbox[t]{\dimexpr\textwidth-2\fboxsep-\secnumberwd}{#4}%
}}}
\makeatother

\begin{document}
\section{This is the title}
{\sffamily\large 0.1 \colorbox{blue!20}{This is the title}\par}