Why does my custom sectioning command crash on second run when using etoc?

Finally I have narrowed down the problem to a custom definition of a sectioning command using titlesec in explicit mode. I suspect it has something to do with me not providing an adequate counter for etoc.

The first run compiles as usual. The second run results in:

! Missing number, treated as zero. \Etoc@level l.27 \section {etoc} ?

Code

\documentclass{article}
\usepackage{fontspec}
%\usepackage{tocloft} % I use this package too.
\usepackage[explicit]{titlesec}
\usepackage{etoc}
%\usepackage{hyperref} % I use this package too.

%\let\tableofcontents\etoctableofcontents %counteract the overwriting by tocloft of \tableofcontents done at  \begin{document} <--Do I need this?

% MINISECTION (Custom)
\titleclass{\minisec}{straight}[\paragraph]
\newcounter{minisec}
\titleformat{\minisec}[hang]{\normalsize\bfseries}{\theminisec}{0pt}{#1}% Change 0pt to a positive value once the representation for the counter has been established
\titlespacing*{\minisec}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\newcommand{\minisecautorefname}{minisec}
\setcounter{secnumdepth}{5}% minisecs will be numebered
\renewcommand\theminisec{}% Provisional empty definition for the counter representation
% Settings for the bookmarks and the ToC entries (change the second
% and third arguments of \@dottedtocline if minisecs should be in the ToC)
\makeatletter
\def\toclevel@minisec{5}
\def\l@minisec{\@dottedtocline{5}{3.8em}{3.2em}}
\makeatother

\begin{document}
\localtableofcontents
\section{etoc}
\minisec{Minisec Test}
\end{document}

• You're missing \etocsetlevel{minisec}{5}. – egreg Oct 25 '15 at 19:06
• @egreg Oh phew. This was driving me nuts. – Jonathan Komar Oct 25 '15 at 19:06

You're missing

\etocsetlevel{minisec}{5}


Full example; for avoiding that tocloft overwrites the definition of \tableofcontents, use the titles option.

\documentclass{article}
\usepackage{fontspec}
\usepackage[titles]{tocloft} % I use this package too.
\usepackage[explicit]{titlesec}
\usepackage{etoc}
\usepackage{hyperref} % I use this package too.

% MINISECTION (Custom)
\titleclass{\minisec}{straight}[\paragraph]
\newcounter{minisec}
\titleformat{\minisec}[hang]
{\normalsize\bfseries}
{\theminisec}
{0pt}% Change 0pt to a positive value once the representation for the counter has been established
{#1}
\titlespacing*{\minisec}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\newcommand{\minisecautorefname}{minisec}
\setcounter{secnumdepth}{5}% minisecs will be numebered
\renewcommand\theminisec{}% Provisional empty definition for the counter representation
% Settings for the bookmarks and the ToC entries (change the second
% and third arguments of \@dottedtocline if minisecs should be in the ToC)
\makeatletter
\def\toclevel@minisec{5}
\etocsetlevel{minisec}{5}
\def\l@minisec{\@dottedtocline{5}{3.8em}{3.2em}}
\makeatother

\begin{document}
\localtableofcontents
\section{etoc}
\minisec{Minisec Test}
\end{document}


• since etoc v1.07k [2014/03/06] it is not needed to pass option titles to tocloft. Either tocloft is loaded before etoc (as recommended by the etoc manual) and all goes through silently, else it is loaded after etoc and etoc will check if that was with option titles and if not correct the situation and emit a warning in the log (which possibly should only be an "info", not a "warning"). Because option titles has some other effects on vertical spacing (I forgot the details), the recommended order is tocloft (with or without titles option) then etoc. – user4686 Oct 27 '15 at 8:53