Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

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

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?

share|improve this question
    
Do you want to use a keyval syntax? –  Marco Daniel May 10 '13 at 13:39
1  
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
1  
"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

1 Answer 1

up vote 6 down vote accepted

Note the definition

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

is really picking up a different kind of error.

If you have

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

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.