Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a command like \title in LaTeX?

Tags:

I am writing a document class for LaTeX and I want it to be generic. In this document class I redefine the \maketitle command to display a custom title page, and here I want to display some information, like the title, author, etc., but also some other informations. Here is how I display the title:

{\LARGE{\bf \@title}}\\

I'd like to create a new command that works similarly to \title or \author, how can I do that?

like image 661
Kjir Avatar asked May 14 '10 12:05

Kjir


1 Answers

If you look at latex.ltx you can see that \title is defined as follows:

\def\title#1{\gdef\@title{#1}}
\def\@title{\@latex@error{No \noexpand\title given}\@ehc}

Those are low-level TeX commands. \title is a command that redefines \@title to expand to the argument given to \title. In more modern LaTeX commands your own definition could look like this:

\newcommand\foo[1]{\renewcommand\@foo{#1}}
\newcommand\@foo{\@latex@error{No \noexpand\foo given}\@ehc}

It's better to use \PackageError or \ClassError to show the error message. Or, if you want \foo to be optional and be empty by default:

\newcommand\foo[1]{\renewcommand\@foo{#1}}
\newcommand\@foo{}

If this is not inside a package, you'll have to put it between \makeatletter and \makeatother because of the @ signs.

like image 63
Thomas Avatar answered Oct 05 '22 23:10

Thomas