6

I took some habits with my sciences reports so I developed some personalized commands.

First, I define in the preamble all the variables that I need like this:

\newcommand{\cvba}{Cv_{\beta/\alpha}}
\newcommand{\za}{z_{\alpha}}
...

Then I begin all my equation labels by "eq:" like in this example :

\begin{equation}
  \za = \frac{1000.-T}{34}
  \label{eq:za}
\end{equation}

And finally, I set a command which allows me to make a reference from a variable to its definition inside the environment equation :

\newcommand{\eref}[2]{\hyperref[eq:#1]{#2}}

In this example, za will point to the previous equation :

\begin{equation}
  \cvba = \eref{za}{\za}+12
  \label{eq:cvba}
\end{equation}

My problem is that I would like to make my new command eref a little bit easier and lighter to use such as it takes only one parameter.

I tried to define it like this :

\newcommand{\eref}[1]{\hyperref[eq:#1]{\#1}}

Obviously it doesn't work because the \# is understood as a specific character.

So my question is : "How could I concatenate two strings to make a new command?"

If it can helps, the names of my variables can change, for example they could be \var_varname such as he new command would looks like :

\newcommand{\eref}[1]{\hyperref[eq:#1]{\var_#1}}

1 Answer 1

9

instead of

\var_#1

you want

\csname var_#1\endcsname
5
  • It worked perfectly (without the underscore in the variable name)! Thanks.
    – GuillaumeM
    Commented Jul 19, 2013 at 12:37
  • For the ones who are interested, here is the good command : \newcommand{\eref}[1]{\hyperref[eq:#1]{\csname #1\endcsname}}
    – GuillaumeM
    Commented Jul 19, 2013 at 12:43
  • It is worth linking to egreg's luminous explanation of how \csname is 'is used to build commands from "variable parts"', so I shall: tex.stackexchange.com/a/39382/175 Commented Jul 19, 2013 at 12:46
  • @CharlesStewart What? egreg has an answer more wordy than mine? I'm shocked. Commented Jul 19, 2013 at 12:59
  • @DavidCarlisle - I'm sure you can draft a dreadnought-class answer to that question :) Commented Jul 19, 2013 at 14:45

You must log in to answer this question.

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