7

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?

0

2 Answers 2

11

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.

2
  • With three arguments, the first one optional, I think your argument numbers in the newcommand are incorrect. The first would be \csname #2\endcsname{#3}, the plural one would be \csname #2s\endcsname{#3#1}, right? Commented Aug 9, 2015 at 4:42
  • @AndrewCashner Right!
    – egreg
    Commented Aug 9, 2015 at 8:44
5

Here is one way to achieve it:

enter image description here

\documentclass{article}

\usepackage{xspace,xparse}

% https://tex.stackexchange.com/a/64380/5764
\makeatletter
\def\stripbs{\expandafter\@gobble\string}

\NewDocumentCommand{\createplural}{m m O{s}}{%
  \newcommand{#1}{#2\xspace}%
  \@namedef{\stripbs#1#3}{#2#3\xspace}%
}
\makeatother

\createplural{\individual}{name}%[s]

\begin{document}

The \individual with the \individuals.

\end{document}

\pluralize{<csname>}{<name>}[<plural>] provides an optional "<plural>" as the last argument.

While using xspace is sometimes beneficial, it also has its drawbacks.

You must log in to answer this question.

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