What's the easiest/least ugly way to avoid the spaces around the hyphen in the following macro?



I get $\nuSwap$, but I want $\nu$-\textsf{swap}.

enter image description here

I would like a macro I can use easily when I'm already in math-mode.


In math mode (as in your $\nuSwap$) the - becomes a minus sign and is thus longer and has spaces around it. Simply preventing the - from being treated as maths as you do with the word swap gives what you want, e.g. by redefining:


in which case:



You want \nuSwap, here's \(\nu\)-\textsf{swap}.

And \nuSwap ping also works in maths mode:



| improve this answer | |
  • Ok, that's pretty painless. (Although in my code, I need to wrap the - in \text{-} separately, because the swap itself is coming from another macro.) – Roly Sep 4 '14 at 13:10
  • 1
    @LaRiFaRi good suggestion - incorporated. – greyshade Sep 4 '14 at 13:13
  • 3
    a small consequence of the way you've defined \nuSwap, though you don't mention it, is that it won't be broken after the hyphen at the end of a line, should a line break be needed. that's probably a good thing, in general, though it's well to be aware that it happens. – barbara beeton Sep 4 '14 at 14:01
  • Also worth noting that the hyphen is now in the sans serif font - can this affect the rendering? Maybe \text{-}\textsf{swap} is better. – Roly Sep 4 '14 at 18:05
  • 1
    @Roly I'm not sure it makes any difference in this particular case, since your \textsf{swap} would ignore things like \boldmath anyway, which in my eyes makes a bold hyphen look strange is the swap is not bold.. see here – greyshade Sep 4 '14 at 18:20

As already mentioned, the hyphen is interpreted as a minus in math mode. To correct the issue you could define a macro \mhyphen that stands for the hyphen in math mode:


this can of course be combined with \ensuremath to get a macro that works in both modes.

I also recommend using \texorpdfstring (from hyperref) if you intend to use it in headers and produce a pdf:



\section{Definition of \nuSwap}

Here we define \nuSwap:

    \nuSwap = \dots

if you inspect the generated PDF you'll find "nu-swap" is used in the index.

| improve this answer | |
  • Ah, I wondered if there would be a trick like that. I'll experiment a bit and see what qualifies as "least ugly" :) – Roly Sep 4 '14 at 13:13
  • @Roly an additional plus: \mhyphen obeys to the current font and size settings. – Bordaigorl Sep 4 '14 at 13:24
  • Is that not also true of escaping to text mode via \text{-}? (Admittedly this is not quite the same as the other answer, but it's similar in spirit.) (Oh and thanks for embellishing your answer.) – Roly Sep 4 '14 at 13:31
  • To my knowledge \mhyphen would be a proper math-mode character, thus obeying to the current font settings in that mode (think subscripts etc...) but I have not tested \text{-} against this... – Bordaigorl Sep 4 '14 at 15:30
  • I do like this answer, but in the end I chose the other one for simplicity. Thanks. – Roly Sep 5 '14 at 10:13

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.