未定义控制序列。\tableofcontents

未定义控制序列。\tableofcontents

我把几个例子拼凑在一起,得到了一个差不多能用的样本。我的最后一个问题是消息

Undefined control sequence. \tableofcontents

当我尝试编译以下代码时出现此信息。

\documentclass[a4paper,11pt,naustrian]{article}
\usepackage[margin=1.85cm]{geometry} % for margins on a A4paper
\usepackage[tracking=true,factor=1100,stretch=10,shrink=10,final]{microtype} 
\usepackage{fontenc}
\usepackage[explicit]{titlesec}
\usepackage{xcolor}
\usepackage{lipsum}
\usepackage{fontspec}
\setmainfont{Liberation Sans}
\usepackage{parskip} 
\usepackage{keyval}

\definecolor{coi_gray}{RGB}{136,155,169}

\makeatletter
\newcommand\boxSection[2]{
\parbox{\linewidth}{%
    \vspace{.5ex}%
    \raggedright
    #1 {\textls*[50]{#2}\endgraf}
    \addvspace{.5ex}
    \ifx\mosquito@subtitle\@empty\else
    \endgraf{\sectionsubtitlefont\mosquito@subtitle\endgraf}
    \fi
    \addvspace{.5ex}
}%
}

\titleformat{\section}
{\normalsize\bfseries\color{gray}}{}{0em}
{\boxSection{\thesection}{#1}}

\titleformat{\subsection}
{\normalsize\bfseries\color{gray}}{}{0em}
{\boxSection{\thesubsection}{#1}}

\titleformat{\subsubsection}
{\normalsize\bfseries\color{gray}}{}{0em}
{\boxSection{\thesubsection}{#1}}


\newcommand\sectionsubtitlefont{\normalfont\sffamily}
\define@key{mosquito}{subtitle}{\def\mosquito@subtitle{#1}}

\newcommand\sectionLvl[3][]{%
    \setkeys{mosquito}{subtitle={},#1}
    \ifcase#2\relax\expandafter\chapter\or
    \expandafter\section\or
    \expandafter\subsection\or
    \expandafter\subsubsection\else
    \def\next{\@level{#2}}\expandafter\next
    \fi {#3}
    \ifx\mosquito@subtitle\@empty\else
    \endgraf
    \fi
}
\newcommand{\@level}[1]{%
    \@startsection{level#1}
    {#1}
    {\z@}%
    {-3.25ex\@plus -1ex \@minus -.2ex}%
    {2.3ex \@plus .2ex}%
    {\normalfont\normalsize\bfseries\color{coi_gray}}}

\newdimen\@leveldim
\newdimen\@dotsdim
{\normalfont\normalsize
    \sbox\z@{0}\global\@leveldim=\wd\z@
    \sbox\z@{.}\global\@dotsdim=\wd\z@
}
\def\l@subsubsection{\@dottedtocline{3}{3.8em}{3.1em}} % 3.8+3.1em=6.9em
\makeatother

\setcounter{secnumdepth}{10}
\setcounter{tocdepth}{10}

\usepackage{kantlipsum}

\begin{document}
    \tableofcontents
    \sectionLvl[subtitle={Test Subtitle}]{1}{abc}
\kant[1-2]
    \sectionLvl[subtitle={Second level}]{2}{second section}
    \kant[5-6]
    \sectionLvl[subtitle={addtional}]{3}{third section}
%       \sectionLvl{3}{third section}
%       subtitle
\kant[8]
\end{document}

有没有简单的方法可以解决这个问题?

答案1

确切的错误代码是

! Undefined control sequence.
<argument> ...tionsubtitlefont \mosquito@subtitle 
                                                  \endgraf } \fi \addvspace ...
l.82     \tableofcontents

并且是不是 \tableofcontents反而\mosquito@subtitle

问题在于\tableofcontents以 开头\section*,而按照您的定义,这将执行\boxSection,这涉及(仍未定义的)控制序列。只需添加

\def\mosquito@subtitle{}

在你的序言中的某个地方(最好是在之前\define@key)。

fontenc无关:如果您使用 Unicode 引擎,则不要加载。

相关内容