2

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 *

  • 3
    what about just \setcounter{secnumdepth}{0} \setcounter{tocdepth}{0}? – touhami Feb 24 at 11:08
  • sorry, for subsection it's 1. – touhami Feb 24 at 11:15
  • There is no need to redefined macros here, as touhami mentions, just globally disable the section numbering for subsections and below. Setting secnumdepth to 1 tells LaTeX to number down to and including section, but not subsection and below – daleif Feb 24 at 11:15
  • In case you're interested, this explains why your macro doesn't work and Steven's does: Redefine marginpar with \renewcommand (it's basically the same question). – TivV Feb 24 at 11:16
4

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.

|improve this answer|||||
4
\documentclass{article}
\let\svsubsection\subsection
\renewcommand\subsection[1]{\svsubsection*{#1}}
\begin{document}
\section{Section Name}
\subsection{Subsection Name}
\end{document}

enter image description here

|improve this answer|||||
  • I just had to re-implement \includegraphics to include a default [max width=\textwidth] and this worked perfectly! – Stewart Feb 24 at 12:40

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.