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.

9

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

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

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.

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