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.

1
  • 1
    convert - to text by typing the following \textsf{-} Commented May 7, 2021 at 11:58

3 Answers 3

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

7
  • 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
    Commented Sep 4, 2014 at 13:10
  • 1
    @LaRiFaRi good suggestion - incorporated.
    – greyshade
    Commented Sep 4, 2014 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. Commented Sep 4, 2014 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
    Commented Sep 4, 2014 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
    Commented Sep 4, 2014 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.

5
  • Ah, I wondered if there would be a trick like that. I'll experiment a bit and see what qualifies as "least ugly" :)
    – Roly
    Commented Sep 4, 2014 at 13:13
  • @Roly an additional plus: \mhyphen obeys to the current font and size settings.
    – Bordaigorl
    Commented Sep 4, 2014 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
    Commented Sep 4, 2014 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
    Commented Sep 4, 2014 at 15:30
  • I do like this answer, but in the end I chose the other one for simplicity. Thanks.
    – Roly
    Commented Sep 5, 2014 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .