Text FormattingTOC formatting - Bold Numbering and Line Spacing

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
FolkSong
Posts: 3
Joined: Thu Sep 06, 2012 4:03 am

TOC formatting - Bold Numbering and Line Spacing

Post by FolkSong »

I'm having trouble doing two things with my table of contents. Any help would be greatly appreciated.

1. Making chapter numbers bold. I made the chapter names bold in the TOC by adding \bf{} around the names themselves, but the numbers are still plain.

2. Line spacing - I would like to add an extra line between chapter entries (but not sections or subsections).

I tried using the tocloft package but it generated errors just by being included. So instead I have been just directly tweaking the class file which was provided by my university. The relevent section of the class file is below (the comments mentioning bold text were in the file when I got it):

Code: Select all

% TABLEOFCONTENTS
\def\tableofcontents{\@restonecolfalse\if@twocolumn\@restonecoltrue\onecolumn
  \fi\chapter*{\bf Table of Contents\@mkboth{}{}\addcontentsline{toc}{chapter}{\bf{Table of Contents}}} 
 \@starttoc{toc}\if@restonecol\twocolumn\fi} %added \bf to print Table of Contents in bold on pg 2006nm


\def\l@part#1#2{\addpenalty{-\@highpenalty}
   \addvspace{2.25em plus 1pt}  % space above part line
   \begingroup
   \@tempdima 3em         % width of box holding part number, used by
     \parindent \z@ \rightskip \@pnumwidth             %% \numberline
     \parfillskip -\@pnumwidth
     {%\large \bfseries          % set line in \large boldface 2006nm
     \leavevmode          % TeX command to enter horizontal mode.
     #1\hfil \hbox to\@pnumwidth{\hss #2}}\par
     \nobreak             % Never break after part entry
   \endgroup}


\def\l@chapter#1#2{\pagebreak[3]


   \vskip 1.0em plus 1pt  % space above chapter line
   \@tempdima 1.5em       % width of box holding chapter number
   \begingroup
     \parindent \z@ \rightskip \@pnumwidth
     \parfillskip -\@pnumwidth
     %\bfseries                  % Boldface removed. 2006nm
     \leavevmode          % TeX command to enter horizontal mode.
     #1\hfil \hbox to\@pnumwidth{\hss #2}\par
   \endgroup}

\def\l@chapter{\@dottedtocline{0}{0em}{2.3em}}  
\def\l@section{\@dottedtocline{1}{1.5em}{2.3em}} 
\def\l@subsection{\@dottedtocline{2}{3.8em}{3.2em}}
\def\l@subsubsection{\@dottedtocline{3}{7.0em}{4.1em}}
\def\l@paragraph{\@dottedtocline{4}{10em}{5em}}
\def\l@subparagraph{\@dottedtocline{5}{12em}{6em}}

% LIST OF FIGURES AND ILLUSTRATIONS
%
\def\listoffigures{\@restonecolfalse\if@twocolumn\@restonecoltrue\onecolumn
  \fi\chapter*{\bf{List of Figures and Illustrations}\markboth
   {}{}\addcontentsline{toc}{chapter}{\bf{List of Figures}}}%(NM 2005 added List of Figures to TOC) 
   \@starttoc{lof}\if@restonecol\twocolumn
  \fi}

\def\l@figure{\@dottedtocline{1}{1.5em}{2.3em}}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

TOC formatting - Bold Numbering and Line Spacing

Post by cgnieder »

Hi FolkSong,

Welcome to the LaTeX community!

Am I right in assuming that you're using a custom class and not a standard one? Anyway, the definition of \l@chapter defines how the chapter entries in the ToC are going to look. So I suggest removing the \bf{} and use the code below for \l@chapter instead. \bf{} is (a) not a LaTeX command but a TeX command and should be avoided in LaTeX and (b) does not have an argument but is a switch. \bf{bold} still bold and \bf bold still bold are (nearly) the same.

In the code you provided you can see in the definition of \l@chapter the following line:

Code: Select all

%\bfseries                  % Boldface removed. 2006nm
So someone removed the bold face (in 2006 presumably). Uncomment the line again. A small vertical skip between the chapter entry and a following entry can be inserted right before the command ends. I've marked the line below where I added \medskip.

Code: Select all

\documentclass{book}

\makeatletter
\def\l@chapter#1#2{\pagebreak[3]
   \vskip 1.0em plus 1pt  % space above chapter line
   \@tempdima 1.5em       % width of box holding chapter number
   \begingroup
     \parindent \z@ \rightskip \@pnumwidth
     \parfillskip -\@pnumwidth
     \bfseries            % Boldface removed. 2006nm %% <= \bfseries uncommented again
     \leavevmode          % TeX command to enter horizontal mode.
     #1\hfil \hbox to\@pnumwidth{\hss #2}\medskip\par%% <= \medskip inserted
   \endgroup}
\makeatother

\begin{document}
\tableofcontents

\chapter{Test chapter}
\section{Test section}
\subsection{Test subsection}

\chapter{Test chapter}
\section{Test section}
\subsection{Test subsection}

\end{document}
toc.png
toc.png (13.83 KiB) Viewed 13266 times
Regards
site moderator & package author
FolkSong
Posts: 3
Joined: Thu Sep 06, 2012 4:03 am

TOC formatting - Bold Numbering and Line Spacing

Post by FolkSong »

Thanks cgnieder for the welcome and the help. Initially the changes you suggested had no effect. There was an additional problem that I eventually solved even though I don't understand it. If you have a minute, maybe for my own education you could explain what happened.

Here is a MWE based on yours that shows the problem:

Code: Select all

\documentclass{book}

\makeatletter

\def\l@chapter#1#2{\pagebreak[3]
   \vskip 1.0em plus 1pt  % space above chapter line
   \@tempdima 1.5em       % width of box holding chapter number
   \begingroup
     \parindent \z@ \rightskip \@pnumwidth
     \parfillskip -\@pnumwidth
     \bfseries            % Boldface removed. 2006nm %% <= \bfseries uncommented again
     \leavevmode          % TeX command to enter horizontal mode.
     #1\hfil \hbox to\@pnumwidth{\hss #2}\par
   \endgroup}
   
\def\l@chapter{\@dottedtocline{0}{0em}{2.3em}}  %added this line 2006nm %Changed 09/05/2012
\def\l@section{\@dottedtocline{1}{1.5em}{2.3em}} %changed 1 to 0 2006nm
\def\l@subsection{\@dottedtocline{2}{3.8em}{3.2em}}
\def\l@subsubsection{\@dottedtocline{3}{7.0em}{4.1em}}
\def\l@paragraph{\@dottedtocline{4}{10em}{5em}}
\def\l@subparagraph{\@dottedtocline{5}{12em}{6em}}
       
\makeatother


\begin{document}
\tableofcontents

\chapter{Test chapter}
\section{Test section}
\subsection{Test subsection}

\chapter{Test chapter}
\section{Test section}
\subsection{Test subsection}

\end{document}
I fixed it by moving the line \def\l@chapter{\@dottedtocline{0}{0em}{2.3em}} above the other chapter definition.

Incidentally, the medskip turned out to be not needed, the spacing was how I wanted it after fixing the other problem.
Attachments
TOC.png
TOC.png (19.19 KiB) Viewed 13264 times
Last edited by cgnieder on Thu Sep 06, 2012 9:32 pm, edited 1 time in total.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

TOC formatting - Bold Numbering and Line Spacing

Post by cgnieder »

\def is a TeX command that defines a macro regardless if it has been defined before. So the second

Code: Select all

\def\l@chapter
redefined the first macro and all changes where gone. So you can simple delete this second definition.

So you can see it yourself:

Code: Select all

\documentclass{article}
\begin{document}
\def\foo{foo}
\foo

\def\foo{bar}
\foo
\end{document}
Regards
site moderator & package author
FolkSong
Posts: 3
Joined: Thu Sep 06, 2012 4:03 am

Re: TOC formatting - Bold Numbering and Line Spacing

Post by FolkSong »

Got it, thanks again!
Post Reply