2

The command

\newcommand{\rsim}{\text{$\overset{r}{\sim}$ }}

works in text and math mode. That said, the workaround looks somewhat ugly, as text mode and math mode are summoned for a single character.

I'll use that symbol a lot in a long document, and I fear compiling will take longer because of the workaround.

Is there a better way to define a math-and-text command?

Kurzd
  • 1,017
  • What's the problem in writing $\rsim$? A math symbol is never text. Note that your definition always produces a space that's generally not wanted. So you should remove the trailing space and type \rsim{} in text, with the same number of keystrokes. I can't see any advantage. – egreg Jan 23 '16 at 17:07
  • I'll use it in text and math mode, so not having to use dollar signs makes writing a bit faster. – Kurzd Jan 23 '16 at 17:09
  • 3
    It's wrong to begin with. But if you really have hundreds of appearances, do \newcommand{\rsim}{\ensuremath{\overset{r}{\sim}}\xspace}. But be advised that this might have unwanted consequences in some circumstances; always better than a wrong space, of course. Add \usepackage{xspace}. Also taking a one-way road from the wrong side can be faster than doing a longer route, but… – egreg Jan 23 '16 at 17:11
  • What's wrong in making that command? – Kurzd Jan 23 '16 at 17:28
  • Have you ever compared the space? Try your definition with \rsim, and you'll see. – egreg Jan 23 '16 at 17:34
  • 1
    I think you should have thousands of \rsims in your document to see some noticeable compiling delay – User Jan 23 '16 at 17:38
  • Is spacing the only wrong thing in there? That incomplete phrase made me think it was much worse. – Kurzd Jan 23 '16 at 17:44

1 Answers1

9

You tagged the question as , so first I'll tell you what's the best practice:

always treat math symbols as math

So the definition should be

\newcommand{\rsim}{\overset{r}{\sim}}

to be used like

We will use the symbol $\rsim$ to denote a very useful
equivalence relation, namely $a\rsim b$ if and only if
$a$ and $b$ are equally handsome.

If you want to break the “law” that math symbols are to be always regarded as such, then you can do

\usepackage{xspace}
\newcommand{\rsim}{\ensuremath{\overset{r}{\sim}}\xspace}

and the text above can become

We will use the symbol \rsim to denote a very useful
equivalence relation, namely $a\rsim b$ if and only if
$a$ and $b$ are equally handsome.

What's the gain? None at all. You even lose syntax coloring.

See When not to use \ensuremath for math macro?

egreg
  • 1,121,712
  • I used the original \rsim definition in math mode and funny things went on with spaces. I am convinced now. – Kurzd Jan 24 '16 at 21:48