# how to check in latex whether the table of content is empty or not before added to the document

I have created a simple template for someone with the table of content added to the first page as follow

\begin{document}
% title and date here
\maketitle
\tableofcontents
\indent \newline
%
\end{document}


But I figure out that someone don't add anything to the table of contents but there will be a word "Contents" shown in the document even the table is empty. Is that anyway I check in advance whether the table of contents is empty or not so to decide what to do next? Thanks.

-
in the standard classes \tableofcontents will print "Contents" (or rather \contentsname) whatever what. So it must be patched. Then it is not enough to check if the .toc file exists, as for example Babel writes to the .toc file. One could imagine patch \addcontentsline so that on its first use it writes something to the aux file which we can test in the \tableofcontents patch. Or patching \contentsline itself. In brief, this is a bit complicated. –  jfbu Jan 23 at 9:00
My intuition tells me that you have to check if the the .toc file is empty or not. I'm not an expert of conditional statements in latex but maybe you can file some information here –  Pouya Jan 23 at 9:03
@Pouya as I said Babel writes to the .toc file. \select@language {french} for example. So it is not enough to check if the .toc file exists, and if it exists if it is empty. –  jfbu Jan 23 at 9:06
Thanks for your replies. To Pouya, since the latex will be run via php calling on the server side. I am wondering if that's possible for some case the .toc has not been cleaned so leads to a misleading judge? Well, buy the way, I don't know how to check the file remotely on the server too :( –  user1285419 Jan 23 at 9:07
About what I said about \addcontentsline and \contentsline they are replaced by hyperref, I think in the preamble (I forgot). So the patching can be done at begin document, I think. I would go for \contentsline. –  jfbu Jan 23 at 9:11

Note though that there are some nice packages dealing with TOC's which could well be upset by the following. Besides, I am assuming a standard \tableofcontents command as in the article class.

\documentclass{article}
\usepackage[french]{babel} % for testing
\usepackage{hyperref}      % for testing

\newif\iftoctitledone

\makeatletter

\iftoctitledone\else
\section *{\contentsname \@mkboth {\MakeUppercase \contentsname }
{\MakeUppercase \contentsname }}\fi
\toctitledonetrue}

\AtBeginDocument{%
\let\mygoodoldcontentsline\contentsline
\renewcommand\tableofcontents{\@starttoc {toc}}
}
\makeatother

\begin{document}

\tableofcontents
%  \section{test}
nothing
\end{document}


The same in another style, slightly more elegant.

\documentclass{article}
\usepackage[french]{babel} % for testing
\usepackage{hyperref}      % for testing

\makeatletter

\section *{\contentsname \@mkboth {\MakeUppercase \contentsname }
{\MakeUppercase \contentsname }}%

\AtBeginDocument{%
\let\mygoodoldcontentsline\contentsline
\renewcommand\tableofcontents{\@starttoc {toc}}
}
\makeatother

\begin{document}

\tableofcontents
%    \section{A}
% a

% \section{B}

% b

% \subsection{C}

nothing
\end{document}

-
It works just pefect. Thanks a lot –  user1285419 Jan 24 at 3:03