# Creating a documentclass with colored titles in koma-script

I am trying to create my own documentclass which fits my needs according to the use of colors.

For a shorter description I put all corresponding things in the following working example:

\documentclass[twoside=true, openright, pdftex, bibliography=totoc, chapterprefix=true, appendixprefix=true, numbers=noenddot, parskip=half, titlepage, index=totoc, headlines=3, footlines=2, headings=normal]{scrbook}
\usepackage{xcolor}

\makeatletter
\renewcommand{\thesection}{\textcolor{red}{Own Section \@arabic\c@section}}
\renewcommand*{\chapapp}{Own Chapter}

\begin{document}
\tableofcontents
\chapter{Chaptertest}
Text 1
\section{Sectiontest}
Text 2
\end{document}


It creates the following chapter

My problem is, as one can see, that the color for the section in the TOC is also changed (but should be still black) and that the spacing is wrong, so that the title is printed over the label.

Has anybody an idea how to fix this? Thank you!

Update:

I used the solution of esdd and added the following support for changing the text before chapter and section using the babel-package. So that it is possible to change it more easily and to add language support.

\usepackage[english]{babel}
\renewcaptionname{english}{\chaptername}{Own Chapter}

\makeatletter

\newcaptionname{english}{\sectionname}{Own Section}
\newcommand*{\secapp}{\@secapp}
\newcommand*\@secapp{\sectionname}


The only thing that is now still missing is a solution for calculating the width of the used text and changing the tocnumwidth depending on the calculated value.

Here is a suggestion redefining \sectionformat. And you can redefine \addchaptertocentry and \addsectocentry to get the desired entries in the table of contents. As already suggested by Johannes you can use \RedeclareSectionCommand or \RedeclareSectionCommands to change the space for the chapter and the section numbers in the table of contents.

\documentclass[
%twoside=true,%default
%openright,%default
%pdftex,%No
bibliography=totoc,
chapterprefix=true,
appendixprefix=true,
numbers=noenddot,
parskip=half,
index=totoc,
footlines=2,
]{scrbook}

\usepackage{xcolor}
\colorlet{prefixcolor}{red}

\newkomafont{sectionprefix}{\color{prefixcolor}}

\renewcommand*\chapapp{Own Chapter}
\newcommand*\secapp{Own Section}

\renewcommand*\sectionformat{%
{\usekomafont{sectionprefix}\secapp~\thesection\autodot\enskip}}

\ifstr{#1}{}{%
}{
}%
}
\ifstr{#1}{}{%
}{%
}%
}
\RedeclareSectionCommands[tocnumwidth=8em]{chapter,section}

\usepackage{blindtext}% for dummy text
\begin{document}
\tableofcontents
\Blindtext
\blinddocument
\blinddocument
\end{document}


• Defining the \secapp command and a corresponding font is cool and works great. I tried to redefine the sectionformat with renewcommand but then only the numbers are shown and "Own Section" is still missing, like I never changed something. Instead using \usekomafont and \secapp when redefining \thesection worked. – baghira Aug 7 '15 at 9:47
• @baghira Then you have an older KOMA-Script Version - maybe 3.15 or 3.16. My code needs at least version 3.17. The current version on CTAN and in TL2015 and MiKTeX 2.9 is 3.18 – esdd Aug 7 '15 at 14:38
• I updated koma-script and now it works. One last question: Is there a way, so that the entry in the Toc is also created when the title is empty like \chapter{}.? – baghira Aug 11 '15 at 12:03
• @baghira KOMA-Script suppresses the Toc entry if the title is empty but you can use something like \chapter[\null]{}. – esdd Aug 11 '15 at 18:06

It is quite easy with a recent version of KOMA-script. You can redefine the appearrance of chapter and section-commands using a key-value-syntax. In your case, you need to add just one line \RedeclareSectionCommand[tocnumwidth=8em]{section}. The value of 8em is chossen by looking at the output. I changed the numbering of sections a bit, just to make it more obvious in the example output below.

\documentclass[twoside=true,
openright,
% pdftex,%No
bibliography=totoc,
chapterprefix=true,
appendixprefix=true,
numbers=noenddot,
parskip=half,
index=totoc,
footlines=2,
\usepackage{xcolor}
\usepackage{blindtext}

\renewcommand{\thesection}{\textcolor{red}{Own Section
\arabic{chapter}--\arabic{section}}}
\renewcommand*{\chapapp}{Own Chapter}
\RedeclareSectionCommand[tocnumwidth=8em]{section}% <- Magic here

\usepackage{biblatex}
\begin{document}
\tableofcontents
\listoffigures
\chapter{Walzing Wombat}
\blindtext
\captionof{figure}{this is just a test caption}
\section{Wobbling Whale}
\blindtext
\chapter{Salsa Squirrel}
\blindtext
\section{Samba Snake}
\blindtext\cite{companion}
\printbibliography

\end{document}

• Using the hook for chapterentry seems indeed to be the most simple solution to input your special string, but i omitted it. The font mechanism is intended for font switches only, i.e. non-printable stuff. As soon as you have an unnumbered chapter, you get into trouble. Or maybe this is the intended output. – Johannes_B Aug 6 '15 at 18:59
• This solution creates a colored entry in the TOC. But I want them only colored in the Text and just black in the TOC. – baghira Aug 10 '15 at 13:34

Command that format (sub)section number in the heading is \@seccntformat Here is what we can do

\documentclass[twoside=true, openright, pdftex, bibliography=totoc, chapterprefix=true, appendixprefix=true, numbers=noenddot, parskip=half, titlepage, index=totoc, headlines=3, footlines=2, headings=normal]{scrbook}
\usepackage{xcolor}
\usepackage{tocstyle}
\settocstylefeature[1]{entryhook}{Own Section }

\makeatletter
\renewcommand*{\chapapp}{Own Chapter}
\def\@seccntformat#1{\@ifundefined{#1@cntformat}%
{\csname #1@cntformat\endcsname}%
}
\makeatother

\begin{document}
\tableofcontents
\chapter{Chaptertest}
Text 1
\section{Sectiontest}
Text 2
\end{document}


\usepackage{tocstyle}