5

I set this new command:

\newcommand{\dd}{{\rm d}}

I want to set the same command but now in a generic way. Something like that:

\newcommand{\##}{{\rm #}}

How to do that?

Edit: The new command that I set (\newcommand{\dd}{{\rm d}}) only works for the letter d right?

I want to write the same new command but now working with any letter.

6
  • 1
    Do you want \newcommand{\myrm}[1]{{\rmfamily #1}}? If not, please clarify what you want to achieve. May 24, 2018 at 19:59
  • Sorry. I edited the question. I think now is more clear.
    – Mateus
    May 24, 2018 at 20:10
  • Sorry but the edit still doesn't make things very clear. Are you looking for a way to define multiple commands (e.g. \dd, \ee, \ff etc.? Or just a way to a have a single command which will apply to anything you give it (which is @PhelypeOleinik 's solution.) (Also, possibly unrelated to your actual problem, but you shouldn't use two-letter font macros in LaTeX. See Will two-letter font style commands (\bf , \it , …) ever be resurrected in LaTeX?.)
    – Alan Munn
    May 24, 2018 at 20:16
  • A way to a have a single command which will apply to anything I give it.
    – Mateus
    May 24, 2018 at 20:17
  • 1
    Then take a look at \newcommand argument confusion (effectively a duplicate I think.)
    – Alan Munn
    May 24, 2018 at 20:19

3 Answers 3

7

When you use \newcommand, you can give the number of "arguments" to that command. These arguments are "things" that you give to that command.

For example, a command without arguments is the one you showed:

\newcommand{\dd}{{\rm d}}

a command with one argument can be defined as:

%                ↓ number of arguments
\newcommand{\dd}[1]{{\rmfamily #1}}
%                              ↑↑ The first argument

so when you use it, the #1 is replaced by the first argument. So when you type this:

\dd{hello}

is the \dd is replaced by:

{\rmfamily hello}

The same goes for a command with more arguments. For example:

%                ↓ number of arguments
\newcommand{\dd}[2]{{#1 #2}}
%                       ↑↑ The second argument
%                    ↑↑ The first argument

so you could use, for example:

\dd{\rmfamily}{hello}

which would be replaced by

{\rmfamily hello}

Optional arguments

You can define an optional argument by specifying the default value for it:

%                ↓ number of arguments
%                   ↓-------↓ Default value for the first argument
\newcommand{\dd}[2][\rmfamily]{{#1 #2}}
%                                  ↑↑ The second argument
%                               ↑↑ The first argument

so when you type

\dd{hello}

or

\dd[\rmfamily]{hello}

the substitution is the same:

{\rmfamily hello}

but if you type

\dd[\bfseries]{hello}

you would get hello!

P.S.: Don't use \rm, it's deprecated. Use \rmfamily instead.

6

You can't set a macro with unknown name so that TeX interprets the macros name and if there are two identical letters uses {\rmfamily <letter>}, if that's what you meant. (Actually this might be possible, since you can do almost anything with TeX, but doesn't seem like a good idea to me.)

What you however can do, is writing a wrapper around your \newcommand that simplifies the definition of such macros:

\documentclass[]{article}

\newcommand*\rmify[1]
  {%
    \expandafter\newcommand\csname #1#1\endcsname{{\rmfamily #1}}%
  }
\rmify{d}
\rmify{r}

\begin{document}
\sffamily
d\dd r\rr
\end{document}

If the usage is for math mode, change the definition of \rmify in above's example to:

\newcommand*\rmify[1]
  {%
    \expandafter\newcommand\csname #1#1\endcsname{\mathrm{#1}}
  }
2
  • 1
    I didn't exactly know what OP was asking for. What you did would be my second guess :) May 24, 2018 at 20:26
  • I’m inclined to think that the definition is for math mode, maybe you can add this case.
    – egreg
    May 24, 2018 at 21:48
4

You seem to be asking for setting up TeX so that

whenever the control sequence \xx is found (with x any letter), then \mathrm{x} is produced.

However this is not possible with a simple instruction similar to the one you'd like to type. Every control sequence must have its own definition.

However, with some tricks it's possible to do “batch definitions” with a common pattern.

\documentclass{article}

\makeatletter
\count@=`\a\loop
\begingroup\lccode`X=\count255
\lowercase{\endgroup
  \@ifundefined{XX}
    {\expandafter\newcommand\csname XX\endcsname{\mathrm{X}}}
    {\typeout{not redefining \@backslashchar XX}}%
}
\ifnum\count@<`\z \advance\count@ by 1 \repeat
\makeatother

\begin{document}

\texttt{\meaning\aa} % not changed

\texttt{\meaning\bb}

\texttt{\meaning\zz}

\end{document}

This will print

enter image description here

and the console will show

not redefining \aa
not redefining \gg
not redefining \ll
not redefining \ss
not redefining \tt

Note: I assume that you want to use \dd and similar commands in math mode, but the syntax {\rm d} has been deprecated for more than twenty years; the correct syntax is \mathrm{d}.

3
  • Nice, as usual! However, even for a medium-experienced LaTeX hacker like me the underlying TeX trickery is hard to read. I assume that, especially for beginner-level questions, an answer based on LaTeX3 or at least etoolbox might be more accessible.
    – Daniel
    May 24, 2018 at 23:24
  • @Daniel You can look at tex.stackexchange.com/a/207992/4427 for a much better way.
    – egreg
    May 25, 2018 at 6:20
  • I should have done this (thought of it, but was too lazy...)
    – Skillmon
    May 25, 2018 at 6:21

You must log in to answer this question.

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