# Titlesec: Redefinition of sectioning command leads to endless execution of latex

I'm new at TeX.SX and I hope I do everything correct.

To avoid answers like "Why do you do this?" I'd first like to describe briefly what I want: I'm developing a class based on tufte-book, which implements \subsubsection to throw an error. However, I'd like to provide the \subsubsection in my class. Furthermore, I'd like to be able to use the \titleformat macro to style the subsubsection since tufte-book uses titlesec. My first try was:

\documentclass{tufte-book}

\makeatletter
% Copy the definition from book.cls
\renewcommand\subsubsection{\@startsection{subsubsection}{3}{\z@}%
{-3.25ex\@plus -1ex \@minus -.2ex}%
{1.5ex \@plus .2ex}%
{\normalfont\normalsize\bfseries}}
\makeatother
% The following macro is called at the very end of titlesec.sty.
% If I uncomment it, the latex process runs forever.

%\titleclass{\subsubsection}{straight}[\subsection]

% Without the optional argument, it works...
% However, if you make the following line a comment...
\titleclass{\subsubsection}{straight}

% ... the access to subsubsection using the titlesec API doesn't work anymore
\titleformat{\subsubsection}{%
\fontsize{36}{42}\selectfont%
}{\thesubsubsection}{1em}{}[]

\begin{document}
\subsection{bar}
\subsubsection{foo}
\end{document}


After investigating titlesec.sty a bit, the MWE can be reduced to using the same call of \titleclass twice:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{titlesec}

% runs forever
%\titleclass{\subsubsection}{straight}[\subsection]

% works
\titleclass{\subsubsection}{straight}

% Does not make sense, but works
%\titleclass{\subsubsection}{straight}[\paragraph]

\titleformat{\subsubsection}{%
\fontsize{36}{42}\selectfont%
}{\thesubsubsection}{1em}{}[]

\begin{document}
\subsection{bar}
\subsubsection{foo}
\end{document}


While I believe that encountering an infinite loop is a good sign to have found a bug, I do neither find it nor I find any comments or threads on this issue in the net. I would agree that using the same \titleclass definition twice is something obscure, but it should at least result in an error instead of something infinite. Does anyone see the (potential) bug or, more likely, my mistake?

• It isn't to do with using it twice. It is to do with not trying to redefine it as itself, I think. \titleclass{\subsubsection}{straight}[\subsection] tries to create a new level below \subsection called \subsubsection. But there is already a level here. It is called \subsubsection. I haven't gone through the code but titlesec must insert a level when there is already a lower level. So it must try to also switch the level of the lower level, but that's \subsubsection. \titleclass is supposed to either just switch the style or create a new level. You are trying to recreate one. – cfr Feb 10 '15 at 1:55
• Welcome to TeX SE, by the way ;)! – cfr Feb 10 '15 at 1:55

Rather than copying the definition from book, you have to supply the forgotten one given by titlesec

\documentclass{tufte-book}

\makeatletter
\renewcommand{\subsubsection}{\ttl@straightclass{subsubsection}}
\makeatother
\titleformat{\subsubsection}[hang]
{\fontsize{36}{42}\selectfont}
{\thesubsubsection}
{1em}
{}
[]

\setcounter{secnumdepth}{2}

\begin{document}
\chapter{A}
\section{B}
\subsection{bar}
\subsubsection{foo}
\paragraph{C}
\end{document}


The problem in both cases is that

\titleclass{\subsubsection}{straight}[\subsection]


tries to move section levels under \subsection down before inserting the new \subsubsection level. A way to work around this is to define a level with a new name, e.g. ssubsection, and then put \subsubsection to be a copy of \ssubsection; because of optional arguments etc. you will need to use \LetLtxMacro from the package of the same name to do this:

\documentclass{tufte-book}

\usepackage{letltxmacro}

\titleclass{\ssubsection}{straight}[\subsection]

\newcounter{ssubsection}

\titleformat{\ssubsection}{%
\normalfont\normalsize\bfseries\selectfont%
}{\thessubsection}{1em}{}[]

\titlespacing{\ssubsection}{0pt}{*3.25}{*1.5}

\LetLtxMacro\subsubsection\ssubsection

\begin{document}
\subsection{Bar} Text.
\subsubsection{Foo} Text.
\end{document}

• I had this solution in mind, too. However, it's only a workaround. Using the solution posted by egreg is more clean, I think. – Patrick Happel Feb 10 '15 at 16:59