GeneralAdding "Chapter" and "Appendix" to ToC

LaTeX specific issues not fitting into one of the other forums of this category.
chelos
Posts: 6
Joined: Mon Nov 17, 2008 10:30 pm

Adding "Chapter" and "Appendix" to ToC

Post by chelos »

Hello, I have just joined and I'm having trouble finding a solution to my problem.

For my Master's Thesis, I am apparently required to have the words "Chapter" and "Appendix" before the respective number and letter on the Table of Contents. For example "Chapter 1 - Introduction" instead of "1 - Introduction". Now, I was able to find somewhere that it might be done with some addon package, but since I'm doing this remotely on the school's LINUX computer using sftp, I do not have permission to install anything.

Is there *any* way to get this result without having to install anything? The deadline is getting close, and I'm beginning to get desperate!

Thanks for any help that can be given!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Adding "Chapter" and "Appendix" to ToC

Post by localghost »

Search the forum for the tocloft package and take look at its documentation. But I'm sure that already some solutions exist here in the forum. The package should already be part of the system you are using.


Best regards and welcome to the board
Thorsten¹
chelos
Posts: 6
Joined: Mon Nov 17, 2008 10:30 pm

Re: Adding "Chapter" and "Appendix" to ToC

Post by chelos »

Great! For some reason I had assumed tocloft was an addon. I'll give it a try and see what happens.
chelos
Posts: 6
Joined: Mon Nov 17, 2008 10:30 pm

Adding "Chapter" and "Appendix" to ToC

Post by chelos »

Ok, I found this thread very useful. I used the following code:

Code: Select all

\usepackage{tocloft}

\renewcommand{\cftchapfont}{\bfseries}
\renewcommand{\cftchappagefont}{\bfseries}
\renewcommand{\cftchappresnum}{Chapter }
\renewcommand{\cftchapaftersnum}{:}
\renewcommand{\cftchapnumwidth}{6em}


\begin{document}
\tableofcontents

 \chapter{First chapter}

.
.
. 

\appendix

\chapter{Appendix Name}

\end{document}
And it works wonders for adding the "Chapter" before the listing int he ToC. However, it also adds "Chapter" where it should say "Appendix", such that it actually says:

Chapter 1 - ....
Chapter 2 - ....
Chapter A - ....

instead of


Chapter 1 - ....
Chapter 2 - ....
Appendix A - ....

Any suggestions? I'm looking through the documentation, and I can't seen to find a way to do this.

Thanks!
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Adding "Chapter" and "Appendix" to ToC

Post by phi »

Try this:

Code: Select all

\makeatletter
\renewcommand*\cftchappresnum{\@chapapp\ }
\makeatother
chelos
Posts: 6
Joined: Mon Nov 17, 2008 10:30 pm

Re: Adding "Chapter" and "Appendix" to ToC

Post by chelos »

phi ...

I tried that, and while it seemed like a great idea, it did not work. Still have "Chapter A" in the ToC.
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Adding "Chapter" and "Appendix" to ToC

Post by phi »

Oh yes, I see the problem. This is an old one which I came across several times: the definition that is active during \tableofcontents matters, not the one that was active during \chapter. You have to write to the TOC file directory:

Code: Select all

\documentclass{report}
\usepackage{tocloft}
\renewcommand{\cftchapfont}{\bfseries}
\renewcommand{\cftchappagefont}{\bfseries}
\renewcommand{\cftchapaftersnum}{:}
\renewcommand{\cftchapnumwidth}{7em}
\makeatletter
\newcommand*\updatechaptername{%
	\addtocontents{toc}{\protect\renewcommand*\protect\cftchappresnum{\@chapapp\ }}
}
\makeatother

\begin{document}
\tableofcontents
\updatechaptername
\chapter{First chapter}
.
.
.
\appendix
\updatechaptername
\chapter{Appendix Name}
\end{document}
daleif
Posts: 199
Joined: Wed Nov 19, 2008 12:46 am

Re: Adding "Chapter" and "Appendix" to ToC

Post by daleif »

If you were using memoir (same author as tocloft) then it would have been as easy as

\renewcommand\cftchaptername{\chaptername~}
\renewcommand\cftappendixname{\appendixname~}
chelos
Posts: 6
Joined: Mon Nov 17, 2008 10:30 pm

Adding "Chapter" and "Appendix" to ToC

Post by chelos »

phi ...

I tried that, and now my ToC looks like:

1:_____Introduction
2:_____Chapter Name

.
.
.

A:_____Appendix Name

with large spaces between the number and chapter name, and nothing before the number.

EDIT: In the .toc file, I note that it adds

Code: Select all

\renewcommand *\cftchappresnum {Chapter\ }

\renewcommand *\cftchappresnum {Appendix\ }
in the respective places where \updatechaptername was used. Is this syntax correct?
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Re: Adding "Chapter" and "Appendix" to ToC

Post by phi »

I tested the code, and it works on my system (TeXLive 2007). Do you use exactly the same code?
Post Reply