添加目录时产生的错误

添加目录时产生的错误

我正在使用 python 包 pylatex 创建报告,但本质上它是使用 LaTeX。

添加目录时出现以下错误:

 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 ...

然后:

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

似乎在使用 hyperref 包并且将 tocdepth 设置为小于目录深度的值时会出现此错误。发生这种情况时,LaTeX 可能无法生成目录,并且可能会产生您描述的错误消息。

但是,如果我注释掉包含 hyperref 包的行,错误仍然存​​在。我还尝试增加序言中的目录深度:\setcounter{tocdepth}{3}%

如果注释掉,错误就会消失\tableofcontents

我在下面添加了代码片段(文档太大,无法包含所有代码)。

这是我的 LaTeX 脚本的开头:

\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}%

我创建了一个这样的标题页:

\begin{titlepage}% 文本 \end{titlepage}%

和 L.42:

\tableofcontents%
\newpage%

我像这样使用 hyperref:

\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}%

答案1

好的,我解决了这个问题。

我猜添加\setcounter{tocdepth}{4}%是有效的,因为我正在使用、和。\setcounter{tocdepth}{3}%\section\subsection\subsubsection

使用 hyperref 包时,如果将 tocdepth 设置为小于目录深度的值,则会出现此错误。发生这种情况时,LaTeX 可能无法生成目录,并会产生我收到的错误消息。

相关内容