1

I want to format my (sub)section numbering in a colored box and a horizontal line below the title. I need something like this:

enter image description here

I tried sectsty but could not get the line to work.

\documentclass{scrbook}
\usepackage{sectsty}
\usepackage[table]{xcolor}
\usepackage{blindtext}

\colorlet{sectitlecolor}{darkgray} \colorlet{sectboxcolor}{darkgray} \colorlet{secnumcolor}{white} \sectionfont{\color{sectitlecolor}} \makeatletter

\renewcommand@seccntformat[1]{% \colorbox{sectboxcolor}{\textcolor{secnumcolor}{\csname the#1\endcsname} }% \quad } \makeatother

\begin{document} \section{Section 1} \blindtext \subsection{Subsection 1} \blindtext \end{document}

Thanks in advance!!

cpt
  • 11

2 Answers2

1

I don't think sectsty is compatible with scrbook.

The \@seccntformat way seems good, but probably there are tools specific for the KoMa bundle.

A problem is that the section title can have descenders and one has to avoid they clash with the rule. I solve this by adding an invisible rule in the colored box that goes below the baseline by 2pt. The rule is drawn at the beginning, shifted down to match the \colorbox.

\documentclass{scrbook}
\usepackage[table]{xcolor}
\usepackage{blindtext}

\colorlet{sectitlecolor}{darkgray} \colorlet{sectboxcolor}{darkgray} \colorlet{secnumcolor}{white} \setkomafont{disposition}{\color{sectitlecolor}\bfseries\sffamily}

\makeatletter \renewcommand@seccntformat[1]{% \makebox[0pt][l]{\rule[-\dimexpr\fboxsep+2pt\relax]{\columnwidth}{1.2pt}}% \colorbox{sectboxcolor}{% \rule[-2pt]{0pt}{0pt}% \color{secnumcolor}\csname the#1\endcsname }% \quad } \makeatother

\begin{document}

\section{Section 1 gyq} \blindtext

\subsection{Subsection 1} \blindtext

\end{document}

enter image description here

Of course this requires that all titles fit on one line.

egreg
  • 1,121,712
1

Do not use packages like secsty or titlesec together with a KOMA-Script class. KOMA-Script provides \sectionlinesformat, chapterlinesformat etc. which can be redefined:

\documentclass{scrbook}
\usepackage[table]{xcolor}
\usepackage{blindtext}

\colorlet{sectitlecolor}{darkgray} \colorlet{sectboxcolor}{darkgray} \colorlet{secnumcolor}{white}

\addtokomafont{section}{\color{sectitlecolor}}

\renewcommand\sectionformat{\colorbox{sectboxcolor}{\textcolor{secnumcolor}{~\thesection}~}\quad} \renewcommand\subsectionformat{\colorbox{sectboxcolor}{\textcolor{secnumcolor}{~\thesubsection}~}\quad}

\newsavebox{\secnumbox} \newlength{\secrulewidth} \setlength{\secrulewidth}{1pt} \newcommand*\boxedandruledsec[2]{% \IfUseNumber{\leavevmode\rlap{\color{sectboxcolor}\rule[\dimexpr-\fboxsep-\secrulewidth\relax]{\textwidth}{\secrulewidth}}}{}% \savebox{\secnumbox}{#1}% \parbox[b]{\wd\secnumbox}{\usebox{\secnumbox}}% \parbox[b]{\dimexpr\textwidth-\wd\secnumbox\relax}{#2}\par\nobreak% }

\newcommand\originalsectionlinesformat{} \let\originalsectionlinesformat\sectionlinesformat

\renewcommand\sectionlinesformat[4]{\setlength\fboxsep{\dp\strutbox}% \Ifstr{#1}{section}{\boxedandruledsec{#3}{#4}}{% \Ifstr{#1}{subsection}{\boxedandruledsec{#3}{#4}}{% \originalsectionlinesformat{#1}{#2}{#3}{#4}% other section levels }}% }

\begin{document} \chapter{Chapter 1} \section{Section 1} \blindtext \subsection{Subsection 1} \blindtext \subsection{What should happen if the title is really really long and needs two or more lines?} \blindtext \subsection*{What should happen if a title is unnumbered?} \blindtext \end{document}

enter image description here

esdd
  • 85,675
  • Thanks a lot, this looks quite neat. However, the bottom margin of the box and the rule are not in-line. How can I achieve that? – cpt Feb 03 '21 at 08:56
  • I have added two screenshots. The bottom of the box and the bottom of the rule are in-line. If the rule should be below of box, then shift the rule by its line width down: \rule[\dimexpr-\fboxsep-1pt\relax]{\textwidth}{1pt}. – esdd Feb 03 '21 at 09:16
  • \rule[\dimexpr-\fboxsep-1pt\relax]{\textwidth}{1pt} was exactly what I meant. Thanks a lot. This is why stackexchange will be mentioned in my thesis' acknowledgements ^_^ – cpt Feb 03 '21 at 10:10
  • I have updated my answer. If you like an answer, you can upvote it und if it solves your issue, you could accept it. – esdd Feb 03 '21 at 10:49
  • One thing I realized right now was that kong section title don't match the height of the box. Is this solvable? – cpt Feb 04 '21 at 08:12
  • I do not understand what you want to do? Should the height of the box be enlarged to fit the height of the title? This could result in an ugly high dark box. How should the number be aligned vertically (inside the high box)? Maybe you have to ask a new question and show the desired result for longer titles. I think it will be a bit tricky. – esdd Feb 04 '21 at 10:11