21

Is it possible to create a TOC (table of contents) with \sections and \subsections, but without the page numbers on the right hand side? I know this defeats the purpose of a TOC but for knowledge's sake I want to know if LaTeX gives this flexibility.

0

2 Answers 2

18

LaTeX is flexible, especially if you are able to read sources to redefine macros.

  1. You could redefine \addcontentsline:

    \renewcommand*{\addcontentsline}[3]{\addtocontents{#1}{\protect\contentsline{#2}{#3}{}}}

  2. Alternatively, you could redefine \contentsline, like Herbert did, or this way:

    \renewcommand*{\contentsline}[3]{\csname l@#1\endcsname{#2}{}}

  3. If you wish to remove page numbers for a certain level, such as subsection, redefine \l@subsection{title}{page number}. Perhaps you would like to do that, because dotted toc lines, used for certain levels, are not meaningful without the page number.

  4. You could even redefine \thepage locally to be empty, in a group together with \tableofcontents.

Note: If you use the hyperref package, solutions should be modified since that package makes modifications to TOC macros for hyperlinking.

For example, the first solution would need an extra parameter for the hyperlink label. This fourth parameter can be left empty if you do not wish to have a hyperlink for that TOC entry.

\renewcommand*{\addcontentsline}[4]{\addtocontents{#1}{\protect\contentsline{#2}{#3}{}{#4}}}

5
  • @Stefan-Yes I am using Hyperref package. How to modify the solution.
    – Aku
    Jan 20, 2011 at 1:26
  • @Aku: hyperref uses one further argument for the link name. I'll check that in the evening (UTC) when I will have access to LaTeX.
    – Stefan Kottwitz
    Jan 21, 2011 at 7:18
  • Could you please check the solutions for hyperref? Also, how can I put something (one time only) that is formatted like a chapter* in the toc, but without the page number?
    – fabikw
    Feb 2, 2011 at 23:22
  • 1
    @StefanKottwitz: Thanks for providing this great answer! Would you be able to indicate how to modify these solutions for documents that are using hyperref? I also have the same question as the above comment, namely, how can I (for instance) have some sections appear with page numbers, and others without?
    – frustrated
    Oct 2, 2012 at 3:13
  • 1
    The last command (provided for hyperref) does not work. The error is ! Incomplete \iffalse; all text was ignored after line 54. <inserted text> \fi
    – Colas
    Dec 8, 2016 at 15:09
18
\documentclass{article}

\usepackage{hyperref}
\let\Contentsline\contentsline
\renewcommand\contentsline[3]{\Contentsline{#1}{#2}{}}
\begin{document}
\tableofcontents

\section{foo}
\section{bar}

\end{document} 
3
  • This seems to work correctly with hyperref.
    – Richard
    Mar 24, 2013 at 16:23
  • @Richard: but it must beloaded before the redefinition of \contensline
    – user2478
    Mar 24, 2013 at 17:18
  • 1
    Seems to work correctly without hyperref Oct 31, 2014 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.