[Tex/LaTex] Macro shortcut for \mathrm command

amsmathmacros

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}

Best Answer

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.

Related Question