0

Background:: Hi there, new to everything here. I have a table of contents I'm producing in an Rnw file using \tableofcontents. I am formatting this toc to be two columns with the multicols package. I am also formatting the toc with the tocloft package: among other things, I have turned off tocloft's automatic section numbers on the left - as a result, the toc consists of (ideally) a left-aligned list of entries, with a right-aligned page number for each entry, all broken up over two columns.

Difficulty: In longer toc entries where 2 or more lines of text get produced, line(s) 2 and up get indented so that they do not align with the left margin of line 1. Ex:

image of two toc entries, second with indent on line 2

There are answers on this forum (regarding tables of contents that have, unlike mine, the page number on the left) for how to remove the indent on lines 2+ of a toc so that they align flush left with the section/page number. That's not what I'm looking for; I want lines 2+ of text to align with line 1 of the toc entry.

The rest of this post consists of my original code, my first attempt to address the issue, and my second.

Original code: (I tried to include only what's necessary for this section, so I hope I didn't cut out too much of what an external reader needs)

% Start the "Preamble."
\documentclass{article}
% use the LaTeX 'tocloft' package to adjust toc formatting.
\usepackage{tocloft}
% Enter remaining LaTeX preamble commands.
\title{This is my report}
\author{H.S.}
\date{May, 2023}
\begin{document}
% Insert a toc title, then a two-column list of contents that is hyperlinked.
\hypersetup{linktoc=all} % make page numbers also links
\renewcommand{\contentsname}{Contents} % give title of TOC
\renewcommand{\cftaftertoctitle}{\thispagestyle{empty}} % omit p# from footer
\renewcommand{\cftsecafterpnum}{\vskip35pt} % add dbl space beween entries
\renewcommand{\cftsecfont}{\LARGE} % adjust toc entries' font size
\renewcommand{\cfttoctitlefont}{
  \fontsize{35pt}{35pt}\selectfont % adjust toc title font size
  \bfseries % make toc title font bold
  \def\numberline#1{} % eliminate tocloft's auto section numbers
\begin{hyphenrules}{nohyphenation} % stop hyphenation
\setlength{\columnsep}{50pt} % set width of gap between columns
\begin{multicols}{2} % use two columns
\tableofcontents % list section titles as toc entries
\end{multicols}
\end{hyphenrules}
\clearpage
\section{Section One Title}
\clearpage
\section{Section Two Title, Super Long Example to Produce a Break so You Can See the Error}
\clearpage
\section{Section Three Title}
\clearpage
\end{document}

First attempt at a fix: I tried @tocrmarg (from the tocloft manual: "To have the (sectional) titles in the ToC, etc., typeset ragged right with no hyphenation," so I also got rid of the \hyphenrules lines in this version):

% Start the "Preamble."
\documentclass{article}
% use the LaTeX 'tocloft' package to adjust toc formatting.
\usepackage{tocloft}
% Enter remaining LaTeX preamble commands.
\title{This is my report}
\author{H.S.}
\date{May, 2023}
\begin{document}
% Insert a toc title, then a two-column list of contents that is hyperlinked.
\hypersetup{linktoc=all} % make page numbers also links
\renewcommand{\contentsname}{Contents} % give title of TOC
\renewcommand{\cftaftertoctitle}{\thispagestyle{empty}} % omit p# from footer
\renewcommand{\cftsecafterpnum}{\vskip35pt} % add dbl space beween entries
\renewcommand{\cftsecfont}{\LARGE} % adjust toc entries' font size
\makeatletter
  \renewcommand{\@tocrmarg}{2.55em plus 1fil}
\makeatother
\renewcommand{\cfttoctitlefont}{
  \fontsize{35pt}{35pt}\selectfont % adjust toc title font size
  \bfseries % make toc title font bold
  \def\numberline#1{}} % eliminate tocloft's auto section numbers
%\begin{hyphenrules}{nohyphenation} % stop hyphenation
\setlength{\columnsep}{50pt} % set width of gap between columns
\begin{multicols}{2} % use two columns
\tableofcontents % list section titles as toc entries
\end{multicols}
%\end{hyphenrules}
\clearpage
\section{Section One Title}
\clearpage
\section{Section Two Title, Super Long Example to Produce a Break so You Can See the Error}
\clearpage
\section{Section Three Title}
\clearpage
\end{document}

Nothing changed.

Second attempt at a fix: I tried to wrap the toc entries in a \raggedright command.

% Start the "Preamble."
\documentclass{article}
% use the LaTeX 'tocloft' package to adjust toc formatting.
\usepackage{tocloft}
% Enter remaining LaTeX preamble commands.
\title{This is my report}
\author{H.S.}
\date{May, 2023}
\begin{document}
% Insert a toc title, then a two-column list of contents that is hyperlinked.
\hypersetup{linktoc=all} % make page numbers also links
\renewcommand{\contentsname}{Contents} % give title of TOC
\renewcommand{\cftaftertoctitle}{\thispagestyle{empty}} % omit p# from footer
\raggedright{
  \renewcommand{\cftsecafterpnum}{\vskip35pt} % add dbl space beween entries
  \renewcommand{\cftsecfont}{\LARGE} % adjust toc entries' font size
  \renewcommand{\cfttoctitlefont}{
    \fontsize{35pt}{35pt}\selectfont % adjust toc title font size
    \bfseries % make toc title font bold
    \def\numberline#1{}} % eliminate tocloft's auto section numbers
  \begin{hyphenrules}{nohyphenation} % stop hyphenation
  \setlength{\columnsep}{50pt} % set width of gap between columns
  \begin{multicols}{2} % use two columns
  \tableofcontents % list section titles as toc entries
  \end{multicols}
  \end{hyphenrules}}
\clearpage
\section{Section One Title}
\clearpage
\section{Section Two Title, Super Long Example to Produce a Break so You Can See the Error}
\clearpage
\section{Section Three Title}
\clearpage
\end{document}

Definitely not what I was looking for - this added spaces between words in entries (like it was trying to justify the text), and some of the page numbers even got tabbed out further right than the other page numbers.

Thoughts? Thank you! :)

4
  • Welcome to TeX.SE! Please make your code snippets be compilable, then we do not have to guess what you are doing ...
    – Mensch
    May 1, 2023 at 17:00
  • Thanks @Mensch. I thought I had? Please tell me what to do differently. Thank you!
    – HermyS
    May 1, 2023 at 17:01
  • Well, perhaps only \end{document} is missing, I did not test it ...
    – Mensch
    May 1, 2023 at 17:04
  • @Mensch Oh my goodness! ::slaps forehead:: thank you so much, Mensch, I can't believe I forgot something so basic. Much appreciated! Hopefully it is compilable this time.
    – HermyS
    May 1, 2023 at 17:33

1 Answer 1

0
% Start the "Preamble."
\documentclass[english]{article}
% it seems this document uses environment hyphenrules from babel
\usepackage{babel}

%it uses multicols
\usepackage{multicol}


% already default
% \renewcommand{\contentsname}{Contents} % give title of TOC


% use the LaTeX 'tocloft' package to adjust toc formatting.
\usepackage{tocloft}

\renewcommand{\cftaftertoctitle}{\thispagestyle{empty}} % omit p# from footer
\renewcommand{\cftsecafterpnum}{\vskip35pt} % add dbl space beween entries
\renewcommand{\cfttoctitlefont}{%
   \fontsize{35pt}{35pt}\selectfont % adjust toc title font size
   \bfseries % make toc title font bold
}

% This is problematic because tocloft executes this in a group with
% the toc entry.  So the \baselineskip change enactment which requires a \par never
% happens.
% \renewcommand{\cftsecfont}{\LARGE} % adjust toc entries' font size
% this \cftsecfont can NOT be conveniently used for changing font size

% so crazy workaround
\renewcommand{\cftsecfont}{\LARGE\aftergroup \LARGE} % adjust toc entries' font size

% it would be THAT MUCH easier actually redefining \l@section ourself
\setlength{\cftsecnumwidth}{0pt}
\renewcommand\numberline[1]{}

% Enter remaining LaTeX preamble commands.
\title{This is my report}
\author{H.S.}
\date{May, 2023}

% it uses hyperref
\usepackage{hyperref}
\hypersetup{linktoc=all,% make page numbers also links
            colorlinks=true,% get rid of red frames (I would actually used
                            % hidelinks option for duration of
                            % tableofcontents)
            linkcolor=blue,
            }

\begin{document}

% Insert a toc title, then a two-column list of contents that is hyperlinked.

% \begin{hyphenrules}{nohyphenation} % stop hyphenation
% but why would you do that? especially in two columns
\setlength{\columnsep}{50pt} % set width of gap between columns
\begin{multicols}{2} % use two columns
\tableofcontents % list section titles as toc entries
\end{multicols}
% \end{hyphenrules}

% left-over from testing things
% \fontsize{35pt}{45pt}\selectfont 

\clearpage
\section{Section One Title}
\clearpage
\section{Section Two Title, Super Long Example to Produce a Break so You Can See the Error}
\clearpage
\section{Section Three Title}
\clearpage
\end{document}

Please ponder code comments for explanations.

enter image description here

2
  • That's super helpful, @user691586! I didn't realize some of the commands I had were interfering with one another. Cleaning that up and adding \renewcommand{\cftsecfont}{\LARGE\aftergroup \LARGE} did the trick. Thank you so much! This has been very educational.
    – HermyS
    May 2, 2023 at 16:56
  • @HermyS There is a problem of principle unfortunately with using \cftsecfont to change font size (either via a command such as \LARGE or some \fontsize{35pt}{40pt}\selectfont combo), because its action on the heading is encapsulated in a braced group {... } so when TeX builds the paragraph the change to the baseline interdistance has been reverted and the result is poor for long multi-line entries. (but this is some other problem than your original one related to the \leftskip).
    – user691586
    May 2, 2023 at 17:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .