Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set spacing of ToC entries for custom "sections"

Tags:

latex

based on this post I was able to customize the appearence of my table of contents. I am very pleased with the result exept for one small detail: The spacing between the ToC-entries.

The table of content as it is with the (new) example code. The distance indicated by the red arrow should be reduced, while not touching the other distances (i.e., blue boxes).

I would like the entries to be closer together. A solution for standard chapters/parts etc. is something like \setlength{\cftbeforeXskip} (probably more like \cftafterXskip, to keep the spacing to previous entries?), but I don't quite understand how to apply this command to the new sectionstyle (chapterstar).

Thank you for your help!

MWE:

\documentclass[12pt,a4paper,ngerman]{scrbook}

\usepackage[titles]{tocloft}
\usepackage{etoolbox}
\makeatletter
\let\l@chapterstar\l@chapter
% \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
\patchcmd{\l@chapterstar}{\cftchapfont}{\cftchapstarfont}{}{}% Insert starred chapter font
\patchcmd{\l@chapterstar}{#2}{\cftchapstarpagefont #2}{}{}% Insert starred chapter page number font

\makeatother
\newcommand{\cftchapstarfont}{\cftchapfont\normalsize}
\newcommand{\cftchapstarpagefont}{\cftsecpagefont\normalsize}

%%%%%%%%%%%%%%%%%%%%% START BODY
\begin{document}
\tableofcontents

\chapter{Full Chapter 1}
\section{Section 1}
\section{Section 2}
\chapter{Full Chapter 2}

\addcontentsline{toc}{chapterstar}{StarChap1}
\chapter*{StarChap1}

\addcontentsline{toc}{chapterstar}{StarChap2}
\chapter*{StarChap2}

\end{document}

Edit: Thank you for improving the post, I thought I was posting within the specific Latex Stack-exchange.

Edit 2: I have updated the code (and image) to be a little more complex. As I have normal parts, chapters and subsections in my original document, but I just want to decrese the distance for the chapterstar entries, while keeping the other distances (such as the blue boxes) unchanged.

like image 283
Pettersson Avatar asked Oct 18 '25 10:10

Pettersson


1 Answers

\setlength{\cftbeforechapskip}{.1ex} seems to also work for your custom chapters:

\documentclass[12pt,a4paper,ngerman]{scrbook}

\usepackage[titles]{tocloft}
\usepackage{etoolbox}
\makeatletter
\let\l@chapterstar\l@chapter
% \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
\patchcmd{\l@chapterstar}{\cftchapfont}{\cftchapstarfont}{}{}% Insert starred chapter font
\patchcmd{\l@chapterstar}{#2}{\cftchapstarpagefont #2}{}{}% Insert starred chapter page number font

\apptocmd{\l@chapterstar}{\setlength{\cftbeforechapskip}{.1ex}}{}{}

\makeatother
\newcommand{\cftchapstarfont}{\cftchapfont\normalsize}
\newcommand{\cftchapstarpagefont}{\cftsecpagefont\normalsize}

%%%%%%%%%%%%%%%%%%%%% START BODY
\begin{document}
\tableofcontents

\chapter{Full Chapter 1}
\section{Section 1}
\section{Section 2}
\chapter{Full Chapter 2}

\addcontentsline{toc}{chapterstar}{StarChap1}
\chapter*{StarChap1}

\addcontentsline{toc}{chapterstar}{StarChap2}
\chapter*{StarChap2}

\end{document}

enter image description here

like image 54
samcarter_is_at_topanswers.xyz Avatar answered Oct 21 '25 10:10

samcarter_is_at_topanswers.xyz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!