Text FormattingTOC entries and formatting in scrbook

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
ck2018
Posts: 11
Joined: Wed Oct 17, 2018 12:21 pm

TOC entries and formatting in scrbook

Post by ck2018 »

My main problem is getting the table of contents (TOC) to look right in scrbook. I can change the font of the chapter-entries, but not the section-entries. The work is divided into four major "divisions", in which there are chapters. I've altered scrbook's chapters to provide my "divisions", and altered the sections to provide the chapters.

The chapter-entry font is resized correctly in the TOC, but the section-entries are in too big a font. You would think that a call to \addtokomafont{sectionentry} would be the answer, but it doesn't work in scrbook, and I want this to be a book. Perhaps it's possible to make scrartcl act like a book, but I don't know how to do it.

MWE (I've included the calls to \RedeclareSectionCommand in case they affect the other aspects of the document.):

Code: Select all

% !TEX TS-program = XeLaTeX

\documentclass[12pt,twoside,openany,cleardoublepage=plain]{scrbook}

\usepackage[
    paperwidth=6in,
    paperheight=9in,
    inner=0.6in,
    outer=0.6in,
    bindingoffset=0.15in,
    top=0.5in,
    bottom=0.5in,
    headsep=0.1in,
    footskip=0in,
    %showframe,
    twoside]{geometry}

\pagestyle{empty}

\usepackage{fontspec}
\setmainfont{Junicode}

\usepackage[automark]{scrlayer-scrpage}

\addtokomafont{disposition}{\normalfont\small\rmfamily}
\BeforeStartingTOC[toc]{\addtokomafont{disposition}{\small\rmfamily}}
\addtokomafont{chapter}{\normalfont\normalsize\rmfamily\bfseries}
\addtokomafont{section}{\normalfont\normalsize\rmfamily\bfseries}


\clearpairofpagestyles
\ihead*{\pagemark}

\RedeclareSectionCommand[beforeskip=0pt,afterskip=\baselineskip,innerskip=0pt,afterindent=false]{chapter}

\RedeclareSectionCommand[beforeskip=\baselineskip,afterskip=\baselineskip,afterindent=false]{section}

\RedeclareSectionCommand[beforeskip=\baselineskip,afterskip=\baselineskip,afterindent=false]{subsection}


\def\thechapter{\Roman{chapter}}

\def\thesection{\Roman{section}}


\begin{document}

\tableofcontents
\newpage

\addchap{bla1}

some writing
more writing
more writing
more writing
more writing
more writing
more writing
more writing
more writing
more writing

\section*{this shouldn't show up in the TOC}

\addchap{somechap1}

some more writing
some more writing
some more writing
some more writing
some more writing
some more writing
some more writing
some more writing
some more writing

\section{some section1}

foo

some more writing
some more writing
some more writing
some more writing
some more writing

\section{A REALLY REALLY REALLY REALLY LONG SECTION HEADING}

bar

\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

TOC entries and formatting in scrbook

Post by Ijon Tichy »

You can change the format of the section entries using either \DeclareTOCStyleEntry or simply prefix the \DeclareTOCStyleEntry attribute entryformat with toc to build the \RedeclareSectionCommand attribute tocentryformat. In the following example, I've also simplified some other font settings:
\documentclass[12pt,twoside,openany,cleardoublepage=plain]{scrbook}

\usepackage[
    paperwidth=6in,
    paperheight=9in,
    inner=0.6in,
    outer=0.6in,
    bindingoffset=0.15in,
    top=0.5in,
    bottom=0.5in,
    headsep=0.1in,
    footskip=0in,
    %showframe,
    twoside]{geometry}

\pagestyle{empty}

\usepackage{fontspec}
\setmainfont{Junicode}

\usepackage[automark]{scrlayer-scrpage}

\setkomafont{disposition}{\normalfont\bfseries\small}% \normalfont usually
% sets to \rmfamily
\setkomafont{chapter}{}% Don't overwrite the font setting (i.e. size) of disposition.
\setkomafont{section}{}% Don't overwrite the font setting (i.e. size) of disposition.
\addtokomafont{chapterentry}{\mdseries}% Don't make chapter entries bold.
% Alternative: \setkomafont{chapterentry}{\small}% Make chapter entries small.
\newcommand*{\smalltocentry}[1]{\small #1}% To setup the small ToC entries
                                % (see \RedeclareSectionCommand[…]{section})

\clearpairofpagestyles
\ihead*{\pagemark}

\RedeclareSectionCommand[beforeskip=0pt,afterskip=\baselineskip,innerskip=0pt,afterindent=false]{chapter}

\RedeclareSectionCommand[beforeskip=\baselineskip,afterskip=\baselineskip,afterindent=false,tocentryformat=\smalltocentry]{section}

\RedeclareSectionCommand[beforeskip=\baselineskip,afterskip=\baselineskip,afterindent=false]{subsection}


\def\thechapter{\Roman{chapter}}

\def\thesection{\Roman{section}}


\begin{document}

\tableofcontents
%\newpage Useless

\addchap{bla1}

some writing
more writing
more writing
more writing
more writing
more writing
more writing
more writing
more writing
more writing

\section*{this shouldn't show up in the TOC}

\addchap{somechap1}

some more writing
some more writing
some more writing
some more writing
some more writing
some more writing
some more writing
some more writing
some more writing

\section{some section1}

foo

some more writing
some more writing
some more writing
some more writing
some more writing

\section{A REALLY REALLY REALLY REALLY LONG SECTION HEADING}

bar

\addchap{One more chapter}

\section{ALSO A SECTION}

\end{document}
I've also added one more section to illustrate, that your changes may result in duplicates for the section numbers. Maybe it would be a better idea to change the style of [t]\chapter[/t] and [t]\part[/t] and use [t]\part[/t] for the divisions. But I cannot decide this, because I do not know enough about the wanted result.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
ck2018
Posts: 11
Joined: Wed Oct 17, 2018 12:21 pm

TOC entries and formatting in scrbook

Post by ck2018 »

Thank you very much for your help! :D The TOC is now looking much better, but the page-numbers for the section entries are still noticeably bigger than those for the chapters. Is there a way to change this?
Ijon Tichy wrote: I've also added one more section to illustrate, that your changes may result in duplicates for the section numbers.
Yes, that's exactly what I want. The divisions could theoretically be published separately, so the section numbers should start from scratch for each one.
Maybe it would be a better idea to change the style of [t]\chapter[/t] and [t]\part[/t] and use [t]\part[/t] for the divisions. But I cannot decide this, because I do not know enough about the wanted result.
You might be right. I have changed the MWE (below) and added notes to help you to get some idea of the desired result when compiled.
  • Divisions without sections need a number in the TOC.
  • Divisions with sections don't really a need a number in the TOC (although this not a big deal). The division "name" is present in the TOC almost like a heading for the sections listed beneath it.
  • You can see that the divisions do not need numbers, but they need to go in the TOC, so \addchap is perfect for this.
  • Sections need to be numbered in the TOC and indented slightly, which they are, so \section works well for this.
I have added numbers=noenddot to get rid of the dots before the numbers in the TOC, but now I need the numbers to be right aligned and to have an extra white space between the numbers and the entries (because some of the numbers go up high, e.g., XXVIII). Actually, it would be good to get the dot after the numbers back for the sections in the body text (not the TOC).

Code: Select all

\documentclass[numbers=noenddot,12pt,twoside,openany,cleardoublepage=plain]{scrbook}
 
\usepackage[
    paperwidth=6in,
    paperheight=9in,
    inner=0.6in,
    outer=0.6in,
    bindingoffset=0.15in,
    top=0.5in,
    bottom=0.5in,
    headsep=0.1in,
    footskip=0in,
    %showframe,
    twoside]{geometry}
 
\pagestyle{empty}
 
\usepackage{fontspec}
\setmainfont{Junicode}
 
\usepackage[automark]{scrlayer-scrpage}
 
\setkomafont{disposition}{\normalfont\bfseries\small}% \normalfont usually
% sets to \rmfamily
\setkomafont{chapter}{}% Don't overwrite the font setting (i.e. size) of disposition.
\setkomafont{section}{}% Don't overwrite the font setting (i.e. size) of disposition.
\addtokomafont{chapterentry}{\mdseries}% Don't make chapter entries bold.
% Alternative: \setkomafont{chapterentry}{\small}% Make chapter entries small.
\newcommand*{\smalltocentry}[1]{\small #1}% To setup the small ToC entries
                                % (see \RedeclareSectionCommand[…]{section})
 
\clearpairofpagestyles
\ihead*{\pagemark}
 
\RedeclareSectionCommand[beforeskip=0pt,afterskip=\baselineskip,innerskip=0pt,afterindent=false]{chapter}
 
\RedeclareSectionCommand[beforeskip=\baselineskip,afterskip=\baselineskip,afterindent=false,tocentryformat=\smalltocentry]{section}
 
\RedeclareSectionCommand[beforeskip=\baselineskip,afterskip=\baselineskip,afterindent=false]{subsection}
 
 
\def\thechapter{\Roman{chapter}}
 
\def\thesection{\Roman{section}}
 
 
\begin{document}
 
\tableofcontents
%\newpage Useless
 
\addchap{First Division: I have no subdivisions, so I do need the number here in the TOC -->}
 
some writing
more writing
more writing
more writing
more writing
more writing
more writing
more writing
more writing
more writing
 
\section*{this shouldn't show up in the TOC}
 
\addchap{Second Division: I have subdivisions, so I don't really need the number here in the TOC -->}
 
some more writing
some more writing
some more writing
some more writing
some more writing
some more writing
some more writing
some more writing
some more writing
 
\section{some section1}
 
foo
 
some more writing
some more writing
some more writing
some more writing
some more writing
 
\section{A REALLY REALLY REALLY REALLY LONG SECTION HEADING}

\section{<-- Numbers should be right-aligned.}

bar
 
\addchap{Third Division}
 
\section{ALSO A SECTION, ONE THAT IS HAPPY THAT ITS NUMBERS HAVE RESTARTED FROM SCRATCH}
 
\end{document}
Last edited by ck2018 on Fri Oct 11, 2019 11:01 pm, edited 1 time in total.
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

TOC entries and formatting in scrbook

Post by Ijon Tichy »

ck2018 wrote:Thank you very much for your help! :D The TOC is now looking much better, but the numbers for the section entries are still noticeably bigger than those for the chapters.
Your example does not have any chapter number in the table of contents. And the numbers of the section entries in my example have the same size as the text of the section entries:

Image
ck2018 wrote:I have added numbers=noenddot to get rid of the dots before the numbers in the TOC, but now I need the numbers to be right aligned and to have an extra white space between the numbers and the entries (because some of the numbers go up high, e.g., XXVIII). Actually, it would be good to get the dot after the numbers back for the sections in the body text (not the TOC).
Actually it is too late to try to understand all this. But please have a look into the description of \DeclareTOCEntryStyle and all the options for style tocline. All these options also can be uses with \RedeclareSectionCommand, if you prefix them with toc as I've already told.
Attachments
the table of contents
the table of contents
test_20191011_223042.png (38.91 KiB) Viewed 7181 times
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
ck2018
Posts: 11
Joined: Wed Oct 17, 2018 12:21 pm

TOC entries and formatting in scrbook

Post by ck2018 »

Ijon Tichy wrote:Your example does not have any chapter number in the table of contents.
Sorry. I meant the page-numbers. But maybe it's an optical illusion.

It is indeed late. I'll have a look at the entries that you mentioned, and try to work it out. Thanks again!
Post Reply