[Tex/LaTex] How to combine two text strings to represent the name of a command

macrosstrings

I want to combine one command with a text string to represent a second command. In MatLab it would be possible to concatenate these two strings into one string and evaluate this new string. However I have no clue as to how this can be done in LaTeX. See my working example below:

\documentclass[10pt,a4paper,twosided]{article}    
    \begin{document}


        \newcommand{\CaseID}{CaseOne} 

        \newcommand{\ProbabilityCaseOne}{1.00E-04}

        \paragraph{The results for \CaseID}

        \CaseID has probabilty  \Probability\CaseID


    \end{document}

The combination \Probability\CaseID does not work as a synonym for \ProbabilityCaseOne. How can I achieve a functionality like that?

Best Answer

This can also be achieved in LaTeX:

enter image description here

\documentclass{article}
\begin{document}

\newcommand{\CaseID}{CaseOne}

\newcommand{\ProbabilityCaseOne}{1.00E-04}

\paragraph{The results for \CaseID}

\CaseID{} has probabilty  \csname Probability\CaseID\endcsname

\paragraph{The results for \CaseID}

\makeatletter
\CaseID{} has probabilty  \@nameuse{Probability\CaseID}
\makeatother
\end{document}

You can either use \csname ... \endcsname to construct a control sequence, or use \@nameuse{...}. The latter, of course, requires to be placed with the appropriate \makeatletter...\makeatother scope. See What do \makeatletter and \makeatother do? For the former, see the reference question What exactly do \csname and \endcsname do?

A reference from the TeX Book (Chapter 7 How TeX Reads What You Type, p 40):

... you can go from a list of character tokens to a control sequence by saying \csname\<tokens>\endcsname. The tokens that appear in this construction between \csname and \endcsname may include other control sequences, as long as those control sequences ultimately expand into characters instead of TeX primitives; the final characters can be of any category, not necessarily letters. For example, \csname TeX\endcsname is essentially the same as \TeX; but \csname\TeX\endcsname is illegal, because \TeX expands into tokens containing the \kern primitive. Furthermore, \csname\string\TeX\endcsname will produce the unusual control sequence \\TeX ...


The above all use the provided kernel functionality. However, etoolbox as a package provides similar constructions as wrappers for \@nameuse (and \@namedef). See section 3.1.1 Macro Definitions (p 5) of the etoolbox documentation. In particular it provides \csuse{...}.

Related Question