0

I am creating a report using the python package pylatex, but essentially it is using LaTeX.

I get the following errors when adding the table of contents:

 l.42 \tableofcontents
                     %
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

! Undefined control sequence.
\contentsline ...noexpand \Hy@tocdestname {#4}}\x 
                                                  \ifx \Hy@tocdestname \ltx@...
l.42 \tableofcontents
                     %
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

! Extra \or.
\contentsline ...ame }{#2}\hyper@linkend }{#3}\or 
                                                  \def \Hy@temp {#3}\ifx \Hy...
l.42 \tableofcontents
                     %
I'm ignoring this; it doesn't match any \if.

! Undefined control sequence.
\contentsline ...up \Hy@safe@activestrue \edef \x 
                                                  {\endgroup \def \noexpand ...
l.42 \tableofcontents
                     %
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

! Undefined control sequence.
\contentsline ...noexpand \Hy@tocdestname {#4}}\x 
                                                  \ifx \Hy@tocdestname \ltx@...
l.42 \tableofcontents
                     %
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

! Extra \or.
\contentsline ...ame }{#2}\hyper@linkend }{#3}\or 
                                                  \def \Hy@temp {#3}\ifx \Hy...
l.42 \tableofcontents
                     %
I'm ignoring this; it doesn't match any \if.

! Undefined control sequence.
\contentsline ...up \Hy@safe@activestrue \edef \x 
                                                 {\endgroup \def \noexpand ...

and later:

(That makes 100 errors; please try again.)
!  ==> Fatal error occurred, no output PDF file produced!
Transcript written on BMP120_2008_QC_report.log.

It seems that this error can occur when using the hyperref package and the tocdepth is set to a value less than the depth of the table of contents. When this happens, LaTeX may be unable to generate the table of contents and the error message you described may be produced.

However, the error persists if I comment out the line that includes the hyperref package. I also tried increasing the TOC depth in the preamble: \setcounter{tocdepth}{3}%

Error goes away if commenting out \tableofcontents

I have added snippets below (the doc is too large to include all code).

Here is the beginning of my LaTeX script:

\documentclass{article}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
\usepackage{geometry}%
\geometry{tmargin=2cm,lmargin=2cm}%
\usepackage{rotating}%
\usepackage{sectsty}%
\usepackage[compact, big]{titlesec}%
\usepackage[section]{placeins}%
\usepackage{graphicx}%
\usepackage{hyperref}%
%
\setcounter{tocdepth}{3}%
%
\begin{document}%

I create a title page like this:

\begin{titlepage}% text \end{titlepage}%

and L.42:

\tableofcontents%
\newpage%

I am using hyperref like this:

\fontsize{8}{12}%
\selectfont%
\begin{tabular}{|l|p{10cm}|}%
\hline%
text \& text&text\\%
\hline%
text  .....  text&\href{link.html}{website\_link}\\%
\hline%
text  .....  text&\href{link.html}{website\_link}\\%
\hline%
\end{tabular}%
\fontsize{15}{12}%
4
  • 1
    I am not getting the same issue with your code. It looks like you are not providing enough info. Mar 1, 2023 at 3:47
  • Thanks for checking. I have added some additional code. The document is so large it's not possible to include all code. Mar 1, 2023 at 5:22
  • you only showed the end of the undefined command message not the start of it which shows which command is undefined Mar 1, 2023 at 7:16
  • It's wrong to load both sectsty and titlesec. What would be the purpose of \fontsize{15}{12}?
    – egreg
    Mar 1, 2023 at 10:36

1 Answer 1

2

Ok I solved this.

Adding \setcounter{tocdepth}{4}% worked instead of \setcounter{tocdepth}{3}%, I guess because I was using \section, \subsection and \subsubsection.

This error can occur when using the hyperref package and the tocdepth is set to a value less than the depth of the table of contents. When this happens, LaTeX may be unable to generate the table of contents and the error message I got was produced.

1
  • this answer is misleading you can set tocdepth to any value. if you change the setting or add or remove hyperref you may need to remove generated toc from a previous run and allow latex to generate a new one. Mar 1, 2023 at 7:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .