Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

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}}}

. Thanks for the help!

share|improve this question
    
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 at 8:46
1  
@morbusg No! This is a bug in unicode-math that really should be fixed –  David Carlisle May 18 at 9:31

1 Answer 1

up vote 7 down vote accepted

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 http://tex.stackexchange.com/a/100428/4427

share|improve this answer
    
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 at 9: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 at 9:39
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 at 11:33
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 at 14:04
1  
@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 at 15:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.