1

Is there a way to generate a table of contents without showing it?

I am using sections and a table of contents to generate an outline in a pdf document, but I would prefer the table of contents not be shown.

Is there a way to solve this?

My document at the moment:

\documentclass[letterpaper,11pt]{article}
\usepackage[empty]{fullpage}
\usepackage{kpfonts}
\usepackage[bookmarksopen=true, bookmarksdepth=5]{hyperref}
\setcounter{tocdepth}{5}
\setcounter{secnumdepth}{5}
\hypersetup{
    bookmarksnumbered=true,     
    bookmarksopen=true,         
    bookmarksopenlevel=1, 
    pdfpagemode=UseOutlines
}
\setlength{\parskip}{\baselineskip}%
\setlength{\parindent}{0pt}%
\begin{document}
\tableofcontents
\newpage
\setcounter{secnumdepth}{0}
\section{Foo}
\subsection{Bar}
\setcounter{secnumdepth}{0}
\section{Other section}
\end{document}
  • Um, %\tableofcontents? Or am I misunderstanding? – Teepeemm Feb 19 at 2:35
  • @Teepeemm commenting out the table of contents will mean no table of contents is generated. I want to generate one, as I need it to display outlines in the PDF file. I just don't want the table of contents to be visible. If there is a way to have outlines in the PDF file without generate a table of contents, that would work also. – Ken Rogers Feb 19 at 2:38
  • Welcome to TeX.SE. – Peter Grill Feb 19 at 4:36
  • @Teepeemm: Please post your comment as an aswer as that is exactly what the OP wanted as per the commens in my answer. – Peter Grill Feb 19 at 4:39
0

By default, hyperref writes the toc entries to the pdf outline, so leaving out \tableofcontents will work just fine:

\documentclass{article}
\usepackage{hyperref}
\begin{document}
\setcounter{secnumdepth}{0}
\section{Foo}
\subsection{Bar}
\section{Other section}
\end{document}

The complicating thing is that for various TeX reasons, anytime you're dealing with a table of contents, you might need to compile two (or three or four) times before being done.

|improve this answer|||||
  • Thank you for your answer. – Ken Rogers Feb 19 at 15:33
0

Not sure exactly what you mean by display the outlines, but here is a solution hides the content of the TOC

Original

enter image description here

After Applying \RedefineToc

enter image description here

If you want just a blank page you can instead comment out the following line

\renewcommand\contentsline[4]{}%

Code:

\documentclass[letterpaper,11pt]{article}
\usepackage[empty]{fullpage}
\usepackage{kpfonts}
\usepackage[bookmarksopen=true, bookmarksdepth=5]{hyperref}
\setcounter{tocdepth}{5}
\setcounter{secnumdepth}{5}
\hypersetup{
    bookmarksnumbered=true,     
    bookmarksopen=true,         
    bookmarksopenlevel=1, 
    pdfpagemode=UseOutlines
}
\setlength{\parskip}{\baselineskip}%
\setlength{\parindent}{0pt}%

\newcommand*{\RedefineToc}{%
    \let\OldContentsLine\contentsline
    \renewcommand\contentsline[4]{\OldContentsLine{##1}{Xx}{##3}{##4}}%
   %\renewcommand\contentsline[4]{}% <-- Uncomment to get blank page
}%

\begin{document}
{\RedefineToc%
\tableofcontents
}%
\newpage
\setcounter{secnumdepth}{0}
\section{Foo}
\subsection{Bar}
\setcounter{secnumdepth}{0}
\section{Other section}
\end{document}
|improve this answer|||||
  • Thank you for your answer. For me this displays a page that says Contents, with no TOC entries, even after uncommenting the line you say will give a blank page. Is there a way to just have no page instead of a blank page? By outlines I mean the navigation entries shown in a PDF view, here is a screengrab showing the chrome PDF viewer: imgur.com/a/CBXuhcl – Ken Rogers Feb 19 at 4:21
  • If you just want the bookmarks, then commenting out the \tableofcontents won't print the TOC and yet still provide the bookmarks. You just need to run it twice (just like you have to do with \tableofcontents) to get the bookmarks. – Peter Grill Feb 19 at 4:35
  • Oh that worked a treat! If you want to make that the answer I will accept...it never would have occurred to me to comment it and run it twice. I don't understand why that works at all, but if it's a silly question happy to delete the question instead. – Ken Rogers Feb 19 at 4:38
  • @KenRogers: No worries. Things with LaTeX are often not obvious, so don't be afraind to ask. I have asked quite a few embarrasing ones on this site, so I undestandl. But you won't be the last person to have this issue. I have asked Teepeemm to post an answer as that comment actually solved the problem for you, not me. – Peter Grill Feb 19 at 4:41
  • \usepackage[bookmarks]{hyperref} produces the bookmarks, using the \section{...} etc commands. \tableofcontents is not required for the bookmarks. – Cicada Feb 19 at 13:35

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.