1

I have a problem with spacing my second table of contents to the publishers demands.

They want two tables of contents, one in front showing only chapters (no chapter numbers, easy). The other one in the back showing chapters and sections. This is what the tocstyle package is for. But now it get's tricky, too tricky for me:

1) The box for the section number should always be as wide as the highest section number on that page, with the dots aligned. I. e.

 1.
11.

instead of

1.
11. 

As I have more than 1000 consecutive sections that should apply to four digits as well.

2) The space between section number and section title should always be of a fixed length, currently it is decreasing, so that it is more or less gone when reaching section 1000.

3) But if one creates a dynamic width for the number and the space before the title, the hanging indent (which I cannot access so far) should adapt as well.

My solution so far: As the book is more or less finished I was able to achieve the necessary changes for pagebreaks with fixed values. If the section number is less than 83 it a box for two digits should be created, if it is less than 958 one for three:

\newcommand{\numberbox}[1]{\makebox[2em][l]{\makebox[\ifnumless{#1}{83}{1.2em}{\ifnumless{#1}{958}{1.5em}{1.7em}}][r]{#1.}}}

But I don't know how to adapt the indent. So basically I would be happy if someone could point me to how to change the hanging indent of the second toc. But a more general approach where no hacks with fixed if-values would be preferred.

\documentclass[openany]{scrbook}
\usepackage{blindtext}
\usepackage[tocfullflat]{tocstyle}
\usepackage[english]{babel}

\RedeclareSectionCommand[tocbeforeskip=0pt]{chapter}

\addtotoclist{dok}
\settocfeature[dok][0]{entryhook}{\usekomafont{chapterentry}}

\newcommand{\dokchap}[1]{%
  \addtocontents{dok}{\vspace{\baselineskip}\protect\centering #1\par\vspace*{0.5\baselineskip}\nobreak}
}  
\newcommand{\doksec}[2]{%
   \addcontentsline{dok}{section}{\framebox[2em][l]{\framebox[1.2em][r]{#1.}}\hspace{1em}{#2}}
}

\begin{document}

\small\setcounter{tocdepth}{0}
\tableofcontents

\chapter{1901}\dokchap{1901}
\section{section one}
\doksec{\the\value{section}}{section one}
\blindtext
\section{section two}
\doksec{\the\value{section}}{section one}
\blindtext
\section{section three}
\doksec{\the\value{section}}{section three}
\blindtext
\section{section four}
\doksec{\the\value{section}}{section four}
\blindtext
\section{section five}
\doksec{\the\value{section}}{section five}
\blindtext
\section{section six}
\doksec{\the\value{section}}{section six}
\blindtext
\section{section seven}
\doksec{\the\value{section}}{section seven}
\blindtext
\section{section eight}
\doksec{\the\value{section}}{section eight}
\blindtext
\section{section nine}
\doksec{\the\value{section}}{section nine}
\blindtext
\section{section ten}
\doksec{\the\value{section}}{section ten}
\blindtext
\section{section eleven}
\doksec{\the\value{section}}{section eleven}
\blindtext
\section{\blindtext}
\doksec{\the\value{section}}{\blindtext}

\blindtext

\settocfeature{raggedhook}{\raggedright}
\setcounter{tocdepth}{1}
\addcontentsline{toc}{chapter}{Second table of contents} 
\listoftoc[Second table of contents]{dok}  

\end{document}

enter image description here

1

I am not sure if I understood, what you want to do. You have mentioned, that there are no chapter numbers. So I will change \chapter to \addchap and number the sections without chapter numbers and without reset at new chapters.

The following example uses the possibilities of tocbasic and needs an uptodate KOMA-Script version. Note that tocstyle is not developed anymore. It will never leave the alpha status.

\documentclass[open=any]{scrbook}
\usepackage{blindtext}% only for dummy text
\usepackage{showframe}% to show the page layout
\usepackage[english]{babel}

\RedeclareSectionCommand[
  counterwithout=chapter
]{section}

% Settings for the first ToC
\BeforeStartingTOC[toc]{\value{tocdepth}=\chaptertocdepth}
\DeclareTOCStyleEntry[
  beforeskip=0pt,
  entryformat=\normalfont,
  pagenumberformat=\normalfont,
  linefill=\TOCLineLeaderFill
]{chapter}{chapter}

% Settings for the second ToC
\newcommand*\secondcontentsname{\contentsname}
\providecaptionname{english}{\secondcontentsname}{Second table of contents}
\DeclareNewTOC[listname=\secondcontentsname]{dok}

\BeforeStartingTOC[dok]{%
  \addtocentrydefault{chapter}{}{\secondcontentsname}%
  \value{tocdepth}=\sectiontocdepth%
  \DeclareTOCStyleEntry[
    beforeskip=\baselineskip,
    entryformat=\normalfont\centering,
    pagenumberformat=\nullfont,
    linefill={},
  ]{chapter}{chapter}%
}

\DeclareTOCStyleEntry[
  onstarthigherlevel=\vspace*{0.5\baselineskip}\nobreak,
  indent=0pt,
  entryformat=\def\autodot{.},
  entrynumberformat=\sectionnumberbox,
  numwidth=1.8em,
]{section}{section}
\newcommand*\sectionnumberbox[1]{\hfill #1\hspace{.4em}}

\usepackage{xpatch}
\xapptocmd\addchaptertocentry{%
  \addxcontentsline{dok}{chapter}{#2}% add chapters to the second ToC
}{}{\PatchFailed}
\xapptocmd\addsectiontocentry{%
  \addxcontentsline{dok}{section}[#1]{#2}% add sections to the second ToC
  %
  \ifnum \value{section}=97% adjust numwidth for larger page numbers; must be done manually
    \addtocontents{dok}{\protect\DeclareTOCStyleEntry[numwidth=2.3em]{section}{section}}%
  \fi
  %
}{}{\PatchFailed}

\begin{document}
\tableofcontents

\addchap{1901}
\section{section one}
\blindtext
\section{section two}
\blindtext
\section{section three}
\blindtext
\section{section four}
\blindtext
\section{section five}
\blindtext
\section{section six}
\blindtext
\section{section seven}
\blindtext
\section{section eight}
\blindtext
\section{section nine}
\blindtext
\section{section ten}
\blindtext
\section{section eleven}
\blindtext
\section{\blindtext}
\Blindtext[2]

\setcounter{section}{96}% only for the example
\section{section 97}
\blindtext
\addtocontents{dok}{\protect\clearpage}% only for the example
\section{section 98}
\blindtext
\section{section 99}
\blindtext
\section{section 100}
\blindtext
\section{\blindtext}
\blindtext

\listoftoc{dok}
\end{document}

Result:

enter image description here

  • Now I understand: Basically I enter the first section on a new page in the ´ifnum´-routine to get the proper spacing for the box. That works! Thank you! – Martin Mueller Jan 9 '18 at 16:44

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.