2

I would like to add a line segment of specified length in the Table of Contents to separate two distinct portions. (I have search this site, but could only find an example to add a full-length horizontal line.)

I am surprised at my difficulty in accomplishing this because \rule{1.25in}{.15mm} works fine within the document itself. However, when I try to add the following, I get an error message:

\addcontentsline{toc}{\rule{1.25in}{.15mm}}

Consider the working example:

\documentclass[12pt]{book}

\usepackage{tocloft}
\renewcommand\cftchapafterpnum{\vskip 12pt} %set space after each Chapter in the Table of Contents
\renewcommand\cftsecafterpnum{\vskip 8pt}  %set space after each Section in the Table of Contents
\renewcommand\cftsubsecafterpnum{\vskip 8pt}  %set space after each Subsection in the Table of Contents


% Centering Table of Contents
\renewcommand{\cfttoctitlefont}{\hfill\Huge\bfseries}
\renewcommand{\cftaftertoctitle}{\hfill}


\begin{document}
\addtocontents{toc}{\protect\thispagestyle{empty}}
\tableofcontents

\addcontentsline{toc}{chapter}{Chapter 1}
\addcontentsline{toc}{chapter}{Chapter 2}
\addcontentsline{toc}{chapter}{Chapter 3}


\addtocontents{toc}{\vspace{\normalbaselineskip}}
%\addcontentsline{toc}{\rule{1.25in}{.15mm}}
\addtocontents{toc}{\vspace{\normalbaselineskip}}

\addcontentsline{toc}{chapter}{Next Part of the Table of Contents}
\end{document}

which produces the output

enter image description here

However, when I run the code with the \addcontentsline{toc}{\rule{1.25in}{.15mm}} command, I get the error message "Argument of \reserved@a has an extra }. \addtocontents".

And the following output results:

enter image description here

Again, I would like to add a horizontal line segment of specified length to the TOC---but everything I've tried so far has failed. Could someone please advise what I am doing wrong and how to correct it? Thank you.

4
  • \addcontentsline has three argument. You should use \addtocontents, or add {chapter} or {section}, etc. Commented Oct 27, 2020 at 20:29
  • @John Kormylo But wouldn't a page number be displayed? If so, how may I suppress the unwanted page number? Thank you,
    – DDS
    Commented Oct 27, 2020 at 20:48
  • @John Kormylo I just tried to specify {chapter}, but I get a different error message. It seems that the TOC does not accept the way I've defined the line segment, though it works fine in the document.
    – DDS
    Commented Oct 27, 2020 at 21:01
  • I didn't realize how badly tocloft was messing with \l@chapter. Anyway, if you don't want the page number, use \addtocontents. Commented Oct 27, 2020 at 21:05

2 Answers 2

4

You need to use \addtocontents rather than \addcontentsline and insert a \par after your \rule in order for the ToC contents to be processed successfully.

enter image description here

\documentclass{book}

\usepackage{tocloft}
\setlength{\cftbeforechapskip}{12pt}%set space after each Chapter in the Table of Contents
\setlength{\cftbeforesecskip}{8pt}  %set space after each Section in the Table of Contents
\setlength{\cftbeforesubsecskip}{8pt}  %set space after each Subsection in the Table of Contents

% Centering Table of Contents
\renewcommand{\cfttoctitlefont}{\hfill\Huge\bfseries}
\renewcommand{\cftaftertoctitle}{\hfill}

\begin{document}

\tocloftpagestyle{empty}

\tableofcontents

\addcontentsline{toc}{chapter}{Chapter 1}
\addcontentsline{toc}{chapter}{Chapter 2}
\addcontentsline{toc}{chapter}{Chapter 3}

\addtocontents{toc}{\vspace{\normalbaselineskip}}
\addtocontents{toc}{\rule[.1\normalbaselineskip]{1.25in}{.15mm}\par}
\addtocontents{toc}{\vspace{\dimexpr\normalbaselineskip-\cftbeforechapskip}}

\addcontentsline{toc}{chapter}{Chapter 4}

\end{document}

I moved the \rule up a tad to appear better vertically centered between the two chapter headings. I also used the default tocloft vertical spacing/skips and page styles rather than add that content to the ToC manually. It doesn't really matter that much though.

4
  • Thank you. It should work perfectly, but I the line "\addtocontents{toc}{\rule[.1\normalbaselineskip]{1.25in}{.15mm}\par}" is causing a problem on my end. The problem must be with me. I recently downloaded the most recent version of TexStudio to replace my old version, and have since had problems with, for example, some centering code in Tcolorbox. This might explain why I am not able to execute properly the two good answers that have been posted. Thank you again. I must figure out what's wrong on my end.
    – DDS
    Commented Oct 27, 2020 at 21:34
  • 1
    @mlchristians: Delete the .aux and .toc and try again. Some auxiliary files could get corrupted, or linger with faulty code in them.
    – Werner
    Commented Oct 27, 2020 at 21:53
  • Thank you for the suggestion; unfortunately, it did not work for this problem. But I hope I remember for the future. Thank you again.
    – DDS
    Commented Oct 27, 2020 at 22:26
  • Thank you for your answer. Though it didn't work for me because of a problem on my end, I did discover that \addtocontents{toc}{\hspace{2in}{\underline{\hspace{2in}}}\par} does the trick. Thanks also for telling me about the \par. It doesn't work otherwise. Thanks also for your comment about .aux and .toc. I believe that is why I got the revised code to work on a second run after it abended on the first.
    – DDS
    Commented Oct 28, 2020 at 2:35
3

This will place the line precisely in the middle of a 36pt gap (\baselineskip=12pt).

\documentclass[12pt]{book}

\usepackage{tocloft}
\renewcommand\cftchapafterpnum{\vskip 12pt} %set space after each Chapter in the Table of Contents
\renewcommand\cftsecafterpnum{\vskip 8pt}  %set space after each Section in the Table of Contents
\renewcommand\cftsubsecafterpnum{\vskip 8pt}  %set space after each Subsection in the Table of Contents


% Centering Table of Contents
\renewcommand{\cfttoctitlefont}{\hfill\Huge\bfseries}
\renewcommand{\cftaftertoctitle}{\hfill}


\begin{document}
\addtocontents{toc}{\protect\thispagestyle{empty}}
\tableofcontents

\addcontentsline{toc}{chapter}{Chapter 1}
\addcontentsline{toc}{chapter}{Chapter 2}
\addcontentsline{toc}{chapter}{Chapter 3}

\addtocontents{toc}{\rule[\dimexpr 0.5\ht\strutbox-0.5\dp\strutbox]{1.25in}{.15mm}\vskip 12pt}

\addtocontents{toc}{Next Part of the Table of Contents}
\end{document}
6
  • Thank you for your answer. I have no doubt it works on your end; but for some reason, when I run it, I get the error message `` Incomplete \iffalse; all text was ignored after line 22.''
    – DDS
    Commented Oct 27, 2020 at 21:14
  • Beats the heck out of me. Try removing the optional argument of \rule. it could be computed separately. Commented Oct 27, 2020 at 21:24
  • The problem has to be on my end. The other day I downloaded the most recent version of TexStudio and have noticed here and there, for example, with the \tcolorbox package, I am not able to do some of the things I should be able to. The download was very quick. Perhaps there was something else I omitted to do. In any case, I've got to figure out the problem on my end. Thank you so much for your help.
    – DDS
    Commented Oct 27, 2020 at 21:38
  • 1
    @mlchristians there is no way texstudio is causing issues on its own it is just an editor calling latex. Are you by any chance on Linux/macports and now you have two latex installations because installing texstudio brought texlive with it as a recommended install
    – daleif
    Commented Oct 28, 2020 at 20:30
  • @daleif When I downloaded the most recent version of Texstudio, I thought I would be receiving all the files in the ``TeX Collection'' as well. Since the download was very rapid, and I seem to be as limited as I was before with my old Tex Collection, I suspect I got no updated files, only the Editor. Perhaps I need to join TUG to be sure I get all the file and package updates on a DVD. Thanks for your comment.
    – DDS
    Commented Oct 29, 2020 at 2:15

You must log in to answer this question.

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