4

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} 

\addtokomafont{chapter}{\color{blue}}
\addtokomafont{chapterprefix}{\color{red}}
\addtokomafont{chapterentry}{Own Chapter~}
\addtokomafont{section}{\color{blue}}

\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

Chapter-Example

and the following table of contents.

Table of Content

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.

3

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,
  headlines=3,
  footlines=2,
  headings=normal
]{scrbook}

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

\addtokomafont{chapter}{\color{headingscolor}}
\addtokomafont{section}{\color{headingscolor}}
\addtokomafont{chapterprefix}{\color{prefixcolor}}
\newkomafont{sectionprefix}{\color{prefixcolor}}

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

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

\renewcommand*\addchaptertocentry[2]{%
  \ifstr{#1}{}{%
    \addtocentrydefault{chapter}{}{#2}%
  }{
    \addtocentrydefault{chapter}{\chapapp~#1\autodot}{#2}%
  }%
}
\renewcommand*\addsectiontocentry[2]{%
  \ifstr{#1}{}{%
    \addtocentrydefault{section}{}{#2}%
  }{%
    \addtocentrydefault{section}{\secapp~#1\autodot}{#2}%
  }%
}
\RedeclareSectionCommands[tocnumwidth=8em]{chapter,section}

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

enter image description here

enter image description here

| improve this answer | |
  • 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
2

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.

baghiraKomaSectionEntriesToc

\documentclass[twoside=true,
 openright,
% pdftex,%No
 bibliography=totoc,
 chapterprefix=true,
 appendixprefix=true,
 numbers=noenddot,
 parskip=half,
 index=totoc,
 headlines=3,
 footlines=2,
 listof=totoc,%<- added for demonstation
 headings=normal]{scrbook}
\usepackage{xcolor} 
\usepackage{blindtext} 

\addtokomafont{chapter}{\color{blue}}
\addtokomafont{chapterprefix}{\color{red}}
\addtokomafont{section}{\color{blue}}

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

\usepackage{biblatex}
\addbibresource{biblatex-examples.bib}
\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}
| improve this answer | |
  • 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
1

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 }

\addtokomafont{chapter}{\color{blue}}
\addtokomafont{chapterprefix}{\color{red}}
\addtokomafont{chapterentry}{Own Chapter~}
\addtokomafont{section}{\color{blue}}

\makeatletter
\renewcommand*{\chapapp}{Own Chapter}
\def\@seccntformat#1{\@ifundefined{#1@cntformat}%
{\csname the#1\endcsname\quad}%
{\csname #1@cntformat\endcsname}%
}
\def\section@cntformat{\textcolor{red}{Own Section \thesection}\quad}
\makeatother

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

Update commands add

\usepackage{tocstyle}
\settocstylefeature[1]{entryhook}{Own Section }

enter image description here

| improve this answer | |
  • This result removed the text "Own Section" completely. I still want to have it in the table of contents. So where should this be added? – baghira Aug 6 '15 at 14:00
  • Warning: This will destroy several of documented KOMA features. – Johannes_B Aug 6 '15 at 17:55
  • @baghira answer updated (see Johannes's comment) – touhami Aug 6 '15 at 18:48
  • Using the tocstyle package is a great option. But it wrote a warning, that it is an alpha-version and removed the formatting (boldface for the chapter) in the TOC. Maybe I need to get an update. – baghira Aug 7 '15 at 10:00
  • @baghira for now I don't recommande my answer, just do what johannes said. – touhami Aug 7 '15 at 11:25

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.