# Setting ToC depth error

I would like to set the depth of my table of contents to include chapters, sections (which are custom defined), subsections and subsubsections.

I tried including (currently commented out)

\setcounter{tocdepth}{3}


in the preamble, but I get an error that "Tex capacity exceeded". Could someone help?

documentclass[twoside,12pt,numbers=noenddot,tocbibind]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\setcounter{secnumdepth}{4}

%margins
\usepackage[top=3cm,bottom=2cm,left=2.5cm,right=2.5cm,asymmetric]{geometry}

\usepackage{tocloft}
%\setcounter{tocdepth}{3}

\colorlet{ctcolorchapterline}{cyan}
\colorlet{ctcolorchapternum}{cyan}

\RedeclareSectionCommand[
beforeskip=-1sp
]{chapter}

\makeatletter
\renewcommand\chapterlinesformat[3]{%
\ifstr{#1}{chapter}
{%
\parbox[b][\ht\strutbox]{\textwidth}{%
\parbox[b]{\dimexpr\textwidth-3em\relax}{\raggedright#3}%
\makebox[3em][r]{%
\hfill
#2%
}%
}%
}{\@hangfrom{#2}{#3}}% <- original definition for other levels
}
\makeatother
\renewcommand\chapterformat{%
\textcolor{ctcolorchapterline}{\rule[-5pt]{2pt}{5cm}}%
{\fontsize{60}{60}\selectfont\textcolor{ctcolorchapternum}{\thechapter}}%
}

\setlength{\cftsubsecindent}{1.2cm}
\setlength{\cftsubsecnumwidth}{2.5em}

%section titles in the margin
\renewcommand*{\sectionformat}{%
\llap{\thesection\autodot\enskip}%
}
\renewcommand*{\subsectionformat}{%
\llap{\thesubsection\autodot\enskip}%
}
\renewcommand*{\subsubsectionformat}{%
\llap{\thesubsubsection\autodot\enskip}%
}

\renewcommand\thesubsubsection{\thesubsection.\alph{subsubsection}}
\renewcommand{\thesubsection}{\arabic{chapter}.\arabic{subsection}}

\newcommand{\mysection}[1]{%
\section*{#1}%
\markright{#1}%
}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyfoot{}
\fancyhf{}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}

%hyperlinks and back referencing in format "(page #)"
\renewcommand\backreftwosep{, }
\renewcommand\backrefsep{, }
\renewcommand*{\backrefalt}[4]{%
\ifcase #1%
\or (page:~#2)%
\else (pages:~#2)%
\fi%
}
\usepackage{enumitem} % referencing in enumerate environment

%bibliography
\usepackage{natbib}


First of all, your Minimum Working Example is not entirely sufficient:

• The \colorlet command is part of the xcolor package, which is not included here.
• It contains other packages that are not a part of the problem.

When I run it with these adjustments and some bare minimum of content, it runs without errors, using LaTeX on a MiKTeX distribution. The error message itself seems to indicate some infinite loop, or other problem causing TeX to give you the error. A wrong symbol in a section heading for example: https://latex.org/forum/viewtopic.php?t=25596

Please strip down the whole document to the bare minimum and post it here. Also state which version of TeX/LaTeX you are using, when and how the problem started to occur (as far as you know) and what steps you already took.

Furthermore: I assume you are aware of the warning message telling you that it is not advised to use scrbook together with fancyhdr? Please see https://latex.org/forum/viewtopic.php?t=30696 for more information and how to suppress the error if necessary.

Unfortunaly there is no working example in the question, so I can not reproduce the issue.

Your definition of \mysection should be removed. Use the KOMA-Script command \addsec instead. It does what you want: A section title without number but with an entry in header and ToC.

For header and footer it is suggested to use KOMA-Script package scrlayer-scrpage with class scrbook.

Do not use package tocloft together with a KOMA-Script class. The indent and the number width for the subsection entries can be changed without this package. Note that scrbook loads and uses the KOMA-Script package tocbasic.

Additionally there is no option tocbibind. If the lists should get TOC entries use listof=totoc. Similar for the bibliography (bibliography=totoc) and index (index=totoc). If the TOC should also get an entry in the TOC (this is not recommended) add \setuptoc{toc}{totoc} to the preamble.

The head height should be set by package geometry.

For the special size of the chapter numbers (60pt) a scalable font is needed.

\documentclass[twoside,12pt,numbers=noenddot,
listof=totoc,% if LOF and LOT should get TOC entries
bibliography=totoc,
index=totoc
]{scrbook}
\usepackage{lipsum}% only for dummy text
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

%margins
\usepackage[top=3cm,bottom=2cm,left=2.5cm,right=2.5cm,asymmetric,
]{geometry}

\setuptoc{toc}{totoc}% if the TOC should get a TOC entry (not recommended)
\setcounter{tocdepth}{\subsubsectiontocdepth}

\colorlet{ctcolorchapterline}{cyan}
\colorlet{ctcolorchapternum}{cyan}

\usepackage{lmodern}% <- a scalable font is needed for the special size of the chapter numbers

\setcounter{secnumdepth}{\subsubsectionnumdepth}
\RedeclareSectionCommand[
beforeskip=-1sp
]{chapter}
\RedeclareSectionCommand[
counterwithout=section,
counterwithin=chapter,
tocindent=1.5em,
tocnumwidth=2.3em
]{subsection}
\RedeclareSectionCommand[
tocindent=3.8em,
tocnumwidth=3.2em
]{subsubsection}

\renewcommand\thesubsubsection{\thesubsection.\alph{subsubsection}}

\makeatletter
\renewcommand\chapterlinesformat[3]{%
\ifstr{#1}{chapter}
{%
\parbox[b][\ht\strutbox]{\textwidth}{%
\parbox[b]{\dimexpr\textwidth-3em\relax}{\raggedright#3}%
\makebox[3em][r]{%
\hfill
#2%
}%
}%
}{\@hangfrom{#2}{#3}}% <- original definition for other levels
}
\makeatother
\renewcommand\chapterformat{%
\textcolor{ctcolorchapterline}{\rule[-5pt]{2pt}{5cm}}%
{\fontsize{60}{60}\selectfont\textcolor{ctcolorchapternum}{\thechapter}}%
}

%section titles in the margin
\renewcommand*{\sectionformat}{%
\llap{\thesection\autodot\enskip}%
}
\renewcommand*{\subsectionformat}{%
\llap{\thesubsection\autodot\enskip}%
}
\renewcommand*{\subsubsectionformat}{%
\llap{\thesubsubsection\autodot\enskip}%
}

\clearmainofpairofpagestyles
\renewcommand\chaptermarkformat{}
\renewcommand\sectionmarkformat{}

\usepackage{enumitem} % referencing in enumerate environment

%bibliography
\usepackage{natbib}

%hyperlinks and back referencing in format "(page #)"
%% The following seems to be not related to the issue:
%\renewcommand\backreftwosep{, }
%\renewcommand\backrefsep{, }
%\renewcommand*{\backrefalt}[4]{%
%\ifcase #1%
%\or (page:~#2)%
%\else (pages:~#2)%
%\fi%
%}

\begin{document}
\tableofcontents
\listoffigures
\listoftables
\chapter{Chapter}
\lipsum[1]