Page LayoutSpacing in a table of contents with many sections

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
mgmillani
Posts: 16
Joined: Fri Dec 20, 2013 7:26 pm

Spacing in a table of contents with many sections

Post by mgmillani »

In a document with more than 9 chapters or sections, the section number in the table of contents overlaps its title (see attached file).

Since I'm writing a .cls file to implement a given standard, I have to rewrite commands like \l@section and \@dottedtocline. This makes using packages like tocstyle a bit more complicated.

My idea to solve the problem was to get the width of the longest number and use it for every entry, for example:

Code: Select all

\renewcommand*{\l@section}[2]{  
  \begingroup\bfseries
  \settowidth{\seclen}{<width of longest section number>}
  \@dottedtocline{1}{0em}{\seclen}{\textbf{#1}}{\textbf{#2}}
  \endgroup%
}
The problem is, have no idea how to get the current section number (#1 includes the title), nor how to compute the largest value in (La)TeX.

I could set \seclen to a larger value, but then the spacing on most cases (where there are less than 10 chapters/sections) would be way too large.

I tried to find where \l@section is called so that I could send the section number as parameter to it, but I couldn't find it.

Any help is much appreciated.
Attachments
article.tex
(2.17 KiB) Downloaded 289 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Re: Spacing in a table of contents with many sections

Post by Johannes_B »

Package tocstyle would have been my first suggestion, why can't you use it?
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
mgmillani
Posts: 16
Joined: Fri Dec 20, 2013 7:26 pm

Spacing in a table of contents with many sections

Post by mgmillani »

Well, I tried to use tocstyle early, but I was getting "Undefined control sequence" when including the package for some reason. But I guess it was related with something on the .aux files, since it works now.

The only problem that I now have is that I can no longer redefine \l@section, since tocstyle overwrites it after the beginning of the document. All I needed was the section title to be in bold face in the TOC. Strangely, I had no problem redefining \l@chapter.

I'm using tocstyle with the options [tocindentauto,tocfullflat].
Attachments
article.tex
Updated document
(6.15 KiB) Downloaded 319 times
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Spacing in a table of contents with many sections

Post by Stefan Kottwitz »

You still could place your redefinitions after \begin{document} yourself. Which would mean, in this example, simply moving the line \begin{document} above \renewcommand*{\l@chapter}... There are macros to do the same, but this way it works easily without loading a package and having a headache what may come first.

Stefan
LaTeX.org admin
mgmillani
Posts: 16
Joined: Fri Dec 20, 2013 7:26 pm

Spacing in a table of contents with many sections

Post by mgmillani »

Adding

Code: Select all

\renewcommand*{\l@section}[2]{  
  \begingroup\bfseries
  \@dottedtocline{1}{0em}{\seclen}{\textbf{#1}}{\textbf{#2}}
  \endgroup%
}
after \begin{document} yields the following error:

Code: Select all

Illegal parameter number in definition of \reserved@a.
<to be read again> 
                   1
l.168   \@dottedtocline{1}{0em}{\seclen}{\textbf{#1}
                                                  }{\textbf{#2}}
Since I'm writing a .cls file, telling the users to put this code after \begin{document} wouldn't be an option. Using \AtBeginDocument was also useless.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Spacing in a table of contents with many sections

Post by Stefan Kottwitz »

mgmillani wrote:Adding ... yields the following error ...
You just forgot to use \makeatletter ... \makeatother.
mgmillani wrote:Since I'm writing a .cls file, telling the users to put this code after \begin{document} wouldn't be an option.
Sure, but if necessary it could be put into a separate file such as body_settings.tex and the user should place \input{body_settings.tex} after \begin{document}. Such a way could still avoid telling internals.
mgmillani wrote:Using \AtBeginDocument was also useless.
Without minimal example, it did not happen. ;-)

Stefan
LaTeX.org admin
mgmillani
Posts: 16
Joined: Fri Dec 20, 2013 7:26 pm

Spacing in a table of contents with many sections

Post by mgmillani »

Stefan_K wrote:You just forgot to use \makeatletter ... \makeatother.
Indeed, there are no more errors. But it was also useless (nothing changes).
Stefan_K wrote:Sure, but if necessary it could be put into a separate file such as body_settings.tex and the user should place \input{body_settings.tex} after \begin{document}. Such a way could still avoid telling internals.
I guess \renewenvironment{document} would also work, so yeah, that won't be a problem.

In the attached file the \AtBeginDocument part was commented out.
Attachments
article.tex
(6.3 KiB) Downloaded 314 times
mgmillani
Posts: 16
Joined: Fri Dec 20, 2013 7:26 pm

Spacing in a table of contents with many sections

Post by mgmillani »

I changed the tocstyle.sty file so that it wouldn't overwirte any \l@something at the beginning of the document. Now everything works like a charm!

Thanks for the help!
Attachments
tocstyle.sty
modified tocstyle
(30.72 KiB) Downloaded 302 times
article.tex
(6.08 KiB) Downloaded 318 times
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Spacing in a table of contents with many sections

Post by Stefan Kottwitz »

Nice to see that you could make it work!

Just a comment: I would not change a .sty file. Either I would use \renewcommand to modify its macros, or if it's a big change I would copy the file, rename it, and change it then. With the same name there could be a problem with other files using that .sty file, or with your document on another computer.

Stefan
LaTeX.org admin
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Spacing in a table of contents with many sections

Post by Johannes_B »

Hey guys, why so complicated? Package tocstyle is designed to give hooks for every possible location, to get the user the possibility to hack in some stuff. Look at the following :-)

Code: Select all

\documentclass{report}
\setcounter{secnumdepth}{5}
\usepackage[tocflat]{tocstyle}
\settocstylefeature[0]{entryhook}{\bfseries}
\settocstylefeature[1]{entryhook}{\bfseries}
\begin{document}

\tableofcontents

\chapter{F}
\section{S}
\subsection{SS}
\chapter{C}
\chapter{C}
\chapter{C}
\chapter{C}
\chapter{C}
\chapter{C}
\chapter{C}
\chapter{C}
\chapter{C}
\chapter{C}
\chapter{C}
\section{S}
\section{S}
\section{S}
\section{S}
\section{S}
\section{S}
\section{S}
\section{S}
\section{S}
\section{S}
\subsubsection{SS}
\subsubsection{SS}
\subsubsection{SS}
\subsubsection{SS}
\subsubsection{SS}
\subsubsection{SS}
\subsubsection{SS}
\subsubsection{SS}
\subsubsection{SS}
\subsubsection{SS}


\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Post Reply