3

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 ?

enter image description here

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
4

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
\addtokomafont{section}{\let\@hangfrom\relax}
\addtokomafont{subsection}{\let\@hangfrom\relax}
\makeatother

enter image description here

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

\renewcommand\addsectiontocentry[2]{%
  \ifstr{#1}{}{%
    \addtocentrydefault{section}{}{#2}}{%
    \addtocentrydefault{section}{Sect.\,#1:}{#2}}}
\renewcommand\addsubsectiontocentry[2]{%
  \ifstr{#1}{}{%
    \addtocentrydefault{subsection}{}{#2}}{%
    \addtocentrydefault{subsection}{Subs.\,#1:}{#2}}}

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}

enter image description here

Code:

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

\makeatletter
\addtokomafont{section}{\let\@hangfrom\relax}
\addtokomafont{subsection}{\let\@hangfrom\relax}
\makeatother

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

\renewcommand\addsectiontocentry[2]{%
  \ifstr{#1}{}{%
    \addtocentrydefault{section}{}{#2}}{%
    \addtocentrydefault{section}{Sect.\,#1:}{#2}}}
\renewcommand\addsubsectiontocentry[2]{%
  \ifstr{#1}{}{%
    \addtocentrydefault{subsection}{}{#2}}{%
    \addtocentrydefault{subsection}{Subs.\,#1:}{#2}}}

\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
1

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

\documentclass{scrbook}
\addtokomafont{disposition}{\itshape}
\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}

gaspaletToc

gaspaletDoc

  • 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
  • 1
    @Gaspalet \let\raggedchapter\raggedright – Johannes_B Jan 25 '15 at 20:11

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.