8

I am using the tocloft package already. I have a couple of long chapter names that wrap onto a second line, but it lines it up with the section numbers below. I must have this new line line up with the original chapter name. I have seen one other similar question, but it was not answered. Example:

CHAPTER 1:  THIS IS AN EXTREMELY LONG,
    LONG CHAPTER NAME.......................2
    1.1 blah blah...........................3
    1.2 blah blah...........................4

I need it to be:

CHAPTER 1:  THIS IS AN EXTREMELY LONG,
            LONG CHAPTER NAME...............2
    1.1 blah blah...........................3
    1.2 blah blah...........................4

Hope someone can help!

Thanks in advance!

Note: Someone else wrote the class file that I am using for my dissertation, so I am trying to edit it to fit the formatting needs of my University.

Here is the original code that's being used to generate the TOC:

  % TABLE OF CONTENTS ----------------------------------------------------------
\renewcommand{\cftbeforechapskip}{\baselineskip}        % allow spacing after each chapter/section entry
\renewcommand{\contentsname}{TABLE OF CONTENTS}
\renewcommand{\cftbeforetoctitleskip}{-0.25in}        % Title is 1in from top
\renewcommand{\cftaftertoctitleskip}{2.0\baselineskip}% 1 double space after title
\renewcommand{\cfttoctitlefont}{\hfill}               % Blank space before title
\renewcommand{\cftaftertoctitle}{\hfill}              % Blank space after title
\renewcommand{\cftchapfont}{ }                        % Can make it bold faced here
\renewcommand{\cftchapleader}{\cftdotfill{\cftchapdotsep}}
\renewcommand{\cftchapdotsep}{\cftdotsep}             % Puts dots after chapter entries
\renewcommand{\cftchappresnum}{CHAPTER }                %
\renewcommand{\cftchapaftersnum}{ }                     %
\renewcommand{\cftchapaftersnumb}{\phantom{CHAPTER}\rm} %
\renewcommand{\cftsecleader}{\cftdotfill{\cftsecdotsep}}%
\renewcommand{\cftchappagefont}{ }                      %

This compiles fine, no errors... When I replace the line

\renewcommand{\cftchappresnum}{CHAPTER }                %
with
\renewcommand{\cftchappresnum}{CHAPTER\ }               %

as advised, and add the line

\renewcommand{\cftchapnumwidth}{1in}

I get 9 errors in the .toc file.

Update: Repaired the 9 errors in .toc file by using

\setlength{\cftchapnumwidth}{1.2in}

instead of

\renewcommand{\cftchapnumwidth}{1in}.

The last thing I'm working on is how these fixes will jive with the appendix patch:

\newcommand*\tocappendixpatch{%
  \addtocontents{toc}{\setcounter{tocdepth}{0}}
  \addtocontents{toc}{\protect\renewcommand*\protect\cftchappresnum{\@chapapp\ }}
  \addtocontents{toc}{\settowidth{\cftchapnumwidth}{\hspace*{9mm}}}
  \renewcommand{\thesection}%
    {\Alph{chapter}.\arabic{section}\hspace{-.5em}\setcounter{equation}{0}}
}

The rest of the TOC looks excellent, but I think the code in the appendixpatch is conflicting, so I'm working on figuring that out, now.

Update: I got it! I replaced the line

\addtocontents{toc}{\settowidth{\cftchapnumwidth}{\hspace*{9mm}}}

with

\setlength{\cftchapnumwidth}{1.2in}

within the appendix patch, and the spacing came out perfectly, matching the lines above it.
This has been quite stressful. It took my whole weekend! It would've been much simpler to just shorten the chapter names!!

Thanks to all of you who helped!! I hope I can help people with their questions later when I have time!

  • 1
    You can also give abbreviated chapter names as an optional argument: \chapter[Short Name]{Long Name} The short name is then used in the TOC – Lagerbaer Apr 18 '11 at 00:08

2 Answers2

11

There are numerous problems with the code you posted, which will explain why the titles don't line up.

Specifically:

\renewcommand{\cftchapfont}{ } % this should not have a space

it should be

\renewcommand{\cftchapfont}{} % notice there is no space here

The following command:

\renewcommand{\cftchapaftersnumb}{\phantom{CHAPTER}\rm} 

is a hack for spacing. It should be removed altogether.

Another extra space in:

\renewcommand{\cftchappagefont}{ }    % this should not have a space either

should be

\renewcommand{\cftchappagefont}{}

You can specify the width of the chapter number and the text will wrap appropriately. So adding your commands to a minimal document, you get the following:

\documentclass{book}
\usepackage{tocloft}

\begin{document}
\renewcommand{\cftbeforechapskip}{\baselineskip}      % allow spacing after each chapter/section entry
\renewcommand{\contentsname}{TABLE OF CONTENTS}
\renewcommand{\cftbeforetoctitleskip}{-0.25in}        % Title is 1in from top
\renewcommand{\cftaftertoctitleskip}{2.0\baselineskip}% 1 double space after title
\renewcommand{\cfttoctitlefont}{\hfill}               % Blank space before title
\renewcommand{\cftaftertoctitle}{\hfill}              % Blank space after title
\renewcommand{\cftchapfont}{}                         % Can make it bold faced here; don't put a space in the {}
\renewcommand{\cftchapleader}{\cftdotfill{\cftchapdotsep}}
\renewcommand{\cftchapdotsep}{\cftdotsep}             % Puts dots after chapter entries
\renewcommand{\cftchappresnum}{CHAPTER\ }             %
\renewcommand{\cftchapaftersnum}{}                    % Don't put a space in the {}
\renewcommand{\cftsecleader}{\cftdotfill{\cftsecdotsep}}%
\renewcommand{\cftchappagefont}{}                      %
\renewcommand{\cftchapnumwidth}{1in}
\frontmatter
\tableofcontents

\mainmatter
\chapter{A really long chapter heading that will wrap around in the table of contents}
\section{First section}
\section{Second section}
\chapter{Another chapter}
\section{Another section}
\end{document}

output of code

Alan Munn
  • 218,180
  • Thank you, but this doesn't seem to be working with my code. I see that the following workaround (which I read has to do with hypertext and tocloft being used at the same time), which prevents the first line from being boldface: \renewcommand{\cftchapaftersnum}{ } % \renewcommand{\cftchapaftersnumb}{\phantom{CHAPTER}\rm} % \renewcommand{\cftsecleader}{\cftdotfill{\cftsecdotsep}}% \renewcommand{\cftchappagefont}{ } %

    Also, the wrapped around text is ALMOST lining up, but is not as perfect as your sample shows. Compiling now gives 9 errors in .toc file

    – Valjean Elander Apr 18 '11 at 00:27
  • So add an example just like mine (but adapted to what you have done) to your question, so that we can see what you are doing. – Alan Munn Apr 18 '11 at 00:30
  • Ok, I edited my main question to include the code and more details. I will include a picture shortly. I am new at this message board, so I appreciate your patience. – Valjean Elander Apr 18 '11 at 00:59
  • It won't let me submit a picture to show the output since I am a new user. – Valjean Elander Apr 18 '11 at 01:03
  • I see in your example, that when you include the line \renewcommand{\cftchapfont}{ }, and it's no longer bold, then the second line indentation no longer lines up – Valjean Elander Apr 18 '11 at 01:18
  • 2
    @Valjean Elander: instead of using \renewcommand{\cftchapfont}{ } (which adds an undesired space) use \renewcommand{\cftchapfont}{\normalfont}. – Gonzalo Medina Apr 18 '11 at 02:11
  • @Valjean I've updated my answer to reflect the changed question. (Gonzalo has also identified part of your spacing problem.) – Alan Munn Apr 18 '11 at 02:21
  • Thank you! I got the main part to work, now I am having trouble with this code interfering with an "appendix patch" that was added so that the chapter numbers will say "APPENDIX 1" instead of CHAPTER 1, etc..., which I am working on now. – Valjean Elander Apr 18 '11 at 03:49
  • I just edited my question to add the appendixpatch that I just referred to. The "chapter name" APPENDIX 1, etc., are overlapping with the Chapter names there now. – Valjean Elander Apr 18 '11 at 03:55
  • I figured it out! The TOC looks perfect now! That took a longer to figure out than writing the whole dissertation! Thanks, everybody! – Valjean Elander Apr 18 '11 at 04:06
  • @Valjean: About 5 hours? Wow. I hope my dissertation only takes 5 hours to write. – TH. Apr 18 '11 at 12:58
  • @TH. I think I was being sarcastic. Haha. – Valjean Elander Apr 19 '11 at 05:24
  • @Valjean: Don't worry, I got it. – TH. Apr 20 '11 at 02:47
  • @AlanMunn: There seems to be an extra \end{document} in the code? – Peter Grill Oct 04 '11 at 00:14
  • @PeterGrill Thanks. Fixed. (It would have been fine if you had edited yourself.) – Alan Munn Oct 04 '11 at 01:54
1

Another solution using the etoc package by Jean-François Burnol (see example p. 75 of the etoc manual).

\documentclass{book}
\usepackage{etoc}

\begin{document}
% From example p.75 The etoc package Jean-François Burnol v1.08g (2015/08/29)
\parindent 0pt \leftskip 0cm \rightskip .75cm \parfillskip -\rightskip
\newcommand*{\EndParWithPagenoInMargin}
    {\nobreak\hfill
     \nobreak\makebox[0.75cm][r]{\mdseries\normalsize\etocpage}%
     \par}
\renewcommand*\etoctoclineleaders
    {\hbox{\normalfont\normalsize\hbox to .75ex {\hss.\hss}}}
\newcommand*{\EndParWithPagenoInMarginAndLeaders}
    {\nobreak\leaders\etoctoclineleaders\hfill
     \nobreak\makebox[0.75cm][r]{\mdseries\normalsize\etocpage}%
     \par }
\etocsetstyle {chapter}
              {}
              {\leavevmode\leftskip 1cm\relax}
              {\large\llap{\makebox[1cm][r]{\bfseries Chapter~\etocnumber\ \ }}%
               \etocname\EndParWithPagenoInMargin\smallskip}
              {}
\etocsetstyle {section}
              {}
              {\leavevmode\leftskip 0.5cm\relax}
              {\normalsize\llap{\makebox[.75cm][l]{\bfseries\etocnumber}}%
               \etocname\EndParWithPagenoInMarginAndLeaders}
              {}

\tableofcontents

\chapter{A really long chapter heading that will wrap around in the table of contents}
\section{First section}
\section{Second section}
\chapter{A really long chapter heading that will wrap around in the table of contents}
\section{First section}
\section{Second section}
\end{document}

enter image description here

SDrolet
  • 4,908
  • 1
    you should wrap the whole thing within \begingroup\parindent 0pt... \tableofcontents\endgroup. Also the \nobreak before the \makebox are no-op in those locations (this is bug in the etoc manual provided example). –  Sep 10 '16 at 13:22