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 Answers3

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

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.

egreg
  • 1,121,712
  • 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 05 '13 at 01: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 05 '13 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 05 '13 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 05 '13 at 11:10
  • @user14448 I added an abstract version that avoids code duplication. – egreg Dec 05 '13 at 11:31
  • Thanks, the last one is exactly what I want. Makes it very easy to define a text id and have it work well with abbreviations and formatting. – user14448 Dec 05 '13 at 11:43
  • not using \xspace would make this so much easier... :) – cgnieder Dec 05 '13 at 11:58
  • @cgnieder I know; but the customer's always right, David would say. – egreg Dec 05 '13 at 12:00
  • @cgnieder I don't care about xspace but something is needed to keep the space so that the text from the macro and the text adjacient to the macro have a space. \ID is an id string should not end up as IDis an id string which xspace solves. – user14448 Dec 07 '13 at 00:05
  • One problem I have is using the commands inside a \caption. Says that there are too many }'s. I guess this is a bug in the macro? – user14448 Dec 07 '13 at 00:05
  • @user14448 Not a bug, but those commands are fragile. Either use \protect in front of them (\protect\mc) or change the first \newcommand in the body of \defineabbrev into \DeclareRobustCommand. – egreg Dec 07 '13 at 00:11
  • @user14448 Spaces after macros made up from one ore more letters are ignored. The usual practice would be to write \ID\ is an id string. – cgnieder Dec 07 '13 at 01:32
  • @cgnieder but by using \xspace I can avoid having to type the \ and everything works fine. Xspace works better. These macro's are only for specifying keywords and will always have spaces after them(except when modifying the keyword for grammatical reasons). That means I would always have to add the additional \ for no real reason when I can just use xspace. – user14448 Dec 07 '13 at 05:12
  • @user14448 Re »xspace works better« - Maybe. Except when it doesn't, see http://tex.stackexchange.com/a/86620/5049 :) – cgnieder Dec 07 '13 at 15:50
  • @cgnieder so far it works for me. I am not doing anything complicated with it as I'm just wrapping key words and it seems to handle that case fine. Maybe in the future it will have issues but surely then one can just resort to the more verbose case without issue. e.g., there is really no drawback AFAICS. If xspace works then it saves you some work. If it doesn't then you just have to add {} at the end and it works? Without xspace you ALWAYS have to add {} or other macro terminators. – user14448 Dec 07 '13 at 16:22
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.

A.Ellett
  • 50,533
  • 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 06 '13 at 04:25