GeneralNew Command to define a Variable

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
bumbolol
Posts: 4
Joined: Tue Jan 24, 2012 3:40 pm

New Command to define a Variable

Post by bumbolol »

Hi,

I'm making my own document class. In this class, I want to print a page according to some information provided by the user of the class. This is very similar to the '\maketitle' command that retrieves information set by commands such as '\title' or '\author'.

Since there is a lot of information that can be set by the user of the class, I need to declare as much variables. To spare me the time of writing numerous times the necessary calls to '\def' and '\gdef' commands, I'm trying to write a new command that does it for me.

Here is what I came up with:

Code: Select all

\newcommand{\defvar}[2]{\def#1##1{\gdef#2{##1}}#1{}}
For example, it can be used like this:

Code: Select all

\defvar{\name}{\getname}
The document class then just use the command '\getname' to get the string set by the document using the command '\name'.

Here is my question: is there a way to make it even shorter, like this:

Code: Select all

\defvar{name}
Which means, is there a way to make the '\defvar' command deducts the name of the setter and the name of the getter from the string provided by the one parameter it is given ?

Any other solution that could give the same result is of course welcome.

Regards.
Last edited by bumbolol on Wed Jan 25, 2012 12:49 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

New Command to define a Variable

Post by cgnieder »

You could use \@namedef:

Code: Select all

\documentclass{article}

\makeatletter
\newcommand*\DefVar[1]{\@namedef{#1}##1{\global\@namedef{get#1}{##1}}}
\makeatother
\DefVar{Name}\DefVar{Haus}\DefVar{Auto}

\Name{Mein Name}
\Haus{Mein Haus}
\Auto{Mein Auto}

\begin{document}

\getName, \getHaus, \getAuto

\end{document}
site moderator & package author
bumbolol
Posts: 4
Joined: Tue Jan 24, 2012 3:40 pm

New Command to define a Variable

Post by bumbolol »

Thanks Clemens, that seems to be a better solution than mine. I have a tiny problem though. With my newcommand, setting a variable was optional. For example, if I don't use '\Haus' then '\getHaus' would just equals to '\empty' (which can be tested). With your newcommand, I get an 'Undefined control sequence' error.

Of course, I could just set it to empty right after the definition:

Code: Select all

\DefVar{Haus}\Haus{}
But I would be somewhat back to square one, it is not longer shorter than using my newcommand.

Would you also have an idea for that one ?

Regards.
bumbolol
Posts: 4
Joined: Tue Jan 24, 2012 3:40 pm

New Command to define a Variable

Post by bumbolol »

Reply to myself: just use '\@nameuse' at the end of the newcommand. In the end, it looks like this:

Code: Select all

\newcommand\DefVar*[1]{\@namedef{#1}##1{\global\@namedef{get#1}{##1}}\@nameuse{#1}{}}
Problem solved, thanks again Clemens. Is there a need to put 'SOLVED' in the title of the thread now ?

Regards.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10348
Joined: Mon Mar 10, 2008 9:44 pm

New Command to define a Variable

Post by Stefan Kottwitz »

Hi,
bumbolol wrote:Is there a need to put 'SOLVED' in the title of the thread now?
it would be great if you would edit the first post in the thread and choose the checkmark as topic icon. That's the way we notice open and solved questions here, no SOLVED necessary, to be consistent.

Thanks!

Stefan
LaTeX.org admin
bumbolol
Posts: 4
Joined: Tue Jan 24, 2012 3:40 pm

Re: New Command to define a Variable

Post by bumbolol »

Done.
Post Reply