1

I want to create a TOC, that will have equal margins between section numbers and section titles regardless of the number of digits in section number.

I have an example of solution, but I want to use titlesec to define my sections.

\renewcommand{\subsection}[1]
{%
  \refstepcounter{subsection}%
  \vskip 1.5ex%  
    {\bf\thesubsection \ #1}%
    \addcontentsline{toc}{subsection{\hspace{1em}\thesubsection\hspace{0.5em}#1}%
  \vskip 1.5ex %  
}%

Here is my try with titletoc It's seems to be right, but I can have a huge amount of sections and subsections in my document, so the reserved space will end up soon.

\documentclass{article}

\usepackage{titletoc}
\dottedcontents{subsection}[3.5em]{}{3.2em}{1pc}

\begin{document}

\tableofcontents

\section{Section}

\section{Section}

\section{Section}

\section{Section}

\section{Section}

\section{Section}

\section{Section}

\section{Section}

\section{Section}

\section{Section}

\subsection{Subsection1}

\subsection{Subsection2}

\subsection{Subsection3}

\subsection{Subsection4}

\subsection{SubsectionN}

\subsection{SubsectionN}

\subsection{SubsectionN}

\subsection{SubsectionN}

\subsection{SubsectionN}

\subsection{SubsectionN}

\subsection{SubsectionN}

\subsection{SubsectionN}

\end{document}

TOC with titlesec

MWE with redefined subsection:

\documentclass{article}

\renewcommand{\subsection}[1]
{%
  \refstepcounter{subsection}%
  \vskip 1.5ex%  
    {\bf\thesubsection \ #1}%
    \addcontentsline{toc}{subsection}{\hspace{1em}\thesubsection\hspace{0.5em}#1}%
  \vskip 1.5ex %  
}%

\begin{document}

\tableofcontents

\section{Section}

\section{Section}

\section{Section}

\section{Section}

\section{Section}

\section{Section}

\section{Section}

\section{Section}

\section{Section}

\section{Section}

\subsection{Subsection1}

\subsection{Subsection2}

\subsection{Subsection3}

\subsection{Subsection4}

\subsection{SubsectionN}

\subsection{SubsectionN}

\subsection{SubsectionN}

\subsection{SubsectionN}

\subsection{SubsectionN}

\subsection{SubsectionN}

\subsection{SubsectionN}

\subsection{SubsectionN}

\end{document}

TOC with redefined subsection

5
  • Why don't you use also titlesec for your subsections? On the other hand, I don't understand very well the problem with using titletoc?
    – Bernard
    Sep 1, 2019 at 12:17
  • @Bernard | use titlesec for my subsections, but I suppressed my subsections definitions to show only TOC. Sep 1, 2019 at 12:34
  • @Bernard The problem with titletoc is that I can't understand how to set equal margins between section number and section title with it. Sep 1, 2019 at 12:35
  • I'm not sure I understand quite well. Do you mean the, for instance, the gap between 10 and Section is not the same as between 9 and its title?
    – Bernard
    Sep 1, 2019 at 12:40
  • @Bernard Yes, it is Sep 1, 2019 at 12:45

2 Answers 2

1

I can propose this, with the rightlabels option and some trial and errors:

\documentclass{article}

\usepackage[rightlabels]{titletoc}%
\dottedcontents{section}[1.7em]{}{1.5em}{1pc}
\dottedcontents{subsection}[4.25em]{\small}{4.25em}{1pc}

\begin{document}
\setlength{\labelsep}{1cm}
\tableofcontents

\section{Section}

\section{Section}

\section{Section}

\section{Section}

\section{Section}

\section{Section}

\section{Section}

\section{Section}

\section{Section}

\section{Section}

\subsection{Subsection1}

\subsection{Subsection2}

\subsection{Subsection3}

\subsection{Subsection4}

\subsection{SubsectionN}

\subsection{SubsectionN}

\subsection{SubsectionN}

\subsection{SubsectionN}

\subsection{SubsectionN}

\subsection{SubsectionN}

\subsection{SubsectionN}

\subsection{SubsectionN}

\end{document} 

enter image description here

0

You could use the tocloft package.

My apologies, I initially used the memoir code but the following works for the article class.

\documentclass{article}
\usepackage{tocloft}
\setlength{\cftsecnumwidth}{...} % resets space for section number (usually 2.3em)
\setlength{\cftsubsecnumwidth}{...} % resets space for subsection number (usually 3.2em)
\begin{document}
\tableofcontents
% ...
\end{document}

Change the ...numwidths to suit your numbering scheme.

5
  • Do titlesec and tocloft play well together?
    – Alan Munn
    Sep 1, 2019 at 19:26
  • This doesn't work with article, only with memoir Sep 2, 2019 at 6:57
  • @AlanMunn I don't know. Any ideas? Sep 2, 2019 at 19:40
  • @AlanMunn I don't know. It may depend on what the user is trying to do. Any ideas. (I tried to edit my earlier comment but SE has a 5 minute space in which you can do so! (I started within the limit but ended outside it. Not a good user interface!) Sep 2, 2019 at 19:47
  • @ShalomAlecheim Thnk you. I have modified my answer. Sep 2, 2019 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.