10

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?

2 Answers 2

14

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{...}.

1
  • Beat me by 8 seconds. :)
    – Alan Munn
    Jun 3, 2013 at 15:54
6

Define a command that does the concatenation.

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

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

\newcommand{\Probability}[1]{\csname Probability#1\endcsname}

\section{The results for \CaseID}

\CaseID\ has probabilty  \Probability\CaseID

\end{document}

However, you can do in a different way:

\documentclass{article}

\makeatletter
\newcommand{\DefineCase}[3]{%
  % #1 is an identifier
  % #2 is the text
  % #3 is the probability
  \@namedef{ID@#1}{{#2}{#3}}%
}
\newcommand{\CaseID}[1]{%
  \expandafter\expandafter\expandafter\@firstoftwo\csname ID@#1\endcsname
}
\newcommand{\Probability}[1]{%
  \expandafter\expandafter\expandafter\@secondoftwo\csname ID@#1\endcsname
}
\makeatother

\DefineCase{1}{CaseOne}{1.00E-04}

\begin{document}
\section{The results for \CaseID{1}}

\CaseID{1} has probability \Probability{1}
\end{document}

The first argument to \DefineCase is an arbitrary string that identifies the case; the second is the corresponding text and the third is the associated probability. In this way you can define as many cases as you want, with a uniform syntax, for instance

\DefineCase{xyz}{Anything}{1}

will allow you to say in the document \CaseID{xyz} to print "Anything" and \Probability{xyz} to print 1.

enter image description here

You must log in to answer this question.

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