6

I am trying to figure out how to use a variable in a class.

From another question (Macro for Declaring Variables in Class Files) I got started. However, I find myself in need of using four lines or rather unclear code. This is what I have:

\let\@headertitle\relax
\def\headertitle#1{\def\@headertitle{#1}}
\newcommand{\printheadertitle}{\@headertitle}
\printheadertitle{} % This will print the string in the variable

It works, but doesn't look good. I simply want to use \headertitle{string} in my document, and be able to somehow call it (e.g. \headertitle{} within my class. Can I simplify this?

1
11

The first and fourth lines are not doing anything very useful, so you can simplify by removing them, then:

If your class or package code has

\def\headertitle#1{\def\@headertitle{#1}}
\newcommand{\printheadertitle}{\@headertitle}

then your document can go

\headertitle{hello world}

and at any point later

\printheadertitle

will produce hello world

This will mean that you get an error if \printheadertitle is used without defining a title. If you want a default title to be silently accepted add in your class code

\headertitle{}

or some default other than empty.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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