2

I have defined a term using a new command as follows:

\newcommand{\ourmethod}{G-MM\xspace}

Now I'd like to add a suffix to the term that I just defined, e.g. I'd like to use "\ourmethod" to say G-MMs (note that there is an "s" added to the term). How can I do this in LATEX?

4
  • 1
    Under what conditions do you want \ourmethod to print G-MM or G-MMs? You can, of course, do \renewcommand{\ourmethod}{G-MMs}. – Werner Dec 11 '15 at 5:13
  • 2
    It would be easier if you had not used xspace. As it is, \ourmethod{}s wil do the right thing most of the time, but having \xspace{} before the s will break any font specified kerns between the last two letters, and affect possible hyphenation. – David Carlisle Dec 11 '15 at 7:44
  • Welcome to TeX.SE. – Peter Grill Dec 11 '15 at 9:04
  • @Werner: I would like to be able to add arbitrary suffixed to the command without having to define a new command for every new suffix. So, I'd like to keep \ourmethod to translate to G-MM. – Sobi Dec 11 '15 at 16:07
7

I think you should follow David's recommendation. But, if you still want to use xspace (or even if you decide not to), I would suggest you define both the singular and plural once with something like:

\DefineTerms{ourmethod}{G-MM}

which creates the two macros: \ourmethod and \ourmethods:

enter image description here


Alternativly, you could define the command where you provide the suffix as an optional parameter in square brackets:

\newcommand{\ourmethod}[1][]{G-MM#1\xspace}

enter image description here


Code: Using etoolbox

\documentclass{article}
\usepackage{etoolbox}
\usepackage{xspace}

\newcommand{\DefineTerms}[2]{%
    \csdef{#1}{#2\xspace}%
    \csdef{#1s}{#2s\xspace}%
}

\DefineTerms{ourmethod}{G-MM}

\begin{document}
\verb|\ourmethod|: \ourmethod

\verb|\ourmethods|: \ourmethods
\end{document}

Code: Without etoolbox:

\documentclass{article}
\usepackage{xspace}

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

%\newcommand{\ourmethod}{G-MM\xspace}
\DefineTerms{ourmethod}{G-MM}

\begin{document}
\verb|\ourmethod|: \ourmethod

\verb|\ourmethods|: \ourmethods
\end{document}

Code: Optional Parameter

\documentclass{article}
\usepackage{xspace}

\newcommand{\ourmethod}[1][]{G-MM#1\xspace}

\begin{document}
\verb|\ourmethod|: \ourmethod

\verb|\ourmethod[s]|: \ourmethod[s]
\end{document}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.