I am trying to create a method MakeCommands which will generate a couple of commands based on the basic name passed as a parameter. However, I want the user to be allowed to pass a name with spaces, which therefore must be extracted from the passed parameter for use as actual function names
What I currently have (creating just one command, for simplicity's sake) is:
\newcommand*{\MakeCommand}[1]
{
\expandafter\newcommand\csname the#1\endcsname{TEST}
}
This works, in that a call such as \MakeCommand{Bday} creates the command \theBday which expands to TEST. However, if the parameter is actually multiple words (\MakeCommand{Date of Birth}), then created command would have to be called using \csname theDate of Birth\endcsname, which isn't very elegant.
So I tried the following:
% \usepackage{xstring}
\newcommand*{\temp}{}
\newcommand*{\MakeCommand}[1]
{
\renewcommand*{\temp}{\StrSubstitute{#1}{ }{}}
\expandafter\newcommand\csname the\temp\endcsname{TEST}
}
Basically, use xstring's StrSubstitute command to extract the spaces from the passed parameter and store it in \temp. And then use this spaceless version of the parameter as the command name.
This, however, doesn't work and I get multiple errors. For instance, for the following MWE:
\documentclass[a4paper,10pt]{article}
\usepackage{xstring}
\newcommand*{\temp}{}
\newcommand*{\MakeCommand}[1]
{
\renewcommand*{\temp}{\StrSubstitute{#1}{ }{}}
\expandafter\newcommand\csname the\temp \endcsname{TEST}
%\expandafter\newcommand\csname the#1\endcsname{TEST}
}
\MakeCommand{Date of Birth}
\begin{document}
\theDateofBirth
\end{document}
I get the following errors:
Missing \endcsname inserted. \MakeCommand{Date of Birth}
Command \the already defined. \MakeCommand{Date of Birth}
Extra \endcsname. \MakeCommand{Date of Birth}
Missing \begin{document}. \MakeCommand{Date of Birth}
Undefined control sequence. ^^I\theDateofBirth
If I comment out the \expandafter... line and replace \theDateofBirth inside the document contents with \temp, the file compiles just fine and outputs "DateofBirth", as expected.
I can tell from other readings ([A], [B], [C]) that this probably requires additional \expandafters, but I can't for the life of me figure out where to put them and why/how.
This is for a file which will display multiple data points. MakeCommand should actually create three related commands. What I meant to do was
\newcommand*{\temp}{}
\newcommand*{\MakeCommand}[1]
{
\renewcommand*{\temp}
{
% store spaceless version of parameter
\StrSubstitute{#1}{ }{}
}
\expandafter\newcommand\csname the\temp\endcsname{}% empty variable for use further ahead
\expandafter\newcommand\csname \temp\endcsname[1]
{
% store value of data point
\expandafter\renewcommand\csname the\temp \endcsname{#1}
}
\expandafter\newcommand\csname print\temp\endcsname
{
% print out formatted data point
\textbf{\temp:} \csname\the\temp\endcsname
}
}
So, MakeCommands{Date of Birth} would create three commands
\theDateofBirth, which is initially empty\DateofBirth, which the user can later call to define\theDateofBirth\printDateofBirth, which prints out the data point
For example, \DateofBirth{August 31, 2016} would cause \printDateofBirth to output "Date of Birth: August 31, 2016".
\makeatletter \newcommand\xxx[1]{\@nameuse{the#1}} \makeatotherthen use it like\xxx{Date of Birth}? (Not that I'd do this in one of my own documents.) – jon Aug 31 '16 at 05:59