0

In this code, I have defined custom sectioning formats using the titlesec package. However, I'm having trouble adjusting the spacing between the section numbers and titles, especially when the section numbers become larger, such as when they exceed a single digit.

For example, when the section number reaches two digits (e.g., 1.10), the distance between the number and the title becomes too small. I want to create a dynamic solution that automatically adjusts this spacing based on the length of the section number.

I have attempted to calculate the required spacing using the provided algorithm, but I'm struggling to implement it correctly into my code. Here's the algorithm I'm trying to apply: I create any commands (which work fine) to measure the length of the number level and thus adjust the spacing as explained with an specific (the only) comment putted into the code.

\documentclass{report}

\usepackage{fontsize}
\usepackage{titlesec}
\usepackage{calc}

\makeatletter
\titleclass{\subsubparagraph}{straight}[\subparagraph]

\titleformat{\subsubparagraph}{\normalfont\changefontsize{11pt}\bfseries}{\thesubsubparagraph}{1em}{}

\titlespacing*{\subsubparagraph}
{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}

\newcounter{subsubparagraph}[subparagraph]

\renewcommand\thesubsubparagraph{\thesubparagraph.\arabic{subsubparagraph}}

\def\toclevel@subparagraph{6}
\makeatother

\setcounter{secnumdepth}{6} 

\setcounter{tocdepth}{6}

\titleformat{\chapter}[hang]{\bfseries\Huge}{\thechapter|}{1.5ex}{}

\makeatletter
\def\l@chapter{\@dottedtocline{0}{0pt}{10pt}}
\def\l@section{\@dottedtocline{1}{10pt}{17.78pt}}
\def\l@subsection{\@dottedtocline{2}{27.78pt}{25.56pt}}
\def\l@subsubsection{\@dottedtocline{3}{53.34pt}{33.34pt}}
\def\l@paragraph{\@dottedtocline{4}{86.68pt}{41.12pt}}
\def\l@subparagraph{\@dottedtocline{5}{127.8pt}{48.90001pt}}
\def\l@subsubparagraph{\@dottedtocline{5}{176.70001pt}{56.68001pt}}

%To follow the algorithm to calculate the measure into the above commands: I would like write a code which automatically use this alorithm since the measure given above does not work when the number of any level is greatest than 9: e.g. if the number of level is 1.1.10 then the distance between the number and the name is less than 5pt.
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \def\l@chapter{\@dottedtocline{0}{0pt}{\chpatermeasure+5pt}}
% \def\l@section{\@dottedtocline{1}{\chaptermeasure +5pt}{\sectionmeasure+5pt}}
% \def\l@subsection{\@dottedtocline{2}{\chaptermeasure+\sectionmeasure+10pt}{\subsectionmeasure+5pt}}
% \def\l@subsubsection{\@dottedtocline{3}{\chpatermeasure+\sectionmeasure+\subsectionmeasure+15pt)}{\subsubsectionmeasure+5pt}}
% \def\l@paragraph{\@dottedtocline{4}{\chpatermeasure+\sectionmeasure+\subsectionmeasure+\subsubsectionmeasure+20}{\paragraphmeasure+5pt}}
% \def\l@subparagraph{\@dottedtocline{5}{\chpatermeasure+\sectionmeasure+\subsectionmeasure+\subsubsectionmeasure+\paragraphmeasure+25pt}{\subparagraphmeasure+5pt}}
% \def\l@subsubparagraph{\@dottedtocline{5}{\chpatermeasure+\sectionmeasure+\subsectionmeasure+\subsubsectionmeasure+\paragraphmeasure+\subparagraphmeasure+30pt}{\subsubparagraphmeasure+5pt}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatother


\newlength{\chapterwidth}
\newcommand{\measurechapterwidth}[1]{%
    \settowidth{\chapterwidth}{#1}%
    \the\chapterwidth
}

\newlength{\sectionwidth}
\newcommand{\measuresectionwidth}[1]{%
    \settowidth{\sectionwidth}{#1}%
    \the\sectionwidth
}

\newcommand{\chaptermeasure}{\measuresectionwidth{\thechapter}}
\newcommand{\sectionmeasure}{\measuresectionwidth{\thesection}}
\newcommand{\subsectionmeasure}{\measuresectionwidth{\thesubsection}}
\newcommand{\subsubsectionmeasure}{\measuresectionwidth{\thesubsubsection}}
\newcommand{\paragraphmeasure}{\measuresectionwidth{\theparagraph}}
\newcommand{\subparagraphmeasure}{\measuresectionwidth{\thesubparagraph}}
\newcommand{\subsubparagraphmeasure}{\measuresectionwidth{\thesubsubparagraph}}

\begin{document}

\tableofcontents

\part*{Part I}
\chapter{Chapter}
\chaptermeasure\\
\section{Section}
\sectionmeasure
\subsection{Subsection}
\subsectionmeasure
\subsubsection{Subsubsection}
\subsubsectionmeasure
\paragraph{Paragraph}
\paragraphmeasure
\subparagraph{Subparagraph}
\subparagraphmeasure
\subsubparagraph{Subsubparagraph}
\subsubparagraphmeasure
\end{document}

Could someone please help me modify my LaTeX code to incorporate this spacing adjustment algorithm correctly? Any insights or assistance would be greatly appreciated.

Thank you in advance for your help.

2
  • 1
    For example, package tocbasic can do the calculation automatically, it just needs an extra LaTeX run. See in the manual \DeclareTOCStyleEntry with style tocline and features dynumwith and dynindent and also this famous answer.
    – cabohah
    Commented May 4 at 12:37
  • @cabohah It seems your hint works fine! Thanks very much for the help! :-) Commented May 4 at 12:56

0

You must log in to answer this question.