5

I am trying to make a few subtle changes to the default table of contents in document{report}.

  1. I need the table of contents entries to adhere to the global \doublespacing, except for when an entry exceeds one line. In this case, I need it to wrap ~3/4 of the way through the page, and for the second line to be single-spaced relative to the first.
  2. I need it to do this for both chapter entries as well as section and subsection entries.

Here's a short compilable example of what I'm trying to do:

\documentclass{report}

\usepackage{setspace}

% Global double spacing
\doublespacing

\begin{document}

\cleardoublepage
\makeatletter
{
\renewcommand{\@pnumwidth}{2em}
\renewcommand{\@tocrmarg}{2em}

\renewcommand*{\l@chapter}[2]{%
  \addpenalty{-\@highpenalty}%
  \vskip 1.0em
  \setlength\@tempdima{1.5em}
  \begingroup
    \parindent \z@
    \rightskip \@pnumwidth
    \parfillskip -\@pnumwidth
    \leavevmode
    \advance\leftskip\@tempdima
    \hskip -\leftskip
    \hangindent=0.75\linewidth
    \hangafter=1
    \doublespacing
    \setstretch{1.0}
    {\bfseries #1}\nobreak
    \leaders\hbox{$\m@th \mkern \@dotsep mu.\mkern \@dotsep mu$}\hfill
    \nobreak #2\par
  \endgroup
}

\renewcommand*{\l@section}[2]{%
  \addpenalty{\@secpenalty}%
  \begingroup
    \parindent \z@
    \rightskip \@pnumwidth
    \parfillskip -\@pnumwidth
    \leavevmode
    \advance\leftskip 2.3em
    \hangindent=0.75\linewidth
    \hangafter=1
    \doublespacing
    \setstretch{1.0}
    #1\nobreak
    \leaders\hbox{$\m@th \mkern \@dotsep mu.\mkern \@dotsep mu$}\hfill
    \nobreak #2\par
  \endgroup
}

\renewcommand*{\l@subsection}[2]{%
  \addpenalty{\@secpenalty}%
  \begingroup
    \parindent \z@
    \rightskip \@pnumwidth
    \parfillskip -\@pnumwidth
    \leavevmode
    \advance\leftskip 3.8em
    \hangindent=0.75\linewidth
    \hangafter=1
    \doublespacing
    \setstretch{1.0}
    #1\nobreak
    \leaders\hbox{$\m@th \mkern \@dotsep mu.\mkern \@dotsep mu$}\hfill
    \nobreak #2\par
  \endgroup
}

\renewcommand{\contentsname}{\MakeUppercase{TABLE OF CONTENTS}}
\tableofcontents
}
\makeatother

\chapter{A Very Long Chapter Title That Will Definitely Require Wrapping To The Second Line in the Table of Contents}
\section{An Example Section Title That Is Long Enough to Wrap and Test This Case}
\subsection{A Subsection Title That Also Wraps to Check Multi-line Behavior}

\chapter{Another Chapter}
\section{Short Section}

\end{document}

Any and all help is greatly appreciated, as I've been banging my head against this wall for quite some time.

Thank you!

3
  • 1
    Welcome to TeX.SE! Please show us a short compilable TeX code resulting in your issue. Then we do not have to guess what you are doing ... Commented Jul 16 at 17:10
  • Thank you @Mensch, I've edited the original question so that hopefully this is reflected. Let me know if there's anything else you need! Commented Jul 16 at 17:32
  • Note: overleaf is irrelevant for the question, because it is a general LaTeX question. But, because it is a question about using package setspace and about (vertical) spacing, I've added these two tags. Commented Jul 18 at 7:04

2 Answers 2

2

You can do this using a toc package like tocbasic:

\documentclass{report}

\usepackage{tocbasic}
\usepackage{setspace}

% Global double spacing
\doublespacing

\DeclareTOCStyleEntries[%
  rightindent=.25\textwidth,% wrap after 3/4 of the text width
  beforeskip=\baselineskip,% separate entries by a vertical distance
  linefill=\TOCLineLeaderFill,% use dots also for chapter entries
]{tocline}{chapter,section,subsection}
\renewcommand*{\tableofcontents}{\listoftoc[TABLE OF CONTENTS]{toc}}
\BeforeStartingTOC[toc]{\singlespacing}

\begin{document}

\tableofcontents

\chapter{A Very Long Chapter Title That Will Definitely Require Wrapping To The Second Line in the Table of Contents}
\section{An Example Section Title That Is Long Enough to Wrap and Test This Case}
\subsection{A Subsection Title That Also Wraps to Check Multi-line Behavior}

\chapter{Another Chapter}
\section{Short Section}

\end{document}

table of contents with one line separation of entries, single-spacing inside entries and wrapping of entries after 3/4 of the page width

In the example, I've uses \singlespacing for the ToC, but separated the entries by an empty line, so a single entry is single-spaced but the distance between entries is like double-spaced. I'm sure, something like this could be done also using other common toc packages, e.g., etoc.

Incidentally, it should be noted that you should use as few declarations and definitions as possible after \begin{document}. In the interests of separating form and content, this should be done in the document preamble before \begin{document} if possible.

2

Patch \l@chapter to use \setstretch{1} and \@dottedtocline to add the vertical space corresponding to \doublespacing, which does \linespread{1.667}.

\documentclass{report}

\usepackage{setspace}
\usepackage{xpatch}

% Global double spacing

\makeatletter
\xpatchcmd{\l@chapter}{\begingroup}{\begingroup\setstretch{1}}{}{}
\xpatchcmd{\l@chapter}
  {\rightskip \@pnumwidth \parfillskip -\@pnumwidth}
  {\rightskip \@tocrmarg \parfillskip -\@tocrmarg}
  {}{}
\xpretocmd{\@dottedtocline}{\par\addvspace{0.667\baselineskip}}{}{}
\xpatchcmd{\@dottedtocline}{\leftskip}{\setstretch{1}\leftskip}{}{}
\renewcommand{\@tocrmarg}{0.25\textwidth}
\makeatother

\doublespacing
\renewcommand{\contentsname}{TABLE OF CONTENTS}

\begin{document}

\tableofcontents

\chapter{A Very Long Chapter Title That Will Definitely 
  Require Wrapping To The Second Line in the Table of Contents}
\section{An Example Section Title That Is Long Enough to Wrap and Test This Case}
\subsection{A Subsection Title That Also Wraps to Check Multi-line Behavior}

\chapter{Another Chapter}
\section{Short Section}

\end{document}

output

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.