4

I am attempting to make a macro shortcut for the \mathrm command from amsmath.

My idea is that in lieu of writing, for example, \mathrm{R} to get the Roman R in math mode, I'd like to enter \RR instead. Likewise, suppose I wanted Roman P, I'd like instead to enter \PP.

The following MWE gets me the Roman R symbol, but I'd like this to be maximally flexible for any letter without having to define 52 separate \aa, \bb, \cc commands.

\documentclass{book}
\usepackage{amsmath}

\newcommand{\RR}{\mathrm{R}}

\begin{document}

$ \RR$

\end{document}
grfrazee
  • 969
  • 1
    Please consider that there might be some commands like \aa etc. already. Your request would break this –  Sep 29 '17 at 19:25
  • 3
    many such two letter commands are already defined, notably \aa and \tt what's wrong with \newcommand\R{\mathrm} then you can use \R R, \R x etc – David Carlisle Sep 29 '17 at 19:25
  • @ChristianHupfer, understood. It looks like the answers have taken this into consideration, but thanks for the warning. – grfrazee Sep 29 '17 at 19:35
  • 1
    I have just modified my answer https://tex.stackexchange.com/a/173246/4427 that could be useful. – egreg Sep 29 '17 at 20:45

5 Answers5

9

Rather than define lots of commands you can define a command with arguments eg

\newcommand\R{\mathrm}

which allows \R R, \R x or \R{P} etc.

Although to be honest I would not do that. Any reasonable TeX editor could have (and probably has already) keyboard shortcuts to add \mathrm so it's no harder to add and much easier for anyone reading your source to read. It's worth making aliases for semantic markup so

\newcommand\vector{\mathrm}
 ...  \vector{x}...

if you are setting vectors in roman, as that allows alternative notations without changing the markup, but just aliasing a font change to use as a font change doesn't really gain a lot.

David Carlisle
  • 757,742
6

Taking your comment into consideration, maybe all you need is to define a shortcut in texstudio that will automatically insert \mathrm for you (replace cmdR by your favourite but not yet taken combo).

enter image description here

5

Here, I used, to demonstrate, the three-letter sequence, since many two-letter combos are already taken.

\documentclass{book}
\usepackage{amsmath,pgffor}
\foreach\i in{A,...,Z}{\expandafter\gdef\csname\i\i\i\expandafter\endcsname
  \expandafter{\expandafter\mathrm\expandafter{\i}}}
\begin{document}
$\RRR$

$\BBB$
\end{document}

For lower-case, an identical loop can be added with only the limits changed to \foreach\i in{a,...,z}.

Percusse provided a more arcane version of the definition, which is nonetheless illustrative:

\foreach\i in{A,...,Z}{  
  \begingroup\edef\temp{\endgroup\noexpand\gdef\csname\i\i\i\noexpand\endcsname{%
  \noexpand\mathrm{\i}}}\temp
}

Nonetheless, David's suggestion of \newcommand\R{\mathrm}, which requires a slightly different syntax, is a better approach overall. I did it this way just to show it could be done.

  • I guess \begingroup\edef\temp{\endgroup\noexpand\gdef\csname\i\i\i\noexpand\endcsname{\noexpand\mathrm{\i}}}\temp is a bit shorter but it's just TeX mediocrity. – percusse Sep 29 '17 at 19:43
  • 1
    so many expandafters:-) sane with fewer: \foreach\i in{A,...,Z}{\expandafter\gdef\csname\i\i\i\expandafter\endcsname\expandafter{\expandafter\mathrm\expandafter{\i}}} – David Carlisle Sep 29 '17 at 19:47
  • @DavidCarlisle I didn't know that I could skip those. Very clever. I will update. – Steven B. Segletes Sep 29 '17 at 19:52
  • by the way, "sane" was a typo for same, not a reflection on your sanity:-) – David Carlisle Sep 29 '17 at 19:53
  • @DavidCarlisle Whew! I almost threw \zz back in your face, in umbrage. :^) – Steven B. Segletes Sep 29 '17 at 19:55
  • @percusse Got it. Thanks for the clever technique. What do the \begingroup and \endgroup accomplish? I've not seen it used that way, in the middle of a definition. – Steven B. Segletes Sep 29 '17 at 20:04
  • Also I've learned it from egreg. It's yet another TeX quirk. It basically makes the temp as if never defined since the definition is performed inside a group. – percusse Sep 29 '17 at 20:16
  • Though @DavidCarlisle's solution is a little more elegant (and keeps with the spirit of long-term readability), I'm giving you the check mark since your answer actually matches my desired syntax, regardless of how "unsustainable" my original request was. Thanks all for the help! – grfrazee Sep 29 '17 at 20:41
  • @grfrazee Thank you so much. Asking a question is a good way to learn new techniques. – Steven B. Segletes Sep 30 '17 at 00:14
  • note @percusse's version is equivalent to \begingroup\edef\temp{\endgroup\gdef\csname\i\i\i\endcsname{\noexpand\mathrm{\i}}}\temp (\gdef isn't expandable so the \noexpand isn't doing anything) and relies on the commands such as \RRR not being defined. (try doing the loop twice:-) – David Carlisle Sep 30 '17 at 07:59
  • @DavidCarlisle Blame egreg he is a bad teacher. Wouldn't the expandafter version have the same problem since csname will expand? – percusse Sep 30 '17 at 08:32
  • @percusse no because it only expands once, to \RRR but in an edef it expands till it pops. you want to replace \noexpand\gdef\csname by \gdef\expandafter\noexpand\csname – David Carlisle Sep 30 '17 at 09:10
  • 1
    @percusse I taught you the trick, but you applied it carelessly. ;-) Anyway, since this is used in \foreach, the trick is not really relevant, because \temp would be forgotten anyway. – egreg Sep 30 '17 at 09:35
  • @egreg you blame the student I blame the system – percusse Sep 30 '17 at 10:00
  • 1
    @percusse I added a much less arcane version, with no \noexpand and just three \expandafters – egreg Sep 30 '17 at 10:11
3

And here is solution based on TeX primitives and plain TeX macros:

\newcount\tmpnum

\def\setRR #1-#2{\tmpnum=`#1
   \loop
      \begingroup\lccode`.=\tmpnum
      \lowercase{\endgroup \expandafter\def\csname..\endcsname{\mathrm{.}}}
      \ifnum\tmpnum<`#2 \advance\tmpnum by1 \repeat
}
\setRR A-Z \setRR a-z

%% test:

\show\RR \show\xx

\bye
wipet
  • 74,238
2

A variation on the theme:

\documentclass{book}
\usepackage{amsmath,pgffor}

\begingroup
\def\makethiscommand#1{\expandafter\gdef\csname #1#1#1\endcsname{\mathrm{#1}}}
\foreach\i in{A,...,Z}{%
  \expandafter\makethiscommand\expandafter{\i}%
}
\endgroup

\begin{document}
$\RRR$ \texttt{\meaning\RRR}

$\BBB$ \texttt{\meaning\BBB}
\end{document}

enter image description here

egreg
  • 1,121,712