0

In this example

\documentclass{article}
\newcommand{\Tt}{\ensuremath{t}}
\begin{document}
We study \Tt as single items.
\end{document}

I have defined a \newcommand \Tt where I use \ensuremath so that \Tt have a dual capacity to be inserted in plain text and in maths mode. In text however the space before \Tt vanishes

enter image description here

I fix the problem by adding xspace (requires \usepackage{xspace}):

\newcommand{\Tt}{\ensuremath{t}\xspace}.

The outcome is appealing:

enter image description here

A problem becomes apparent when I write:

We study \Tt\Tt pairs.

A space has appeared between the \Tt's

enter image description here

If I would really want a space between the \Tt's I would have added it myself. How to approach this problem?

Viesturs
  • 7,895
  • 1
    If don't use xspace is not an acceptable answer, you may want to try \xspaceaddexceptions{\Tt}. – moewe Nov 10 '18 at 14:30
  • @moewe, how exactly? \Tt in \xspaceaddexceptions{\Tt} disappeared. – Viesturs Nov 10 '18 at 14:32
  • 1
    Is there a real reason for not typing We study $t$ as a single item and $tt$ as a pair? \Tt is three keys as well as $t$, \Tt\Tt is six instead of four. Even if you want to define a command (and there are good reasons to), $\Tt$ is way clearer and less error prone than using \ensuremath. – egreg Nov 10 '18 at 14:33
  • Sorry, I don't quite follow. What are you saying with "\Tt in \xspaceaddexceptions{\Tt} disappeared."? The command should be used once in the preamble to avoid \xspace adding the unnecessary space before \Tt. You are not supposed to use it instead of \Tt in We study \Tt as single items.. – moewe Nov 10 '18 at 14:35
  • @moewe, adding \xspaceaddexceptions{\Tt} in preamble works. Will you supply an answer? – Viesturs Nov 10 '18 at 14:37
  • @egreg, \Tt is heavily used in the document and it should be differentiated from $t$'s not having the same meaning as \Tt. And I simplify my examples. Imagine, $t^{3}_{a}$. – Viesturs Nov 10 '18 at 14:40
  • 1
    @Viesturs I said that there are indeed good reasons for doing \newcommand{\Tt}{t} (so that you can change your mind later about how to represent the object). There is no good reason for using \ensuremath. – egreg Nov 10 '18 at 14:50
  • 1
    This is definitely a case where you should not use \xspace (sometimes I regret publishing that package at all:-) – David Carlisle Nov 10 '18 at 18:02
  • 1
    "If I would really want a space between the \Tt's I would have added it myself." that basically is why \xspace isn't useful, it is much simpler to add the space when you need one, than try to tune its automatic guess of when a space is needed. – David Carlisle Nov 10 '18 at 22:30

1 Answers1

2

You can tell xspace not to add a space before \Tt with

\xspaceaddexceptions{\Tt}

As pointed out in the comments by David Carlisle this will stop xspace from adding space before \Tt in all situations, even if you would otherwise like to see it, say because you have

\newcommand\study{\study\xspace}

and then

We \study \Tt.

(David's example.)

Since it has been brought up in comments under the question and under the answer, let me also mention in this answer that it would be preferable not to use \ensuremath and \xspace in this situation at all. Forcing yourself into math mode where math mode is appropriate has the advantage that you probably won't write something like \Tt - \Tt in text mode, which is nearly always not what you wanted. You might save a few keystrokes, but you lose clarity and run the risk of producing unfortunate constructions like \Tt - \Tt. Plus if you are forced to use math mode, the issue with macros eating the following space goes away... For reasons not to use xspace see also Drawbacks of xspace, for \ensuremath there is When not to use \ensuremath for math macro?.

I strongly recommend you use the \Ttm in the example below, i.e. no \xspace and no \ensuremath.

\documentclass{article}

\usepackage{xspace}
\newcommand{\Tt}{\ensuremath{t}\xspace}
\xspaceaddexceptions{\Tt}

\newcommand{\Ttm}{t}

\begin{document}
We study \Tt pairs.

We study $\Ttm$ pairs.

We study \Tt\Tt pairs.

We study $\Ttm\Ttm$ pairs.
\end{document}
moewe
  • 175,683
  • 2
    while this is of course an answer to the question, neither \ensuremath nor \xspace should be used here, also the exception is saying never to put a space before \Tt which isn't really right, if you had \newcommand\study{\study\xspace} then this would mean that \study \tt had no space after study. – David Carlisle Nov 10 '18 at 18:00
  • 1
    @DavidCarlisle Oh yes, that is a good point, I didn't think of other macros using \xspace as well. Since you and egreg both advocate against \ensuremath and \xspace I have allowed myself to add a short paragraph echoing these sentiments in the answer. – moewe Nov 10 '18 at 22:08
  • Some people have created \xpace and \ensuremath while other people strongly advocate against their use. – Viesturs Nov 11 '18 at 10:44
  • @Viesturs But at least for \xspace the intersection of the set of creators and set of people who don't recommend its use is non empty. For \ensuremath I believe that does not hold. And \ensuremath certainly has its uses, but this is very likely not one of them. – moewe Nov 11 '18 at 10:47
  • Plus I don't believe that only the developer/creator of a macro/package should be allowed to discourage its use. What about \it, $$? Would you only allow Donald Knuth to say anything about this? – moewe Nov 11 '18 at 10:53