I'm using the KOMA class scrbook, and trying to add some text before any section and subsection title. Like that :

\section{Hello !} must display Section 1 : Hello ! (1 is the normal counter). The same with "Subsection # :" before any section title.

So I tried the following code :

\renewcommand{\thesection}{Section \@arabic\c@section}


The snag is that I want to have those section/subsection title centered, so I added the parameter \centering when defining my headings font :

\addtokomafont{disposition}{\HeadingFont\centering}


But with this code, only the very name of the section is centered, but not the prefix "section 1". It is a problem when I have long titles, the page lacks balance. Can I try with \othersectionlevelsformat, like indicated in the manual, or have you another solution ?

As a bonus, have you a quick way to fix the table of contents, because the dots are encroaching the added text. I manually tried something with :

\renewcommand\l@section{\@dottedtocline{1}{1.5em}{4.2em}}

• You are asking quite a lot here, please split that to different questions, each one with a minimal working example to make the question understandable and the problem reproducable. – Johannes_B Jan 25 '15 at 18:42
• \let\raggedsectin\centering – Johannes_B Jan 25 '15 at 18:43

Update regarding KOMA-Script version 3.28:

Redefine \raggedsection and \sectionlinesformat to center the section titles including the numbers:

\documentclass[numbers=noenddot]{scrbook}
%\providecommand*\Ifstr{\ifstr}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif
\usepackage{blindtext}
\let\raggedsection\centering
\let\raggedchapter\raggedright

\renewcommand\sectionformat{Section~\thesection:\enskip}
\renewcommand\subsectionformat{Subsection~\thesubsection:\enskip}
\renewcommand\subsubsectionformat{Subsubsection~\thesubsection:\enskip}

\renewcommand*\sectionlinesformat[4]{#3#4}

\DeclareTOCStyleEntry[
dynnumwidth,
entrynumberformat=\entrywithprefix{Sect.}
]{default}{section}
\DeclareTOCStyleEntry[
dynnumwidth,
entrynumberformat=\entrywithprefix{Subs.}
]{default}{subsection}
\DeclareTOCStyleEntry[
dynnumwidth,
entrynumberformat=\entrywithprefix{Subsubs.}
]{default}{subsubsection}

\newcommand*\entrywithprefix[2]{%
\def\autodot{}%
\mbox{#1\,#2:}%
}

\begin{document}
\tableofcontents
\chapter{First chapter}
\blindtext
\section{First section}
\blindtext
\subsection{a rabbit subsection with quite a few words to make it
extra long not only because ducks are so cute}
\Blindtext
\blinddocument
\end{document}



You could redefine \raggedsection, \raggedchapter and \othersectionlevelsformat (for version 3.17 or newer see the update below):

\let\raggedsection\centering
\let\raggedchapter\raggedright

\renewcommand\othersectionlevelsformat[3]{%
\ifstr{#1}{section}{Section~#3:\enskip}{%
\ifstr{#1}{subsection}{Subsection~#3:\enskip}{%
#3\autodot\enskip}}}


To centr the section and subsection titles including the numbers use

\makeatletter
\makeatother


If the entries in the table of contents should also be changed redefine \addsectiontocentry and \addsubsectiontocentry additionally:

\renewcommand\addsectiontocentry[2]{%
\ifstr{#1}{}{%
\ifstr{#1}{}{%


To get more space for the numbers you could enlarge the tocnumwidth for section and subsection (needs the up to date KOMA-Script version 3.15)

\RedeclareSectionCommand[tocnumwidth=5em]{section}
\RedeclareSectionCommand[tocnumwidth=5.5em]{subsection}


Code:

\documentclass[numbers=noenddot]{scrbook}
\usepackage{blindtext}
\let\raggedsection\centering
\let\raggedchapter\raggedright

\makeatletter
\makeatother

\renewcommand\othersectionlevelsformat[3]{%
\ifstr{#1}{section}{Section~#3:\enskip}{%
\ifstr{#1}{subsection}{Subsection~#3:\enskip}{%
#3\autodot\enskip}}}

\ifstr{#1}{}{%
\ifstr{#1}{}{%

\RedeclareSectionCommand[tocnumwidth=5em]{section}
\RedeclareSectionCommand[tocnumwidth=5.5em]{subsection}

\begin{document}
\tableofcontents

\chapter{First chapter}
\blindtext
\section{First section}
\blindtext
\subsection{a rabbit subsection with quite a few words to make it
extra long not only because ducks are so cute}
\Blindtext
\blinddocument
\end{document}


Update regarding \othersectionlevelsformat and KOMA-Script Version 3.17 or newer

Note that KOMA-Script Version 3.17 uses \sectionformat, \subsectionformat etc. instead \othersectionlevelsformat. So you have to redefine this commands instead of \othersectionlevelsformat

\renewcommand\sectionformat{Section~\thesection:\enskip}
\renewcommand\subsectionformat{Subsection~\thesubsection:\enskip}


If the code should work with both the older versions (3.15 and 3.16) and version 3.17 or newer you can use

\renewcommand\othersectionlevelsformat[3]{%
\ifstr{#1}{section}{Section~#3:\enskip}{%
\ifstr{#1}{subsection}{Subsection~#3:\enskip}{%
#3\autodot\enskip}}}

\providecommand*\sectionformat}{}
\providecommand*{\subsectionformat}{}
\renewcommand\sectionformat{Section~\thesection:\enskip}
\renewcommand\subsectionformat{Subsection~\thesubsection:\enskip}

• @gaspalet I would prefer this solution. – Johannes_B Jan 25 '15 at 20:05
• Great work ! I added \let\raggedchapter\raggedright not to touch the behavior of the chapter. Yet my main problem remains : I added a picture in my first post to show what I mean. I want the hole section title to be centered, not just what is entered in \section{}. Any idea ? – Gaspalet Jan 25 '15 at 20:33
• @Gaspalet I have updated my answer. – esdd Jan 25 '15 at 21:05
• Thanks a thousand times, that's perfect ! An jeweler's work ;) Thanks too @Johannes_B ! – Gaspalet Jan 25 '15 at 21:57

I have absolutely no idea what you want to achieve, but this should give you a good start:

\documentclass{scrbook}
\let\raggedsection\centering
\makeatletter
\renewcommand{\thesection}{Section \@arabic\c@section}
\renewcommand{\thesubsection}{Subsection \@arabic\c@subsection}
\makeatother
\usepackage{tocstyle}
\usetocstyle{KOMAlike}
\begin{document}
\tableofcontents
\chapter{gaspalet}
\section{a duck section}
\label{duck}
\subsection{a rabbit subsection with quite a few words to make it
extra long not only because ducks are so cute}
See \ref{duck}

\end{document}


• Great, that's close to what I was looking after ! I still have two problems. At first, I don't want chapter titles to be centered. The second issue is more tricky, and it was why I looked for help : in the solution you gave, only the words "a rabbit subsection...so cute" are centered, "Subsection 1" is kinda put aside on the left. I want the hole to be centered. I added a photo in my first post to show what I mean :) – Gaspalet Jan 25 '15 at 20:06
• @Gaspalet \let\raggedchapter\raggedright – Johannes_B Jan 25 '15 at 20:11