[Tex/LaTex] Overriding commands

document-classes

How can I partially re-implement an existing command in Latex?

I have a \documentclass with:

\ProvidesClass{myclass}[My sample class]
\LoadClass{article}

When someone uses \subsection{}, I really want to treat it as if they wrote \subsection*{} (no section numbers). In my *.cls I've tried:

\renewcommand{\subsection}[1]{\subsection*}

But this just causes an infinite loop in Latex and I need to kill the process.
I've also tried:

\let\subsection\subsection*

But this simply lets \subsection equal itself and then prints a *

Best Answer

LaTeX has a built-in mechanism for this. Each sectioning command is assigned a level

part −1
chapter 0
section 1
subsection 2
subsubsection 3
paragraph 4
subparagraph 5

A class might not have chapters, but for uniformity the levels are always the same.

You can decide up to what level sectional titles are numbered by setting the counter secnumdepth:

\setcounter{secnumdepth}{1}

will only number parts, chapters (if provided by the class) and sections.

There is a companion counter, tocdepth, that controls what sectioning levels are listed in the table of contents.

Of course, users can override your setting, if they know about secnumdepth. You can't do much about that.

Even if you decide to go the “redefine” way with

\let\latex@subsection\subsection
\renewcommand{\subsection}{\latex@subsection*}

a user can still do

\makeatletter
\let\subsection\latex@subsection
\makeatother

and override your decision.