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?

  • 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
  • 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
6

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?

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.