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:
\let\@foo\relax
\def\foo#1{\def\@foo{#1}}
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!)
%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\ClassInfoit's good to have one. – egreg Jan 15 '13 at 14:24\DefineAuthorVariable{foo}doesn't redefine an existing\foomacro? – egreg Jan 16 '13 at 15:37\IsDefined{foo}{<true code>}{<false code>}, which would execute the relevant code segment contingent on whether the author provided a value for the optional variablefooor not. It doesn't seem like I can do this with\ifx\ting@foo\@empty, sincefoois initialized to give an error (or maybe I'm doing it wrong?). At the same time, I really like having the default error logging that you've provided. – Michael Tingley Jan 16 '13 at 15:47\newcommand*with an asterisk for the additional error-checking see here. Also things to have a look at, when writing more involved macros, are\expandafterand all its expansion-order affecting friends. I already use very similar constructs in my current project, but your answer provided valuable ideas on how I could improve on my class. – Valryne Jan 02 '15 at 00:53\newcommand*may indeed make sense when simple words are to be passed and not complete paragraphs. – egreg Jan 02 '15 at 00:55