1

I'm creating my own class based on article and I would like to add a parameter subtitle which users of my class can set similar to title, author and date, e.g.,

\documentclass{myclass}
\title{My title} % This sets `\@title` to `My title` somehow.
\subtitle{Some subtitle} % I want this to set `\@subtitle` to `Some subtitle`.

\begin{document} \makebanner % this command uses @title and @subtitle under the hood \end{document}

Looking at the source code of article.cls, the title command seems to be defined as follows.

\global\let\@title\@empty
\global\let\title\relax

This doesn't really make sense to me, so I think I'm missing something. Also, adding \global\let\@subtitle\@empty and \global\let\subtitle\relax doesn't work. What is a proper way to do this? How does article.cls do it?

Safron
  • 360
  • 1
  • 9
  • 1
    \global\let\@subtitle\@empty and \protected\long\def\subtitle#1{\gdef\@subtitle{#1}} does the trick. The \global\let\title\relax is executed inside of \maketitle. The real definition of \title is done by the kernel and not part of article.cls. – Skillmon Jan 25 '22 at 17:55
  • Of course. The reason is in the comment above: the LaTeX kernel defines \title, article.cls just uses an existing definition. – Skillmon Jan 25 '22 at 18:38
  • @Skillmon Okay, that explains a lot. You may add an answer if you want. – Safron Jan 25 '22 at 18:43

1 Answers1

3

In ltsect.dtx the LaTeX kernel makes the following definitions:

\DeclareRobustCommand\title[1]{\gdef\@title{#1}}
\DeclareRobustCommand*\author[1]{\gdef\@author{#1}}
\DeclareRobustCommand*\date[1]{\gdef\@date{#1}}
\def\@title{\@latex@error{No \noexpand\title given}\@ehc}
\def\@author{\@latex@warning@no@line{No \noexpand\author given}}
\def\@date{\today}

The standard classes (like article.cls) don't define those commands, they use the existing definitions. The "definitions" you saw are not their definitions, just the redefinitions done to make them unavailable once \maketitle was used.

For this reason, the definitions you could use would be:

\let\@subtitle\@empty % default value
\protected\def\subtitle#1{\gdef\@subtitle{#1}}

(\protected\def results in similar behaviour to \DeclareRobustCommand, pick whichever you prefer)

Skillmon
  • 60,462