1

I want to print the section names in the ToC in a larger font than everything else.

Here is the command for it:

\titlecontents{section}
    [1.5em]
    {}
    {\large\sffamily\bfseries\contentslabel[\thecontentslabel.]{1.5em}}
    {\hspace{-1.5em}}
    {\hspace*{\fill}\sffamily\bfseries\large\contentspage}

However, because of the \large command the number of the section turns out to be in the margin:

enter image description here

How can I fix it? Here is the full MWE:

\documentclass[12pt]{article}

\usepackage[showframe]{geometry}

\usepackage{titletoc}


\titlecontents{section}
    [1.5em]
    {}
    {\large\sffamily\bfseries\contentslabel[\thecontentslabel.]{1.5em}}
    {\hspace{-1.5em}}
    {\hspace*{\fill}\sffamily\bfseries\large\contentspage}


\begin{document}

\tableofcontents

\section{One}
\subsubsection{}
\subsubsection{title}
\subsubsection{title}


\section{Two}
\subsubsection{}
\subsubsection{title}
\subsubsection{title}


\end{document}
3
  • Increase the [1.5em] to something like [2em], or else decrease the {1.5em} to something like {1.1em}. Oct 2 '17 at 10:15
  • @StevenB.Segletes I want perfect alignement. This is not going to work, because different digits have different width. Oct 2 '17 at 10:17
  • with tex, unless you are explicitly specifying something else, you will only get lining/tabular figures, which are always the same width. Oct 2 '17 at 13:47
2

This is caused by em unit depending on font.

\documentclass[12pt]{article}

\usepackage[showframe]{geometry}

\usepackage{titletoc}

\setbox0\hbox{\large\sffamily\bfseries\xdef\myoneandhalfem{\the\dimexpr1.5em}}

\titlecontents{section}
    [\myoneandhalfem]
    {}
    {\large\sffamily\bfseries\contentslabel[\thecontentslabel.]{1.5em}}
    {\hspace{-1.5em}}
    {\hspace*{\fill}\sffamily\bfseries\large\contentspage}


\begin{document}

\tableofcontents

\section{One}
\subsubsection{}
\subsubsection{title}
\subsubsection{title}

\section{Two}
\subsubsection{}
\subsubsection{title}
\subsubsection{title}

\end{document}

enter image description here

I don't know titletoc and what {\hspace{-1.5em}} stands for, but perhaps you may need there too the \hspace{-\myoneandhalfem}, although the picture seems fine even without it.

It is better not to use em at all when alignments are at stake, and font changes may occur. Unfortunately even the hard-coded distances in LaTeX table of contents code use it (but as much is hard-coded anyhow, that was not so much of a problem).

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.