# Macro shortcut for \mathrm command

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}

• Please consider that there might be some commands like \aa etc. already. Your request would break this – user31729 Sep 29 '17 at 19:25
• 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
• I have just modified my answer tex.stackexchange.com/a/173246/4427 that could be useful. – egreg Sep 29 '17 at 20:45

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
• 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
• @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 9:35
• @percusse I added a much less arcane version, with no \noexpand and just three \expandafters – egreg Sep 30 '17 at 10:11

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.

• I can't seem to find a shortcut for \mathrm in TeXstudio, if indeed there is one. I've gotten tired of typing out \mathrm a bunch of times, hah. Point taken about future readability. – grfrazee Sep 29 '17 at 19:50
• "Any reasonable editor" :-) – David Carlisle Sep 29 '17 at 20:00
• Hah. Us neophytes need more idiot-proof editors :-) – grfrazee Sep 29 '17 at 20:17
• Any reason not to use \let\R\mathrm? – Patrick Happel Sep 29 '17 at 23:13
• @PatrickHappel I thought (for once) I'd stick to commands documented in the latex book not use internal tex primitives:-) – David Carlisle Sep 30 '17 at 7:50

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).

• I believe that defining it as you have it (Mac) or Ctrl + R (Windows) would rebind the Find/Replace command. However, it looks like Alt + Ctrl + R is unreserved. Thanks for the pointer! – grfrazee Sep 29 '17 at 20:12
• @grfrazee cmd+R is indeed just a random example, but you got the message :) – user36296 Sep 29 '17 at 20:14
• ...and Alt + Ctrl + R is reserved too. Whoops. Alt + Shift + R seems to work though. – grfrazee Sep 29 '17 at 20:14
• @grfrazee My usual approach to find free combinations is to look through the already defined ones for combinations I never use and change them to something complicated, which will free the combination. – user36296 Sep 29 '17 at 20:16
• You mean that you don't use Ctrl + Alt + Shift + % on a regular basis? ;) – grfrazee Sep 29 '17 at 20:18

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


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}
`