GeneralUsing commands as variables

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
charlie hubbard
Posts: 2
Joined: Mon Mar 08, 2010 9:58 pm

Using commands as variables

Post by charlie hubbard »

I have a problem I've been fighting for a couple of days now, and, so
far at least, I just haven't been able to find a workable solution.
Hopefully someone here will know the answer to this, or at least be able
to tell me, "No, that can't be done." Here's the problem...

I would like something akin to text variables so that I can store a
value in a variable and then pull it out at some later point in my
document and use it. I understand that LaTeX doesn't have exactly
variables, but, in most instances, I think a new command can be defined
that does the same thing. Unfortunately, making a new command is
failing me for this particular situation. Look at the following example

\newcommand{\myvalue}{apple}
\newcommand{\othervalue}{orange}
\newcommand{\capturevalue}{
\renewcommand{\myvalue}{\othervalue}
}
\renewcommand{\othervalue}{pair}

What I really want to do here, is capture in \myvalue the expansion of
\othervalue at the time \capturevalue was called. But of course the
above code doesn't do that. Instead, it just makes \myvalue an alias
for \othervalue, so, if at some later point in my document, I output

\myvalue

It's exactly the same as if I'd typed \othervalue there, and I get
"pair" and not "orange".

What I really want to do is expand the \othervalue command before it
gets assigned to \myvalue. I haven't been able to figure out how to do
it. I thought maybe macros would get expanded before being passed as
parameters, so I tried a version that looks like this.

\newcommand{\myvalue}{apple}
\newcommand{\othervalue}{orange}
\newcommand{\capturevalue}[1]{
\renewcommand{\myvalue}{#1}
}

Then I use it in my document like so...

\capturevalue{\othervalue}
\renewcommand{\othervalue}{pair}
\myvalue

No good. \myvalue still outputs "pair" and not the value that
\othervalue contained when \capturevalue was called. Evidentally, macro
expansion doesn't happen at the point that a command is used as a
parameter.

I've spent hours googling latex macros, looking for some example that
does this or demonstrates how to force a macro expansion, and I've tried
50 different things, none of which seem to work. I suspect it's just
not possible with straight LaTeX commands (maybe there's a TeX way to do
it?), but I thought I'd run it by the experts first to see if you guys
knew a clever way to do it, before I just gave up.

Recommended reading 2024:

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

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

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Using commands as variables

Post by frabjous »

Could you use saveboxes?

Code: Select all

\documentclass{article}
\begin{document}
\newsavebox{\myvalue}
\savebox{\myvalue}{apple}
\newsavebox{\othervalue}
\savebox{\othervalue}{orange}
The values are currently \usebox{\myvalue} and \usebox{\othervalue}.

\newcommand{\capturevalue}{
\savebox{\myvalue}{\usebox{\othervalue}}
}

But now I shall capture the value of the second with the first.\capturevalue

And then change the second.\savebox{\othervalue}{pear}

The variable was \usebox{\myvalue}, but now it's \usebox{\othervalue}.
\end{document}
If not, I think there are ways to do it with TeX (not LaTeX) commands, but I prefer higher-level commands when possible.
charlie hubbard
Posts: 2
Joined: Mon Mar 08, 2010 9:58 pm

Re: Using commands as variables

Post by charlie hubbard »

frabjous,

Your solution worked perfectly for me. Thank you for the quick response and the great example!
Post Reply