5

Say I have written two commands, CommandA and CommandB. I wish to have a third command that takes inputs to determines which of the two previous commands to execute. Very naively this how I would try and accomplish this as shown below, however, it does not work.

\newcommand{CommandA}{
    \hspace{5mm}
}
\newcommand{CommandB}{
    \vspace{5mm}
}
\newcommand{C}[1]{
    \Command#1
}

So if I call \C{A}, as written I would really execute \CommandA.

Novice C
  • 153

3 Answers3

11

For a normal user, this is probably the easiest to understand

\usepackage{etoolbox}
\newcommand{\CommandA}{%
    \hspace{5mm}%
}
\newcommand{\CommandB}{%
    \vspace{5mm}%
}
\newcommand\C[1]{%
   \csuse{Command#1}%
}

\csuse builds a macro name, and calls it

David Carlisle
  • 757,742
daleif
  • 54,450
3

It makes sense to define families of such commands.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\defineobject}{mm}
 {
  \prop_new:c { g_garth_#1_prop }
  \prop_gset_from_keyval:cn { g_garth_#1_prop } { #2 }
 }

\NewDocumentCommand{\getobject}{mm}
 {% #1 = object name, #2 = variable or string
  \prop_item:cf { g_garth_#1_prop } { #2 }
 }
\cs_generate_variant:Nn \prop_item:Nn { cf }
\ExplSyntaxOff

\defineobject{C}{
  A=\hspace{5mm},
  B=\vspace{5mm}
}
\newcommand{\C}[1]{\getobject{C}{#1}}

\begin{document}

X\C{A}Y\C{B}

XYZ

\end{document}

enter image description here

This might turn to be useful for the similar problem in Can you create concatenate variable names in LaTex?

\documentclass[12pt]{exam}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\defineobject}{mm}
 {
  \prop_new:c { g_garth_#1_prop }
  \prop_gset_from_keyval:cn { g_garth_#1_prop } { #2 }
 }

\NewDocumentCommand{\getobject}{mm}
 {% #1 = object name, #2 = variable or string
  \prop_item:cf { g_garth_#1_prop } { #2 }
 }
\cs_generate_variant:Nn \prop_item:Nn { cf }
\ExplSyntaxOff

\defineobject{fun}{
  A={$s(t) = 12t^2 -7t + 16$},
  B={$s(t) = 16t^2 +3t + 10$},
  C={$s(t) = 12t^2 + t + 10.$}
}

\newcommand{\exam}{A}

\begin{document}

\begin{questions}

\question
The position of an object moving along a straight line is given by 
\getobject{fun}{\exam}. Find the average velocity of the object over 
the interval $[1,1+h]$ where $h>0$ is a real number.

\renewcommand{\exam}{B} % just for testing

\question
The position of an object moving along a straight line is given by 
\getobject{fun}{\exam}. Find the average velocity of the object over 
the interval $[1,1+h]$ where $h>0$ is a real number.

\end{questions}

\end{document}

The second question has a modified \exam just to test whether the expected output is obtained. You can define as many objects as you want. If one has just one variable \exam, it also makes sense to define a shorthand:

\newcommand{\obj}[1]{\getobject{#1}{\exam}}

and then the calls above can be more simply \obj{fun}.

enter image description here

egreg
  • 1,121,712
1
\documentclass{article}

\newcommand\exchange[2]{#2#1}%
\newcommand\name{}%
\long\def\name#1#{\romannumeral0\innername{#1}}%
\newcommand\innername[2]{%
   \expandafter\exchange\expandafter{\csname#2\endcsname}{ #1}%
}%

\name\newcommand{CommandA}{%
    \hspace{5mm}%
}

\name\newcommand{CommandB}{%
    \vspace{5mm}%
}

\name\newcommand{C}[1]{%
    \name{Command#1}%
}

\begin{document}

\hbox{\hbox{X}\C{A}\hbox{X}}%
\C{B}%
\hbox{\hbox{X}\C{A}\hbox{X}}%

\end{document}

enter image description here

Ulrich Diez
  • 28,770