5

If I do something like

\newcommand{\mc}{\textbf{MyCommand}\xspace}

I would like to be able to add an s or 's to the text. In this case I would like to easily be able to ouput MyCommands or MyCommand's in bold.

Right now I have to have multiple command to do so. Ideally I would like to use \mc's and \mcs(or if necessarily, something like \mc^s) to add the 's but all in bold face.

3 Answers 3

7

I don't think it's a good idea. Anyway, here it is:

\documentclass{article}
\usepackage{xspace}
\makeatletter
\newcommand{\mc}{%
  \textbf{MyCommand}%
  \@ifnextchar'%
    {\mc@checks}% check for a following s
    {\xspace}% no ' follows
}
\newcommand{\mc@checks}[1]{% gobble the apostrophe
  \@ifnextchar s%
    {\textbf{'s}\@gobble}% print 's and gobble the s
    {'}% reinstate the apostrophe
}
\makeatother

\begin{document}
\mc is simple

\mc's has s

``\mc'' has no s

``\textbf{MyCommand}'' has no s % just for testing
\end{document}

enter image description here

If you have several commands like this and you need also an "s-variant", just abstract the construction:

\documentclass{article}
\usepackage{xspace}
\makeatletter
\newcommand{\defineabbrev}[2]{%
  \expandafter\newcommand\csname#1\endcsname{%
    \textbf{#2}\@ifnextchar'{\dabbr@checks}{\xspace}%
  }
  \expandafter\newcommand\csname#1s\endcsname{%
    \textbf{#2s}\xspace
  }
}
\newcommand{\dabbr@checks}[1]{%
  \@ifnextchar s{\textbf{'s}\@gobble}{'}%
}
\makeatother

\defineabbrev{mc}{MyCommand}

\begin{document}
\mc is simple

\mc's has s

``\mc'' has no s

``\textbf{MyCommand}'' has no s

\mcs has s
\end{document}

With \defineabbrev{xyz}{Text} you define both \xyz that checks for 's following it and \xyzs that directly appends s.

15
  • To work as requested, don't you need to add \newcommand{\mcs}{\textbf{MyCommands}}? I thought about defining \mcs in terms of \mc but I think that will break hyphenation and other niceties.
    – cfr
    Dec 5, 2013 at 1:54
  • @cfr Yes, this only gets the 's but not the s. I guess all one would need to do is create a new command with an added s in the command definition somehow so \mcs can be used for but not have to be explicitly defined?
    – user14448
    Dec 5, 2013 at 10:02
  • @user14448 No, you can't use \mcs unless you define it; but I wouldn't add a test for \mc s, because an ‘s’ after \mc might be the start of another word.
    – egreg
    Dec 5, 2013 at 10:43
  • yes, I want \mcs defined, but I don't want to have to do it myself because it would require duplicating every macro.
    – user14448
    Dec 5, 2013 at 11:10
  • @user14448 I added an abstract version that avoids code duplication.
    – egreg
    Dec 5, 2013 at 11:31
7
\documentclass{article}
\usepackage{xspace}
\newcommand{\mc}[1][]{\textbf{MyCommand#1}\xspace}
\begin{document}
\mc 
\mc[s]
\mc['s].
\end{document}

enter image description here

1
  • I would like to avoid having to pass the s directly using arguments because it verbose.
    – user14448
    Dec 5, 2013 at 10:18
1

From the comments above, I gather that you are not opposed to using different commands for each form that you want: you just don't want the hassle of having to write out the code each time you want to build a new declinable macro.

Here's an approach which lets you build all the macros in one command that would closely resemble the typical \newcommand that you would issue to build the underlying macro.

The macro name is \buildDeclinedForms which takes two arguments. The first argument should be a bare control sequence name (but without the escape \). The second argument should be the replacement text with #1 inserted where you want to insert the variant forms.

\documentclass{article}
\usepackage{xspace}
\makeatletter

\newcommand\buildDeclinedForms[2]{%%
  \expandafter\newcommand\csname ae@#1\endcsname[1]{#2}
  \expandafter\newcommand\csname #1\endcsname{\csname ae@#1\endcsname{}}%%
  \expandafter\newcommand\csname #1s\endcsname{\csname ae@#1\endcsname{s}}%%
  \expandafter\newcommand\csname #1as\endcsname{\csname ae@#1\endcsname{'s}}%%
  \expandafter\newcommand\csname #1sa\endcsname{\csname ae@#1\endcsname{s'}}}%%

\makeatother
\begin{document}

\buildDeclinedForms{nc}{\textbf{declinable command#1}\xspace}

\nc   following text\par
\ncs  following text\par
\ncas following text\par
\ncsa following text\par

\end{document}

The resulting output:

enter image description here

Of course this opens a whole Pandora's box of other possibilities: creating verb forms, contextual capitalization, etc. If you wanted to go to this extreme, it shouldn't be that hard.

There's also potential confusion in error messages once you forget how you're acquiring all these fancy commands. If you've already defined \ncsa or later choose to define \ncsa, the error messages LaTeX sends you will not be particularly helpful. At least if it were me, I might remember creating \nc but might for get that I've subsequently also created \ncs, \ncas, and \ncsa.

1
  • Yeah, this was originally what I was trying to do. It works and allows more possibilities than egregs version but I like the simplicity of his for the specific case. A combination of the methods is probably best.
    – user14448
    Dec 6, 2013 at 4:25

You must log in to answer this question.

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