5

Whenever I write a hyphenated word inside \mathit or \mathbf or whatever ...

\mathit{long-variable-name}

... I always intend for the hyphens to be hyphens, not minus signs. There is a webpage giving several workarounds for this (http://www.logic.at/staff/salzer/etc/mhyphen/), but I would like a proper solution where I don't have to write monstrosities like

\mathit{long\mhyphen{}variable\myhyphen{}name}

My vision is to redefine the \mathit command (and its cousins) so that when - appears in its argument, it is automatically replaced with whatever command is necessary to produce a hyphen.

  • Is this possible?

  • Might doing so cause problems? I mean, do people ever put a whole formula inside \mathit? ('Cause then you would want the - to produce a minus.)

4
  • 1
    Is the text and math fonts different in your document?, because then I'd just use \textit. BTW: long function names should be typeset upright not italic.
    – daleif
    Aug 15, 2014 at 15:06
  • 1
    @daleif Thanks, I've changed it from 'function-name' to 'variable-name'. Yeah, my document (regrettably) uses different fonts for math and non-math. Aug 15, 2014 at 15:10
  • 1
    @JohnWickerson -- what's regrettable about using different fonts for math and non-math? having different fonts allows a one-letter variable a to be distinguishable from the article "a" in an italicized theorem (english; other possibilities in other languages). Aug 15, 2014 at 15:24
  • 1
    Heh, I thought that might be contentious! I dislike having different fonts for math and non-math, mainly because I think {rm,it,tt,sf,sl,bf}x{math/nonmath} is just too many fonts to have in one document. (And I'm never sure whether to write $42$ or just 42; if it's the same font then I don't have to worry!) Aug 15, 2014 at 15:31

2 Answers 2

7

You can use \newmcodes@ from amsopn, which is used for \operatorname. I would not redefine \mathit and similar commands.

\documentclass{article}
\usepackage{amsmath} % actually amsopn

\makeatletter
\DeclareRobustCommand{\varname}[1]{\begingroup\newmcodes@\mathit{#1}\endgroup}
\makeatother

\begin{document}
$\varname{long-variable-name}-\varname{short}=0$
\end{document}

enter image description here

You could change the meaning of \mathit, but I strongly advise you not to:

\usepackage{letltxmacro,amsmath}

\LetLtxMacro\ORImathit\mathit
\makeatletter
\DeclareRobustCommand{\mathit}{%
  \begingroup\newmcodes@\ORImathit{#1}\endgroup
}
\makeatother
7
  • Super, thanks. I'll just add that if anyone did want to go ahead and meddle with \mathit despite your advice, they should write \makeatletter \let\oldmathit\mathit \renewcommand\mathit[1]{\begingroup\newmcodes@\oldmathit{#1}\endgroup} \makeatother Aug 15, 2014 at 15:34
  • Note, that the first version of @egreg's solution didn't solve the protecting of \varname from \write too.
    – wipet
    Aug 15, 2014 at 18:01
  • @egreg You are not precisely right. Try my solution with TOC without protecting. The line includes somethig like: ...${\mathcode -=- \mathit{var-name}}$... in the .toc file and this is readable again without problems. It means: my solution needs not protecting because it is very simple based on primitives. Simplicity is power. And now, try the solution from your first version with \varname in the title. The .toc line includes garbage and the sedond run crashes. And crash all consecutive runs until somebody remove .toc file.
    – wipet
    Aug 16, 2014 at 4:35
  • @wipet I can't understand what you're after. I showed you that \let\mathitori=\mathit is wrong (it is notwithstanding the fact that one test makes it work) and changed my answer to protect \varname because I realized the problem. Simplicity is not power: writing \mathcode`-=`- in the .aux file doesn't seem the best idea and, if you think about it, you'll get two of these assignments in the .toc file and you'll do three of them when typesetting the TOC.
    – egreg
    Aug 16, 2014 at 6:28
  • @egreg The \let\mathitori=\mathit isn't wrong if the user exactly knows what happens and how TeX works inside. And believe me: I know about it.
    – wipet
    Aug 16, 2014 at 7:04
4

The typesetting of all characters in math mode depends on \mathcode of such character. You can simply re-set mathcode of -.

\let\mathitori=\mathit
\def\mathit#1{{\mathcode`-=`- \mathitori{#1}}}
5
  • Thanks wipet. The extra {...} in your \def -- is that the same as egreg's \begingroup...\endgroup? Aug 15, 2014 at 17:05
  • Yes, we create variable as Ord math object, this means {...}. Moreover this localizes the \mathcode setting. If you dislike this, you can type \def\mathit#1{\mathitori{\mathcode`-=`- #1}} because the argument of \mathit is typeset in the group.
    – wipet
    Aug 15, 2014 at 17:10
  • It's exactly what \newmcodes@ does, but it changes also other mathcodes (closing quote ', asterisk *, period ., slash / and colon :). Note also that \let\orimathit\mathit is quite dangerous if \mathit gets in a \protected@write, see tex.stackexchange.com/a/47372/4427
    – egreg
    Aug 15, 2014 at 17:10
  • @egreg The question was about hyphen only. I did show how TeX works inside by mentioning the \mathcode primitive. And I didn't use any @ in control sequences. Of course, protecting from \write is implemented in LaTeX no very ideal, so I didn't solve this. I have much better \write protecting in my OPmac, for example:)
    – wipet
    Aug 15, 2014 at 17:27
  • @JohnWickerson If you really needs to use \mathit in \chapter/\section parameters then you can replace \def\mathit... by \protected\def\mathit.... The \protected primitive is from eTeX.
    – wipet
    Aug 15, 2014 at 17:58

You must log in to answer this question.

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