5

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

\documentclass{article}

\newcommand{\nuSwap}{\nu-\textsf{swap}}

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

enter image description here

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

Roly
  • 4,221
  • 4
  • 26
  • 56

3 Answers3

10

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:

\newcommand{\nuSwap}{\nu\textsf{-swap}}

in which case:

\documentclass{article}

\newcommand*{\nuSwap}{\ensuremath{\nu\textsf{-swap}}}

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

And \nuSwap ping also works in maths mode:
\[\nuSwap\]
\end{document}

gives:

output

greyshade
  • 3,576
  • 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 04 '14 at 13:10
  • 1
    @LaRiFaRi good suggestion - incorporated. – greyshade Sep 04 '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 04 '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 04 '14 at 18:05
  • @Roly I guess that's a matter of taste, since the word following it is in the sf font, one might argue so should the hyphen be - especially since the character before it is a math character, to some extend falling out of the general picture anyway (if appearing within a text). of course \text{-}\textsf{swap} will work just fine if that's more to your liking.. – greyshade Sep 04 '14 at 18:08
  • Agreed, it doesn't affect the suitability of your answer. Although I'm inclining towards the \mhyphen answer below, having read this. – Roly Sep 04 '14 at 18:12
  • 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 04 '14 at 18:20
3

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:

\mathchardef\mhyphen="2D
\newcommand{\nuSwap}{\nu\mhyphen\textsf{swap}}

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:

\documentclass{article}
\usepackage{hyperref}

\mathchardef\mhyphen="2D
\newcommand{\nuSwap}{\texorpdfstring{\ensuremath{\nu\mhyphen\textsf{swap}}}{nu-swap}}

\begin{document}
\section{Definition of \nuSwap}

Here we define \nuSwap:

\[
    \nuSwap = \dots
\]
\end{document}

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

Bordaigorl
  • 15,135
  • 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 04 '14 at 13:13
  • @Roly an additional plus: \mhyphen obeys to the current font and size settings. – Bordaigorl Sep 04 '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 04 '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 04 '14 at 15:30
  • I do like this answer, but in the end I chose the other one for simplicity. Thanks. – Roly Sep 05 '14 at 10:13
2

The hyphen should be in sans serif style. However, using \textsf{-swap} might render it in italics, if in a context where italics is used (theorem statements, for instance).

Safe solution: use the same trick as amsmath for using a hyphen in the argument to \operatorname instead of a minus sign.

\documentclass{article}
\usepackage{amsmath}

\makeatletter \newcommand\nuSwap{\nu\mathsf{\newmcodes@-swap}} \makeatother

\begin{document}

I want $\nuSwap$

It also respects bold math {\boldmath$\nuSwap$}

\end{document}

enter image description here

egreg
  • 1,121,712