1

I'm making a book with the koma-script (scrbook) class and I need to add a minitoc at the start of every part and chapter. I found a solution with this post, however it doesn't work with the greek language from babel.

Here is a minimal example:

\documentclass[fontsize=10pt,open=any,twocolumn]{scrbook}[2019/10/12]% needs version 3.27 or newer

\usepackage[greek]{babel}

\providecommand*{\Ifstr}{\ifstr}% added because of changes in upcoming KOMA-Script version 3.28 and \ifstr replaced by \Ifstr in the code (see https://komascript.de/faq_deprecatedif) 
\usepackage{blindtext}
\usepackage{xcolor}

\usepackage{scrwfile}% avoid trouble with the limited number of write handles
\usepackage{xpatch}

\colorlet{partcolor}{blue}
\addtokomafont{partprefix}{\color{partcolor}}% change color of part prefix
\addtokomafont{part}{\normalcolor}% use normal color for part title
%\renewcommand*\partformat{\partname~\thepart}% if you want to remove \autodot from the definition
\RedeclareSectionCommand[
  tocdynnumwidth=true,%
  tocbeforeskip=1em,
  tocindent=1.5em
]{part}
\RedeclareSectionCommand[
  beforeskip=0pt,
  afterindent=false,
  afterskip=2\baselineskip,
  tocdynnumwidth,
  tocbeforeskip=1em plus 1pt minus 1pt,% add some glue
  toclinefill=\TOCLineLeaderFill
]{chapter}
\RedeclareSectionCommand[
  beforeskip=\baselineskip,
  afterindent=false,
  afterskip=.5\baselineskip,
  tocindent=1.5em,
  tocnumwidth=3.5em
]{section}
\RedeclareSectionCommand[
  beforeskip=.75\baselineskip,
  afterindent=false,
  afterskip=.5\baselineskip,
  tocindent=3.8em,
  tocnumwidth=4em
]{subsection}
\RedeclareSectionCommand[
  beforeskip=.5\baselineskip,
  afterindent=false,
  afterskip=.25\baselineskip,
  tocindent=7em,
  tocnumwidth=4.1em
]{subsubsection}
\RedeclareSectionCommand[
  tocindent=10em,
  tocnumwidth=5em
]{paragraph}
\RedeclareSectionCommand[
  tocindent=12em,
  tocnumwidth=6em
]{subparagraph}

\RedeclareSectionCommands
  [tocpagenumberwidth=3em]
  {part,chapter,section,subsection,paragraph,subparagraph}

% https://tex.stackexchange.com/a/359758
% https://tex.stackexchange.com/a/502077
\makeatletter
\newif\ifuseparttoc
\newcommand*{\parttoc}[1][\thepart]{% new command to generate and show a chapter toc
  \useparttoctrue% switch on part-toc-entries
  \edef\ext@parttoc{tcp#1}% extension of the part-toc-file, e.g., tcpI
  \DeclareNewTOC[
    listname=Obsah,
    %unset=onecolumn% if the part toc should use twocolumn
  ]{\ext@parttoc}% declare a new toc file
  \begingroup
    \value{tocdepth}=\chaptertocdepth% we want entries down to chapter
    \listoftoc{\ext@parttoc}% show the toc with header
  \endgroup
}
\xapptocmd\addtocentrydefault{% patch the KOMA-Script's generic toc entry generator
  \ifuseparttoc% if part toc entries should be generated
    \expandafter\tocbasic@addxcontentsline\expandafter{\ext@parttoc}{#1}{#2}{#3}% do it
  \fi
}{}{}
\xpretocmd\part{\useparttocfalse}{}{}% entries in part toc are automatically switched off at start of \part
\newif\ifusechaptertoc% Switch to tell \addtocentrydefault to not only make entries to the toc-file but also to the current section-toc-file
\newcommand*{\chaptertoc}[2][\thechapter]{% new command to generate and show a chapter toc
  \usechaptertoctrue% switch on chapter-toc-entries
  \edef\ext@chaptoc{tcc#1}% extension of the chapter-toc-file, e.g., tcc1
  \DeclareNewTOC{\ext@chaptoc}% declare a new toc file
  %\addsec*{Contents}% header of the chaptertoc
  \setchapterpreamble{%
    \begin{minipage}{\linewidth}
      \hrulefill\par
      \value{tocdepth}=\subsectiontocdepth% we want entries down to subsection
      \listoftoc*{\ext@chaptoc}% show the toc without header
    \end{minipage}%
    \par\bigskip\nobreak\noindent\hrulefill\par
    \bigskip\noindent\ignorespaces% add some vertical space after the toc and do not indent the following text
  }%
}
\xapptocmd\addtocentrydefault{% patch the KOMA-Script's generic toc entry generator
  \ifusechaptertoc% if chapter toc entries should be generated
    \Ifstr{#1}{chapter}{}
      {\expandafter\tocbasic@addxcontentsline\expandafter{\ext@chaptoc}{#1}{#2}{#3}}% do it
  \fi
}{}{}
\xpretocmd\chapter{\usechaptertocfalse}{}{}% entries in chapter toc are automatically switched off at start of \chapter
\xpretocmd\part{\usechaptertocfalse}{}{}% entries in chapter toc are automatically switched off at start of \part
\makeatother

\newcommand\setchaptertoc[1][]{%
  \Ifstr{#1}{}
    {\AddtoOneTimeDoHook{heading/preinit/chapter}{\chaptertoc}}
    {\AddtoOneTimeDoHook{heading/preinit/chapter}{\chaptertoc[#1]}}%
}

\setcounter{tocdepth}{\partnumdepth}% depth of TOC
\begin{document}
\tableofcontents

\setcounter{page}{3000}
\setcounter{chapter}{100}
\setcounter{part}{5080}

\part{Part I}
\parttoc

\setchaptertoc
\chapter{My chapter I}
\section{My first section}
\blindtext
\subsection{My first subsection}
\blindtext
\section{My second section}
\blindtext

\chapter{My chapter II}
\chapter{My chapter II}
\chapter{My chapter III}
\chapter{My chapter IV}
\chapter{My chapter V}
\chapter{My chapter VI}
\chapter{My chapter VII}
\chapter{My chapter VIII}
\chapter{My chapter IX}
\chapter{My chapter X}
\chapter{My chapter XI}
\chapter{My chapter XII}
\chapter{My chapter XIII}
\chapter{My chapter XIV}
\chapter{My chapter XV}
\chapter{My chapter XVI}
\chapter{My chapter XVII}
\chapter{My chapter XVIII}
\chapter{My chapter XIX}
\chapter{My chapter XX}
\chapter{My chapter XXI}
\chapter{My chapter XXII}
\chapter{My chapter XXIII}
\chapter{My chapter XXIV}
\chapter{My chapter XXVI}
\chapter{My chapter XXVII}
\chapter{My chapter XXVIII}
\chapter{My chapter XXIX}
\chapter{My chapter XXX}

\part{Part II}
\parttoc
\setchaptertoc
\chapter{My chapter I}
\section{My first section}
\blindtext
\subsection{My first subsection}
  \blindtext
\section{My second section}
\blindtext:

\end{document}

This puts an error

! Incomplete \iffalse; all text was ignored after line 127.
<inserted text> 
                \fi

Using the polutonikogreek language instead of the greek language puts the same error. I suspect that this is related to the Ifstr\ifstr problem in KomaScript but when the greek language works in a complete book, it doesn't bear this minitoc approach? I need to write some sentences in greek, not the complete book.

2
  • The error appears with pdflatex. It does not with xelatex or lualatex. Jan 21 at 16:20
  • Thanks you Simon, I confirm. However, for the moment, I can't compile the complete book with xelatex or lulatex. Maybe is there a solution with pdflatex ? Jan 21 at 18:15

1 Answer 1

1

With \usepackage[greek]{babel} and PDFLaTeX \@Roman is no longer expandable and not suitable to be used as part of a filename, because it contains font switching commands. A simple suggestion would be, to not use \thepart but \the\c@part to build the extensions of the part contents auxiliary files:

\documentclass[fontsize=10pt,open=any,twocolumn]{scrbook}

\usepackage[greek]{babel}

\usepackage{blindtext}

\usepackage{scrwfile}% avoid trouble with the limited number of write handles
\usepackage{xpatch}

% https://tex.stackexchange.com/a/359758
% https://tex.stackexchange.com/a/502077
\makeatletter
\newif\ifuseparttoc
\newcommand*{\parttoc}[1][\the\c@part]{% CHANGED
  \useparttoctrue% switch on part-toc-entries
  \edef\ext@parttoc{tcp#1}% extension of the part-toc-file, e.g., tcp1
  \DeclareNewTOC[
    listname=Obsah,
    %unset=onecolumn% if the part toc should use twocolumn
  ]{\ext@parttoc}% declare a new toc file
  \begingroup
    \value{tocdepth}=\chaptertocdepth% we want entries down to chapter
    \listoftoc{\ext@parttoc}% show the toc with header
  \endgroup
}
\xapptocmd\addtocentrydefault{% patch the KOMA-Script's generic toc entry generator
  \ifuseparttoc% if part toc entries should be generated
    \expandafter\tocbasic@addxcontentsline\expandafter{\ext@parttoc}{#1}{#2}{#3}% do it
  \fi
}{}{}
\xpretocmd\part{\useparttocfalse}{}{}% entries in part toc are automatically switched off at start of \part
\newif\ifusechaptertoc% Switch to tell \addtocentrydefault to not only make entries to the toc-file but also to the current section-toc-file
\newcommand*{\chaptertoc}[2][\thechapter]{% new command to generate and show a chapter toc
  \usechaptertoctrue% switch on chapter-toc-entries
  \edef\ext@chaptoc{tcc#1}% extension of the chapter-toc-file, e.g., tcc1
  \DeclareNewTOC{\ext@chaptoc}% declare a new toc file
  %\addsec*{Contents}% header of the chaptertoc
  \setchapterpreamble{%
    \begin{minipage}{\linewidth}
      \hrulefill\par
      \value{tocdepth}=\subsectiontocdepth% we want entries down to subsection
      \listoftoc*{\ext@chaptoc}% show the toc without header
    \end{minipage}%
    \par\bigskip\nobreak\noindent\hrulefill\par
    \bigskip\noindent\ignorespaces% add some vertical space after the toc and do not indent the following text
  }%
}
\xapptocmd\addtocentrydefault{% patch the KOMA-Script's generic toc entry generator
  \ifusechaptertoc% if chapter toc entries should be generated
    \Ifstr{#1}{chapter}{}
      {\expandafter\tocbasic@addxcontentsline\expandafter{\ext@chaptoc}{#1}{#2}{#3}}% do it
  \fi
}{}{}
\xpretocmd\chapter{\usechaptertocfalse}{}{}% entries in chapter toc are automatically switched off at start of \chapter
\xpretocmd\part{\usechaptertocfalse}{}{}% entries in chapter toc are automatically switched off at start of \part
\makeatother

\newcommand\setchaptertoc[1][]{%
  \IfArgIsEmpty{#1}
    {\AddtoOneTimeDoHook{heading/preinit/chapter}{\chaptertoc}}
    {\AddtoOneTimeDoHook{heading/preinit/chapter}{\chaptertoc[#1]}}%
}

\setcounter{tocdepth}{\partnumdepth}% depth of TOC
\begin{document}
\tableofcontents

\part{Part I}
\parttoc
\setchaptertoc
\chapter{My chapter I}
\section{My first section}
\blindtext
\subsection{My first subsection}
\blindtext
\section{My second section}
\blindtext

\chapter{My chapter II}
\chapter{My chapter II}
\chapter{My chapter III}

\part{Part II}
\parttoc
\setchaptertoc
\chapter{My chapter I}
\section{My first section}
\blindtext
\subsection{My first subsection}
  \blindtext
\section{My second section}
\blindtext:

\end{document}

But note: If you somewhere switch the part numbering, this would not work. However, because of the optional argument of \parttoc you can alternatively use the original definition and use the optional argument, e.g. \parttoc[I] or \parttoc[examples].

Another alternative would be to locally redefine \ensureascii (and do this also for chapters) while building the file extension:

\documentclass[fontsize=10pt,open=any,twocolumn]{scrbook}
\usepackage[greek]{babel}

\usepackage{blindtext}

\usepackage{scrwfile}% avoid trouble with the limited number of write handles
\usepackage{xpatch}

% https://tex.stackexchange.com/a/359758
% https://tex.stackexchange.com/a/502077
\makeatletter
\newif\ifuseparttoc
\newcommand*{\parttoc}[1][\thepart]{% new command to generate and show a part toc
  \useparttoctrue% switch on part-toc-entries
  \begingroup
    \let\ensureascii\@firstofone% Don't switch encoding or font
    \xdef\ext@parttoc{tcp#1}% extension of the part-toc-file, e.g., tcpI
  \endgroup
  \DeclareNewTOC[
    listname=Obsah,
    %unset=onecolumn% if the part toc should use twocolumn
  ]{\ext@parttoc}% declare a new toc file
  \begingroup
    \value{tocdepth}=\chaptertocdepth% we want entries down to chapter
    \listoftoc{\ext@parttoc}% show the toc with header
  \endgroup
}
\xapptocmd\addtocentrydefault{% patch the KOMA-Script's generic toc entry generator
  \ifuseparttoc% if part toc entries should be generated
    \expandafter\tocbasic@addxcontentsline\expandafter{\ext@parttoc}{#1}{#2}{#3}% do it
  \fi
}{}{}
\xpretocmd\part{\useparttocfalse}{}{}% entries in part toc are automatically switched off at start of \part
\newif\ifusechaptertoc% Switch to tell \addtocentrydefault to not only make entries to the toc-file but also to the current section-toc-file
\newcommand*{\chaptertoc}[2][\thechapter]{% new command to generate and show a chapter toc
  \usechaptertoctrue% switch on chapter-toc-entries
  \begingroup
    \let\ensureascii\@firstofone% Don't switch encoding or font
    \xdef\ext@chaptoc{tcc#1}% extension of the chapter-toc-file, e.g., tcc1
  \endgroup
  \DeclareNewTOC{\ext@chaptoc}% declare a new toc file
  %\addsec*{Contents}% header of the chaptertoc
  \setchapterpreamble{%
    \begin{minipage}{\linewidth}
      \hrulefill\par
      \value{tocdepth}=\subsectiontocdepth% we want entries down to subsection
      \listoftoc*{\ext@chaptoc}% show the toc without header
    \end{minipage}%
    \par\bigskip\nobreak\noindent\hrulefill\par
    \bigskip\noindent\ignorespaces% add some vertical space after the toc and do not indent the following text
  }%
}
\xapptocmd\addtocentrydefault{% patch the KOMA-Script's generic toc entry generator
  \ifusechaptertoc% if chapter toc entries should be generated
    \Ifstr{#1}{chapter}{}
      {\expandafter\tocbasic@addxcontentsline\expandafter{\ext@chaptoc}{#1}{#2}{#3}}% do it
  \fi
}{}{}
\xpretocmd\chapter{\usechaptertocfalse}{}{}% entries in chapter toc are automatically switched off at start of \chapter
\xpretocmd\part{\usechaptertocfalse}{}{}% entries in chapter toc are automatically switched off at start of \part
\makeatother

\newcommand\setchaptertoc[1][]{%
  \IfArgIsEmpty{#1}
    {\AddtoOneTimeDoHook{heading/preinit/chapter}{\chaptertoc}}
    {\AddtoOneTimeDoHook{heading/preinit/chapter}{\chaptertoc[#1]}}%
}

\setcounter{tocdepth}{\partnumdepth}% depth of TOC
\begin{document}
\tableofcontents

\part{Part I}
\parttoc
\setchaptertoc
\chapter{My chapter I}
\section{My first section}
\blindtext
\subsection{My first subsection}
\blindtext
\section{My second section}
\blindtext

\chapter{My chapter II}
\chapter{My chapter II}
\chapter{My chapter III}

\part{Part II}
\parttoc
\setchaptertoc
\chapter{My chapter I}
\section{My first section}
\blindtext
\subsection{My first subsection}
  \blindtext
\section{My second section}
\blindtext

\end{document}

This would also fix similar issues with \@Alph or \@alph, e.g., if you have an appendix.

Note also: I've removed some code only to minimize the example a little more.

However, instead of the shown suggestion, I would suggest to use LuaLaTeX and also to remove the experimental package scrwfile. The KOMA-Script manual also suggests to use LuaLaTeX to avoid the “No room for new write” issue.

1
  • That's great @cabohah, thanks! What to do if I want the summary of the chapters falls just under the part title (no page break) and if I don't want a title to this list of chapters? The script doesn't work if I deactivate listname. And if I don't want a page break after a part, I can use the Mico script. but it isn't useful here, is it? Jan 23 at 22:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .