I'm designing a new LaTeX class, and trying to write a convenient helper command for declaring new 'variables' for use in the class.

Specifically, say I want to use a variable called \foo. I'd like an author to be able to write \foo{bar}, and then be able to use \@foo in the class file (which would itself expand to bar. This can be accomplished with the following code:


and the author can define it with \foo{bar}. I want to create a helper command that allows the variable declaration to be done with a command like \DeclareAuthorVariable, which I would use in the LaTeX class file to declare variables. For example, I would like to be able to write \DeclareAuthorVariable{foo} in the class file instead of the ugly two line mess above. I've tried a number of different approaches, but I'm having trouble figuring out how to do this, since the commands that I'm trying to replace themselves involve arguments. I'm wondering if anyone knows how to do this or if it isn't possible? Thanks!

(Also, apologies, I'm very new to LaTeX class design!)

3 Answers 3


I'd add also some functions. You can declare a variable to be mandatory or optional; for instance title might be mandatory, while subtitle only optional. I propose the syntax

\DeclareAuthorVariable*{title}   % mandatory
\DeclareAuthorVariable{subtitle} % optional

Here is the code. Change the ting prefix to what suits you best.

% Do the branching between * and normal version

% The main command; the internal version of \foo is \ting@foo
% The macro \ting@foo is initialized to give an error or an info
% message when used, so if the user doesn't provide a value for a
% mandatory variable, we'll catch the issue
% The error and info messages
    {Missing value for mandatory variable
    {You have to provide a value with
    {Missing value for optional variable

%%% Define two variables

If you need to branch according to a variable having been given a value or not, you can modify \ting@DeclareAuthorVariable:


and add


so that you can say something like


(Here I assume that the title page will be set under \centering; the subtitle or, if missing, a dash would be printed.)

  • 2
    Wow, thanks so much! This is not only an excellent answer in its own right, but provides great insight into the correct stylistic and robustness concerns in package design. Commented Jan 15, 2013 at 14:12
  • One question though, why do you end the intermediate lines in your functions with %? Commented Jan 15, 2013 at 14:21
  • @MichaelTingley Most of these % are irrelevant here; but it's a good habit to put them where a space might creep in. You don't need them between arguments to a macro, but only at the end. So no % is needed after \ClassInfo{ting} because TeX will be looking for the next argument to \ClassInfo, so skipping spaces; after the second argument to \ClassInfo it's good to have one.
    – egreg
    Commented Jan 15, 2013 at 14:24
  • Any advice for how to check if one of these optional variables is defined? Thanks Commented Jan 16, 2013 at 13:23
  • @MichaelTingley Are you meaning a check that \DefineAuthorVariable{foo} doesn't redefine an existing \foo macro?
    – egreg
    Commented Jan 16, 2013 at 15:37







> \foo=macro:
#1->\@namedef {@foo}{#1}.
l.7 \show\foo

> \@foo=macro:
l.11 \show\@foo

No pages of output.

You may want to study the KOMA-classes and their macro \newkomavar.

  • 1
    \newkomavar is only available in the scrlttr2 class
    – cgnieder
    Commented Jul 19, 2013 at 14:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .