16

I am using lhs2TeX to convert code into LaTeX. It typesets code into math mode. As I am also writing math formulas in the same document, I use unicode-math to switch between different math mode fonts in order to make the two things more distinct (and also to use Unicode symbols in my math).

lhs2TeX sets variable identifiers using a macro \Varid, which is usually set to \mathit.

The problem here is that with unicode-math, \mathit does not work correctly. I am unsure whether this is a problem with unicode-math or the OpenType math fonts, but the bottom line is that it does not typeset its argument as text but like a formula, with additional spacing between the letters. I also found a bug report on this.

I could redefine \Varid with \textit, but this leads to another problem: There are a lot of subscripts in my identifiers, and as the underscore doesn't do subscripts in text mode, this doesn't work. At least not without me overriding every identifier with custom TeX to fix the subscripts.

Here is an example demonstrating the problem.

\documentclass{minimal}

\usepackage{fontspec}
\setmainfont{TeX Gyre Pagella}

\usepackage{unicode-math}
\setmathfont[version=lm]{Latin Modern Math}
\setmathfont[version=pg]{TeX Gyre Pagella Math}


\begin{document}

\mathversion{lm}
% math in latin modern
    $x ∼ y$

\mathversion{pg}
% haskell code in tex gyre pagella

    \textit{factorial}

    $\mathit{factorial}$ \quad This should look like the former, not the latter.

    $factorial$

\mathversion{lm}
% latin modern again
    $x ∼ y$

\end{document}

Screenshot of the Output
(source: goedderz.info)

So the question is: Can mathit be fixed in this setting? If not, can I write a macro that works in math mode, but chooses the correct font? Or can I write a macro which locally redefines the underscore to do subscripts in text mode?

Edit: A main problem here, that unfortunately isn't part of my example, is that the TeX code inside \Varid (which can be redefined) is usually automatically generated and often contains _ for subscripts (or else must be rewritten manually for every identifier which I try to avoid). So using \text instead is problematic.

Edit: Actually, that is not quite true, as something like x1 gets translated to \Varid{x}_1, which works when defining \Varid as something like

\newfontfamily\haskvarfont{TeX Gyre Pagella}
\renewcommand{\Varid}[1]{\text{\haskvarfont\emph{#1}}}
moewe
  • 175,683
  • They look correct to me; You should use \textit for function names and such. For super-/subscript, just use, for example, \textit{factorial}_{\textit{foo}}. See this question for why \mathit works the way it does. Oh, and welcome to tex.stackexchange! – morbusg May 18 '14 at 08:46
  • 2
    @morbusg No! This is a bug in unicode-math that really should be fixed – David Carlisle May 18 '14 at 09:31

2 Answers2

11

This is the consequence of a bad decision in unicode-math: they named \mathit the normal math italic letters, instead of respecting the LaTeX convention of referring to them as \mathnormal; \mathit should choose the text italic font, in order to make unicode-math a drop-in replacement.

I wouldn't expect different output when unicode-math is loaded or not; but this simple example shows the bug:

\documentclass{article}
%\usepackage{unicode-math}

\begin{document}

$\mathit{different}$

$different$

\end{document}

If the line with unicode-math is commented out, we get

enter image description here

If I uncomment the line, I get

enter image description here

which is definitely wrong.

Workaround:

Define a new math alphabet:

\documentclass{article}
\usepackage{unicode-math}

\DeclareMathAlphabet{\Lmathit}{\encodingdefault}{\familydefault}{m}{it}

\begin{document}

$\Lmathit{different}$

$different$

\end{document}

If you use lhs2TeX, you can add

\renewcommand{\Conid}[1]{\Lmathit{#1}}
\renewcommand{\Varid}[1]{\Lmathit{#1}}

after loading it.

This shouldn't raise the Too many math alphabets error; if it does, then add the code you find between \makeatletter and \makeatother in https://tex.stackexchange.com/a/100428/4427

egreg
  • 1,121,712
  • Something along those lines was my first workaround as well: I can redefine \Varid. However, the TeX code inside is automatically generated and often contains _ for subscripts. Using \text breaks this. – Tobias Gödderz May 18 '14 at 09:36
  • @TobiasGödderz If I say $x_\Varid{ff}$ I get the expected result. I added a pair of braces for safety. – egreg May 18 '14 at 09:39
  • But $\Lmathit{x_1}$ doesn't. – Tobias Gödderz May 18 '14 at 09:41
  • @TobiasGödderz \mathit{x_1} is wrong to begin with; it should be \mathit{x}_1 – egreg May 18 '14 at 09:43
  • Ah, you're right. And actually, lhs2TeX does generate code that way -- it was an identifier I formatted myself that prevented your example from working. – Tobias Gödderz May 18 '14 at 09:53
  • @TobiasGödderz I've changed the workaround to a better one. – egreg May 18 '14 at 10:02
  • 2
    Naming “mathematical italic” \mathit doesn’t sound like a bug to me. If something else uses “mathematical italic” to mean “text italic”, then the bug is IMHO in that. – morbusg May 18 '14 at 11:33
  • 2
    @morbusg The problem is not in the name, but in the fact that, for LaTeX, \mathit means using the text italic font (as opposed to the normal math italic). Maybe the name was badly chosen twenty years ago; too late for changing the convention. – egreg May 18 '14 at 12:22
  • 2
    Welp, if it were too late for changing conventions in general, we’d still be watching the sun revolve around our flat Earth… Calling it a bug in unicode-math just feels very wrong to me. – morbusg May 18 '14 at 14:04
  • @egreg I wouldn't say that \mathit{x_1} is intrinsically wrong. Indeed fontmath.ltx contains \DeclareMathSymbol{1}{\mathalpha}{operators}{\1}etc, hence one can do\mathbf{1_2}if one wishes to. No guarantee of course this gives the same bold1as\boldsymbol {1}`. –  May 18 '14 at 14:34
  • @morbusg what is wrong, but explained by the historical constraints, is that TeX only envisions italic fonts for math letters, hence LaTeX does provide \mathnormal where there should be a \mathnormalit and also \mathnormalrm and perhaps a \mathnormalother which would give upright letters if the user respects the default italic setup, and italic letters if the user has done something like \mathupright. –  May 18 '14 at 14:45
  • @morbusg -- while the name \mathit is a latex introduction, "math italic" is knuthian terminology; the plain tex command is \mit, and the concept is introduced on p.164 of the texbook. the terminology may be confusing, but some things won't change as long as you're using tex of any flavor. – barbara beeton May 19 '14 at 15:07
  • @barbara: Confusing? No; I know \mit does \fam1 on plain, and I even linked on the OP to the question which deals with the differences between the two italics. – morbusg May 19 '14 at 15:30
  • 2
    @jfbu In the olden times one had to say ${\it func}(x)$, which LaTeX2e removed preferring $\mathit{func}(x)$. However the name shouldn't be read “math italic”, but “italics used in math”. With LaTeX2e, the declaration \mit (taken from Plain TeX), became the command with an argument \mathnormal. – egreg May 19 '14 at 15:42
  • 1
    @morbusg -- i've now gone back and read the linked item on github. in that, will robertson has asked if \mit should be used for the new unicode material. i've written to will suggesting very strongly that this is a bad idea, and cited the texbook reference. one could wish the terminology were different, but it's not; egreg's phrasing -- "italics used in math" -- is probably the best/easiest way to remember the concept "correctly" (i.e., as it is used in tex). – barbara beeton May 19 '14 at 16:08
  • After reading the github conversation, I am starting to question my own sanity. Just wanted to throw that out there. ;-D – morbusg May 19 '14 at 18:05
  • @egreg: Your workaround doesn't work with TeXLive 2014 and LuaLaTeX. Could you update your solution? – Géry Ogam May 22 '15 at 21:41
  • 1
    @Maggyero There's no update, unfortunately. The upgrade from TL2013 to 2014 broke everything. :( – egreg May 22 '15 at 21:45
  • @egreg: That's a serious issue. Did Will Robertson correct this for TeXLive 2015 in his unicode-math package? (It's about to be frozen so I hope so.) – Géry Ogam May 22 '15 at 21:48
  • @Maggyero No, it does the same in TL2015/pretest – egreg May 22 '15 at 21:50
  • @egreg: Is he aware of the problem? – Géry Ogam May 22 '15 at 21:52
  • @Maggyero Yes, he is. – egreg May 22 '15 at 22:12
  • 1
    @morbusg Whether the earth is flat could not be a matter of convention. We might have a convention of saying so or thinking so, but we couldn't have one of its being so. – cfr Aug 28 '16 at 03:21
1
\documentclass{article}
\usepackage{unicode-math}
\begin{document}

$\mathit{different}$

$\symit{different}$

$different$

\end{document}

enter image description here

user187802
  • 16,850