[Tex/LaTex] How to define a newcommand that expands into another newcommand

macros

I'm not sure exactly what terminology to use to ask this question.

What I want to do is define a command pluralize such that I can do this:

\newcommand{\individual}{individual\xspace}
\pluralize{\individual}

the \individual with the \individuals

and then get

the individual with the individuals

The reason I want to do this is because I might change terminology like:

\newcommand{\individual}{name\xspace}
\pluralize{\individual}

the \individual with the \individuals

and then get

the name with the names

Currently what I'm doing is explicitly writing out the plural version of each command like:

\newcommand{\individual}{individual\xspace}
\newcommand{\individuals}{\individual{}s\xspace}

But it seems like I should be able to turn this into a macro for simple pluralizations.

I have tried three things so far that do not work. First I tried what I thought may work:

\newcommand{\pluralize}[1]{\newcommand{\#1}{\#1{}s\xspace}

Then I looked online. I tried following the guidelines in
Defining a newcommand, with variable name, inside another newcommand
I came up with

\makeatletter
\newcommand{\pluralize}[1]{%
    \expandafter\newcommand\csname #1s\endcsname{#1{}s\xspace}%
}
\makeatother

and

\makeatletter
\newcommand{\pluralize}[1]{%
    \@namedef{#1s}{\\#1s\xspace}
}
\makeatother
\expandafter\newcommand\csname #1s\endcsname{#1{}s}%

but I either get a compile error, or an error if I use the macro. I'm not really sure what the @namedef, \expandafter, or \csname commands do either.


How can I correctly define the pluralize command?

Best Answer

I'd change the approach:

\newcommand{\makename}[3][s]{%
  \expandafter\newcommand\csname #2\endcsname{#3\xspace}%
  \expandafter\newcommand\csname #2s\endcsname{#3#1\xspace}%
}

\makename{individual}{name}
\makename[es]{veggie}{potato}

In the document you can do

The \individual eats \veggie, while \individuals eat \veggies.

that will print

The name eats potato, while names eat potatoes.

Note. In general I recommend against using \xspace; I do also in this case.