4

I wonder if you could offer some help. I want to customise the section titles in a scrbook class document. Something similar to this: enter image description here

Section number in a box followed by a centred line Section name and another vertically centred line filling the text width.

So far, I have searched for solutions in this StackExchange but with no success. I have checked the Koma-Script manual and found two possible commands to do something similar: \sectionformat and \sectionlinesformat. But honestly, my very limited knowledge of latex does not allow me to understand when to use one or the other.

Looking at similar solutions found in this forum, I manage to have this:

\documentclass[openright]{scrbook}
\usepackage{geometry}
\geometry{a4paper,twoside, margin=3cm}
\usepackage{blindtext}% only dummy text
\usepackage{xcolor}

\renewcommand*{\sectionformat}{%
    \usekomafont{section}
    \makebox[0pt][r]{\fboxrule=1.5pt\fcolorbox{black}{white!0}{\color{black}\thesection}\rule[.7ex]{10pt}{1.5pt}}
 }

\begin{document}
    \chapter{Introduction}
    \section{Section 1}
    \Blindtext[2]
    \section{Section 2}
    \Blindtext[2]
    \section{Section 3}
    \Blindtext[2]
    \section{Section 4}
    \Blindtext[2]
\end{document}

from which I get: enter image description here

But I have not found a way to add the horizontal-vertical text height aligned line to fill the text width.

Another option I tried was using:

%%%%% Simple + underline
\renewcommand{\sectionlinesformat}[4]{%
\Ifstr{#1}{section}{%
    \parbox[c]{\linewidth}{%
      \raggedsection{\hskip #2{\color{black}#3}}{#4}\par%
      \kern-.75\ht\strutbox\rule{\linewidth}{1pt}%
    }%
  }{%
    {\hskip #2#3}{#4}}% 
}

See, here \sectionlinesformat is used instead of \sectionformat, but no idea why. For which I get:

enter image description here

A third option I found was using \makeatletter and \makeatother:

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

And I get:

enter image description here

Any guidance on how can I get the desired result?

Also, It would be nice if we can have the option to have the section number and its box aligned with the text or on the margin.

1
  • Should subsections and subsubsections use the same format and color as sections? Are there section headings that require two or more lines? How should they be formatted?
    – esdd
    Commented May 13, 2022 at 10:48

2 Answers 2

4

Something like this?

\documentclass[openright]{scrbook}
\usepackage{geometry}
\geometry{a4paper,twoside, margin=3cm}
\usepackage{blindtext}% only dummy text
\usepackage{xcolor}
\usepackage{xhfill}

\renewcommand*{\sectionformat}{%
    \usekomafont{section}%
    \fboxrule=1pt\fcolorbox{blue}{white!0}{\color{black}\thesection}%
    \color{blue}\rule[.7ex]{10pt}{1pt}\>%
}
\makeatletter
\renewcommand*{\sectionlinesformat}[4]{%
    \@hangfrom{\hskip #2#3}{\color{blue}#4}\>%
    \xrfill[.7ex]{1pt}[blue]%
}
\makeatother

\begin{document}
    \chapter{Introduction}
    \section{Section 1}
    \Blindtext[2]
    \section{Section 2}
    \Blindtext[2]
    \section{Section 3}
    \Blindtext[2]
    \section{Section 4}
    \Blindtext[2]
\end{document}
4
  • It definitely solves the problem. I couple of questions if you allow me: 1. I ran the code as you wrote, but It did not work in my code, I had to put the \> in math mode --> $\>$. 2. I have read that people discourage using makeatletter/makeatother (not clear for me why anyway). In your solution, I think you use it because it requires the use of \@hangfrom. what @\hangfrom command does? 3. I see \sectionlinesformat command requires 4 entries, from them I found out #4 is the section name, but I could not figure out what are #1, #2 and #3, do you have an idea that these represent? Commented May 12, 2022 at 7:46
  • @JuanGuerrero-Fernández 1. \> is just a space so it shouldn't change anything. You could also change it to another space if you prefer.
    – Vincent
    Commented May 12, 2022 at 13:24
  • 2. and 3. Indeed, \makeatletter and \makeatother are required here because of \@hangfrom. To understand what these two commands do, see this question. For \@hangfrom, in fact I don't know exactly what it does, I just know that it's basically the default version of \sectionlinesformat (for details about that command and its arguments, see page 498 of the documentation of scrbook). To redefine \sectionlinesformat, I just copied the default version and added the line on the right.
    – Vincent
    Commented May 12, 2022 at 13:25
  • \@hangfrom is for the alignment of headings with two or more text lines. Note that \sectionlinesformat is also used for other free-standing headings subsection and subsubsection (except chapter and part). So they will have also blue text followed by a blue line.
    – esdd
    Commented May 13, 2022 at 13:49
2

Only as addition to the answer of @Vincent:

If only sections (and not subsections and subsubsections) should use the different format:

\documentclass[twoside,open=right]{scrbook}
\usepackage{geometry}
\geometry{margin=3cm}
\usepackage{blindtext}% only for dummy text
\usepackage{xcolor}
\usepackage{xhfill}

\renewcommand*{\sectionformat}{%
  \fboxrule=1.5pt\fcolorbox{.}{white}{\color{black}\thesection}%
  \rule[.7ex]{10pt}{1pt}\;%
}

\NewCommandCopy{\originalsectionlinesformat}{\sectionlinesformat}
\renewcommand*{\sectionlinesformat}[4]{%
  \originalsectionlinesformat{#1}{#2}{#3}{#4\Ifstr{#1}{section}{\;\xrfill[.6ex]{1.5pt}[.]}{}}%
}

\addtokomafont{section}{\color{blue!50!black}}

\begin{document}
\chapter{Introduction}
\section{Section 1}
\subsection{Subsection}
\subsubsection{Subsubsection}
\Blindtext[2]
\end{document}

enter image description here

If subsections and subsubsections should use the same format but different colors:

\documentclass[twoside,open=right]{scrbook}
\usepackage{geometry}
\geometry{margin=3cm}
\usepackage{blindtext}% only for dummy text
\usepackage{xcolor}
\usepackage{xhfill}

\renewcommand*{\sectionformat}{%
  \fboxrule=1.5pt\fcolorbox{.}{white}{\normalcolor\thesection}%
  \rule[.7ex]{10pt}{1pt}\;%
}
\renewcommand*{\subsectionformat}{%
  \fboxrule=1.5pt\fcolorbox{.}{white}{\normalcolor\thesubsection}%
  \rule[.7ex]{10pt}{1pt}\;%
}
\renewcommand*{\subsectionformat}{%
  \fboxrule=1.5pt\fcolorbox{.}{white}{\normalcolor\thesubsubsection}%
  \rule[.7ex]{10pt}{1pt}\;%
}

\NewCommandCopy{\originalsectionlinesformat}{\sectionlinesformat}
\renewcommand*{\sectionlinesformat}[4]{%
  \originalsectionlinesformat{#1}{#2}{#3}{#4\;\xrfill[.6ex]{1.5pt}[.]}%
}

\addtokomafont{section}{\color{blue!50!black}}
\addtokomafont{subsection}{\color{green!50!black}}
\addtokomafont{subsubsection}{\color{red!50!black}}

\begin{document}
\chapter{Introduction}
\section{Section 1}
\subsection{Subsection}
\subsubsection{Subsubsection}
\Blindtext[2]
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .