Document ClassesHorizontal spacing in table of contents

Information and discussion about specific document classes and how to create your own document classes.
Post Reply
nettel
Posts: 14
Joined: Wed Oct 08, 2008 11:25 pm

Horizontal spacing in table of contents

Post by nettel »

im using \usepackage{titletoc} to generate table of contents.

The number format for this is given by
\renewcommand{\thesection}{\Roman{section}}
\renewcommand{\thesubsection}{\Alph{subsection}}
\renewcommand{\thesubsubsection}{\arabic{subsubsection}}

My problem is that as the width of the roman numerals in the toc for each section get bigger (more numerals ...hence greater the spacing they take up), it is writing over the top of the section heading in my TOC. How do i set a horizontal space in my document to move all of the section headers to the right to prevent them from overlaying on the roman numerals that sit adjacent to them on the left hand side?

I call the toc by using;

\begin{center}
\tableofcontents
\end{center}

This is new to me so if you could assist where to insert the appropriate code it would be much appreciated.

Cheers

Recommended reading 2024:

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

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

User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Horizontal spacing in table of contents

Post by gmedina »

Hi nettel,

we do not know how are you using titletoc, so it's hard to give you the exact solution. However, I designed a little example that might be helpful.

Code: Select all

\documentclass{article}
\usepackage{titletoc}

\titlecontents{section}[1.5em]
  {}
  {\contentslabel{1.5em}}%change the argument to obtain the 
                         %desired spacing
  {\hspace*{-2.3em}}
  {\titlerule*[1pc]{.}\contentspage}

%just to do a test
\setcounter{section}{10000}

\begin{document}
\tableofcontents
\newpage
\section{test}
\end{document}
In the code above I redefined the ToC entries for sections. If you process that code you will observe the undesired bahaviour that you mentioned. All you have to do is to change the argument of \contentslabel from its current value of 1.5em to, for example, 3em and the problem will be solved.

Refer to the titletoc documentation (pp. 16ff) for further information.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
nettel
Posts: 14
Joined: Wed Oct 08, 2008 11:25 pm

Horizontal spacing in table of contents

Post by nettel »

Thanks gmedina that fixed my spacing problems (btw i was just using Latex defaults for this packake). However the standard format options are now gone. Originally I had the chapters in bold and the the sections that sat under that chapter were non bolded. Now the entire TOC is unbolded and the horizontal spacing has also been lost. I have provided an before and after doc attached to this post to explain what i mean. The before section is the way I want it......just had to amend for the spacing which was {\contentslabel{2em}}

How can I amend the code to get this to format the way i require it?

\titlecontents{section}[1.5em]
{}
{\contentslabel{1.5em}}%change the argument to obtain the
%desired spacing
{\hspace*{-2.3em}}
{\titlerule*[1pc]{.}\contentspage}


Cheers
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Horizontal spacing in table of contents

Post by gmedina »

Hi,

Apparently you forgot the attachment. But a better idea would be for you to post, if possible, the actual code that produces the undesired bahaviour (in the sense of a minimal working example MWE). Following the link you will find instructions to build such a MWE.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
nettel
Posts: 14
Joined: Wed Oct 08, 2008 11:25 pm

Horizontal spacing in table of contents

Post by nettel »

sorry couldnt get file upload to work.

Here is a cut down example of my code that should explain what im trying to do.
Run this code first ( i had to build first to get this to work before i previewed the document)

Code: Select all

\documentclass[a4paper]{article}
\usepackage{titletoc}
%\titlecontents{section}[0em]
%  {}
%  {\contentslabel{2em}}%change the argument to obtain the 
%                         %desired spacing
%  {\hspace*{-2.3em}}
%  {\titlerule*[1pc]{.}\contentspage}

\renewcommand{\thesection}{\Roman{section}} % Numbering format for TOC
\renewcommand{\thesubsection}{\Alph{subsection}}
\renewcommand{\thesubsubsection}{\arabic{subsubsection}}

\begin{center}
\tableofcontents
\end{center}

\begin{document} 
\begin{center}
\section{Introduction}\label{intro} % 1st chapter
\end{center}
\subsection{Background} % sub heading under 1st chapter
	some text....
\subsection{Aim}
	some more text....
\subsection{Scope}
	more text....
\begin{center}
\section{Project Methodology}\label{prometh} % 2nd chapter
\end{center}

\subsection{Requirement for the Project} % sub heading under 2nd chapter
	text.....
\subsection{Redesign of a New Model} % sub heading under 2nd chapter
	text....
\subsection{Summary} % sub heading under 2nd chapter
	text...	
\end{document}
Now uncomment the section gmedina provided which is near the top as follows;

Code: Select all

%\titlecontents{section}[0em]
%  {}
%  {\contentslabel{2em}}%change the argument to obtain the 
%                         %desired spacing
%  {\hspace*{-2.3em}}
%  {\titlerule*[1pc]{.}\contentspage}
You should see the different format of the contents page now where bold headings have been lost and the vertical spaces has reduced. Im trying to use the piece of code gmedina provided which works but i wish to get the page the same as the default which is the example obtained from the first run of the code

can someone give me an insight on what to amend in this example to get it to work (again the format i wish for is the std toc format on the first run)

Cheers
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Horizontal spacing in table of contents

Post by gmedina »

Hi,

try using

Code: Select all

\titlecontents{section}[1.5em]
  {\bfseries}
  {\contentslabel{2em}}
  {\hspace*{-2.3em}}
  {\titlerule*[1pc]{.}\contentspage}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
nettel
Posts: 14
Joined: Wed Oct 08, 2008 11:25 pm

Re: Horizontal spacing in table of contents

Post by nettel »

Excellent we are getting there.
The only remaing issue is how do i now provide the spacing between chapters like in the first example of the code?

Also how do i remove the dots off the lines that are now bold (not all the dotted lines, just the ones from the chapter headings)?

Cheers
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Horizontal spacing in table of contents

Post by gmedina »

Hi,

try this:

Code: Select all

\titlecontents{section}[1.5em]
  {\addvspace{1em}\bfseries}
  {\contentslabel{2em}}
  {\hspace*{-2.3em}}
  {\hfill\contentspage}
You could also use titlesec to format the section titles instead of using \begin{center}...\end{center}. Refer to the package documentation for further information.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
nettel
Posts: 14
Joined: Wed Oct 08, 2008 11:25 pm

Re: Horizontal spacing in table of contents

Post by nettel »

You are a legend ....worked perfectly and all the issues have been rectified.

Thankyou for all of your help gmedina
Post Reply