1

I stitched several examples together and got a more or less working sample. My final problem is the message

Undefined control sequence. \tableofcontents

which appears when I try to compile the following code.

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

Is there an easy way to fix this problem?

  • The undefined macro is \mosquito@subtitle. – campa Jan 14 at 13:59
  • uhu.... but how to fix it? Its for the subtitle which shoudn't appear in the title. – LeO Jan 14 at 14:01
1

The exact error code is

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

and is not \tableofcontents but rather \mosquito@subtitle.

The trouble is that \tableofcontents starts with a \section*, and by your definitions this executes \boxSection, which involves the (still undefined) control sequence. Just add

\def\mosquito@subtitle{}

somewhere in your preamble (ideally just before \define@key).

Unrelated: don't load fontenc if you are using a Unicode engine.

  • Thx for the solution. I didn't see the output since I'm working with TeXStudio. I think need to make a follow up since the command structure of LaTeX seems (little bit) strange. – LeO Jan 14 at 15:36
  • Yeah, editors tend to mask error messages; in doubt always compile from the command line. – campa Jan 14 at 15:38

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.