## LaTeX forum ⇒ Page Layout ⇒ Adding a heading to the table of contents Topic is solved

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Posts: 19
Joined: Sun Nov 20, 2016 8:12 pm

Hi,

I need to add a heading to the TOC as the image below:

table-of-contents.png (4 KiB) Viewed 752 times

Thanks

Stefan Kottwitz
Posts: 8946
Joined: Mon Mar 10, 2008 9:44 pm

you can use this:

\usepackage{afterpage}\newcommand*{\headline}{%  \hbox to \linewidth{\underline{Title}\hfill\underline{Page}}  \bigskip\afterpage{\headline}}\addtocontents{toc}{\protect\headline}

Stefan

Posts: 19
Joined: Sun Nov 20, 2016 8:12 pm
Stefan Kottwitz wrote:Hi nadaa,

you can use this:

\usepackage{afterpage}\newcommand*{\headline}{%  \hbox to \linewidth{\underline{Title}\hfill\underline{Page}}  \bigskip\afterpage{\headline}}\addtocontents{toc}{\protect\headline}

Stefan

Perfect!. Thank you so much, Stefan.
There is a problem, this is repeated on all the pages, and multiple empty pages were created!?

I still need to set the font size for "Table of Content" to be 16px and centered.
chapter font size within the TOC to be 14px bold and section 12px bold.
How can I do that?
I use a report class doc.

Stefan Kottwitz
Posts: 8946
Joined: Mon Mar 10, 2008 9:44 pm
Please post a minimal but compilable example of your code. It takes too much time to imagine and create some code, that may be similar to what you wrote, or not... no way to test without your code.

Stefan

Posts: 19
Joined: Sun Nov 20, 2016 8:12 pm
Stefan Kottwitz wrote:Please post a minimal but compilable example of your code. It takes too much time to imagine and create some code, that may be similar to what you wrote, or not... no way to test without your code.

Stefan

Hi Stefan. I am working on a large document (thesis). My question is about formatting table of content. What shall I show?
The previous solution you gave works, but the heading was copied to the whole doc, besides, it adds multiple empty pages?!

Stefan Kottwitz
Posts: 8946
Joined: Mon Mar 10, 2008 9:44 pm
Then use it this way:

\usepackage{afterpage}\newif\iftoc\newcommand*{\headline}{%  \hbox to \linewidth{\normalfont\underline{Title}\hfill\underline{Page}}  \bigskip\afterpage{\iftoc\headline\fi}}\addtocontents{toc}{\protect\headline}...\begin{document}...{\toctrue\tableofcontents}

Stefan

Posts: 19
Joined: Sun Nov 20, 2016 8:12 pm
Stefan Kottwitz wrote:Then use it this way:

\usepackage{afterpage}\newif\iftoc\newcommand*{\headline}{%  \hbox to \linewidth{\normalfont\underline{Title}\hfill\underline{Page}}  \bigskip\afterpage{\iftoc\headline\fi}}\addtocontents{toc}{\protect\headline}...\begin{document}...{\toctrue\tableofcontents}

Stefan

Thanks, I tried it, but the same.
When I delete \toctrue, the heading appears on the first page of TOC only, not on the rest of TOC pages!

Stefan Kottwitz
Posts: 8946
Joined: Mon Mar 10, 2008 9:44 pm
nadaa wrote:Thanks, I tried it, but the same.

Are you sure that you did not omit the braces? They are required, they keep the change local. Either

{\toctrue\tableofcontents}

or

\toctrue\tableofcontents\tocfalse

should work.

Stefan

Posts: 19
Joined: Sun Nov 20, 2016 8:12 pm
Stefan Kottwitz wrote:
nadaa wrote:Thanks, I tried it, but the same.

Are you sure that you did not omit the braces? They are required, they keep the change local. Either

{\toctrue\tableofcontents}

or

\toctrue\tableofcontents\tocfalse

should work.

Stefan

Yes, it works. I forgot the brackets.
Thank you
How I can learn all these stuff. I have been using Latex for a while, but I can't solve problems without asking!. Any suggestion?.

Stefan Kottwitz
Posts: 8946
Joined: Mon Mar 10, 2008 9:44 pm
Knowing all stuff is not so important. A key is, to know where to look for (documentation), work with source code of packages, just try fixing things. There are good ways to know, such as why and how to build a minimal working example - once one understood that, he easily tracks down problems and either fixes them himself with the hacked-down code piece, or post the small code sample to a LaTeX forum such as here. Once a problem can be reproduced and tested, it's usually easy to fix for very experienced people, even without knowing details before.

Experience comes over time. You can simply ask about any LaTeX problem here, that's the purpose of the forum. Once we talked about it and posted a fix, other people have a good chance to find it via google. So, asking questions helps other people too. Reading answers then also adds knowledge over time. Often solutions are quick hacks that show how to manually fix things.

Stefan