Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}}
share|improve this question

1 Answer 1

up vote 7 down vote accepted

instead of

\var_#1

you want

\csname var_#1\endcsname
share|improve this answer
    
It worked perfectly (without the underscore in the variable name)! Thanks. –  GuillaumeM Jul 19 '13 at 12:37
    
For the ones who are interested, here is the good command : \newcommand{\eref}[1]{\hyperref[eq:#1]{\csname #1\endcsname}} –  GuillaumeM Jul 19 '13 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 –  Charles Stewart Jul 19 '13 at 12:46
    
@CharlesStewart What? egreg has an answer more wordy than mine? I'm shocked. –  David Carlisle Jul 19 '13 at 12:59
    
@DavidCarlisle - I'm sure you can draft a dreadnought-class answer to that question :) –  Charles Stewart Jul 19 '13 at 14:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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