# Setting initial value of a storage macro

When I create a "storage macro" I often initialize it to be empty with

\newcommand{\@mymacro}{}


Instead of initializing it to empty I could do \let\@mymacro\relax. In letter.cls storage macros (is there a better term) are defined by

\newcommand{\mymacro}[1]{\def\@mymacro{#1}}
\mymacro{}


In this method one wouldn't even have to initialize \@mymacro and could leave out the \mymacro{}. This would lead to potentially cryptic error messages and in the LaTeX kernel the storage macros \@title and \@author are initialized as

\def\@title{\@latex@error{No \noexpand\title given}\@ehc}
\def\@author{\@latex@warning@no@line{No \noexpand\author given}}


to give less cryptic messages. Is there a proper way to initialize storage macros?

-
Do you want to use a keyval syntax? –  Marco Daniel May 10 '13 at 13:39
It mostly depend on the purpose of \mymacro. If it's mandatory or recommended to use it in the document (like \title and \author), then initialize it to give a message. Otherwise do as you prefer: the first and the second way are pretty much alike, but probably I'd say \let\@mymacro\@empty. –  egreg May 10 '13 at 13:42
@MarcoDaniel I guess I would like to know how to initialize if I wasn't using keyval, if I was, and if I wasn't sure if I would be. –  StrongBad May 10 '13 at 13:44
"storage macros (is there a better term)" - they are called "declarations" in Lamport's original LaTeX book. (See p.22) –  Thruston May 10 '13 at 13:53

Note the definition

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


is really picking up a different kind of error.

If you have



then you get an error on first use as you can not redefine an undefined \@mymacro

As you say, you can use \def to avoid distinguishing the first case.

But whether you use \def or \renewcommand If you initialise the internal macro to \relax or {} then if you do not use the command in the document then this is siletly accepted with the default value being the initial value.

LaTeX used \def (\gdef actually) \def\title#1{\gdef\@title{#1}} but most classes want it to be an error to use \maketitle without having previously used \title hence the initialisation to the error message you showed.

It is hard to give global rules about this. It is not always possible (or desirable) to make initial values be an error. \baselinestretch for example is initialised to 1 as it needs to be usable in a tex primitive length multiplication, and normally it doesn't need to be set by the user, so making it an error if it wasn't set would not be so useful.

It is probably cleanest to set the default value via the advertised setting command so use

\title{\@latex@error{No \noexpand\title given}\@ehc}


rather than what latex actually uses

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


That way, if you decide to use a more complicated internal structure (such as a LaTeX3 property list or whatever) behind the \title command things will still work as you have not assumed the internal data is stored in a particular way. (This would mean that you always accessed the data via a defined accessor as well, if you just use \@title directly obviously it will break if you change the internals.

-