[Tex/LaTex] How to get the XITS font to work with unicode-math and hepnames in lualatex

fontsluatexunicode-math

I have been trying to get the XITS font, hepnames and unicode-math to behave correctly together for quite some time now, but I have been unable to find a solution to my problem.

Perhaps, I have been searching wrong… the closest I could find to my problem was this question, so I would appreciate any help with my issue, even if it is just pointing me to a link with relevant information.

The problem itself is quite easy to describe, the hepnames package provides a series of macros to typeset particle symbols, from what I have been able to gather it uses mathmode for this, which requires me to use unicode-math to change the font. After setting this up, it seems that some of the macros work and others do not, and when the macros do not work, there is no output (for that macro) in the generated pdf file. I tried running the same file with XeLaTeX, just to confirm it was not a LuaLaTeX bug or something, and the output was nearly identical, with the only difference that XeLaTeX would output empty boxes where the failing macros are. Also, moving the order of the commands in the preamble changes the macros that do and do not work.

Here is a MWE:

\RequirePackage[l2tabu, orthodox]{nag}
\documentclass[12pt,a4paper,twoside]{report}

\usepackage{hepnames}

\usepackage{fontspec}
% With setmainfont here, the \Pgm and \Pgt macros work
\setmainfont
[    Extension = .otf,
   UprightFont = *-regular,
      BoldFont = *-bold,
    ItalicFont = *-italic,
BoldItalicFont = *-bolditalic,
]{xits}

\usepackage[math-style=ISO]{unicode-math}

% With setmainfont here, the \Pl macro works
\setmathfont
[    Extension = .otf,
      BoldFont = *bold,
      Ligatures=TeX,
]{xits-math}

\begin{document}

Macros being tested:
\begin{itemize}
  \item $\Pgm$ should print $\mu$-like
  \item $\Pgt$ should print $\tau$-like
  \item $\Pgn$ should print $\nu$-like % I was unable to get this macro, \Pgn, to work under any circumstance
  \item $\Pl$ should print an ell
\end{itemize}

\end{document}

I tried looking into the log files. When the setmainfont command is in the second position in the MWE (the one where only the comment is), the log has the following 3 lines inside:

Missing character: There is no 𝜇 (U+1D707) in font "[xits-regular.otf]:mode=base;script=latn;language=DFLT;"!
Missing character: There is no 𝜏 (U+1D70F) in font "[xits-regular.otf]:mode=base;script=latn;language=DFLT;"!
Missing character: There is no 𝜈 (U+1D708) in font "[xits-italic.otf]:mode=base;script=latn;language=DFLT;"!

And when the setmainfont command is in the position exactly like the MWE, I get similar messages but, the weird part here, for a font that is not XITS (cmti10 is the computer modern font, right?):

Missing character: There is no 𝜈 (U+1D708) in font cmti10!
Missing character: There is no ℓ (U+2113) in font cmti10!

And this final part of the logs just confuses me. Why does it not use the xits font? Why does it not use the xits-math font when it does use the xits font? Why can it not find the characters for mu, tau, nu? As shown in the MWE, it does find those characters when I use $\mu \tau \nu$

As a final note, I am working on a mac and according to the TeX Live Utility, everything on my system is up to date (TeX Live 2015).

This is my first post on a stackexchange website, even though I have been a long time lurker, so please forgive me if I am doing something wrong. Also, despite me knowing quite a bit of LaTeX, I am not an expert user but I will be happy to try any suggestions.

Best Answer

The package is not adapted to unicode-math. It uses e.g. \mathit where with unicode-math you should use \symit. In your example you can solve the problem with this patch, but there more places which need to be corrected. You should contact the author.

\documentclass[12pt,a4paper,twoside]{report}

\usepackage{hepnames}

\usepackage{fontspec}
% With setmainfont here, the \Pgm and \Pgt macros work
\setmainfont
[    Extension = .otf,
   UprightFont = *-regular,
      BoldFont = *-bold,
    ItalicFont = *-italic,
BoldItalicFont = *-bolditalic,
]{xits}

\usepackage[math-style=ISO]{unicode-math}

% With setmainfont here, the \Pl macro works
\setmathfont
[    Extension = .otf,
      BoldFont = *bold,
      Ligatures=TeX,
]{xits-math}
\usepackage{xpatch}
\makeatletter
\xpatchcmd\@HepGenStyle{\mathit}{\symit}{}{\show\fail}
\makeatother
\begin{document}

Macros being tested:
\begin{itemize}
  \item $\Pgm$ should print $\mu$-like
  \item $\Pgt$ should print $\tau$-like
  \item $\Pgn$ should print $\nu$-like % I was unable to get this macro, \Pgn, to work under any circumstance
  \item $\Pl$ should print an ell
\end{itemize}

\end{document}

enter image description here

Edit

The correct place for the \setmainfont is behind unicode-math, as this will set \mathit to the italic version of xits and not to computer modern.

In this case one then has to patch another command and replace \mathup. The symbols are upright now. If this is not wanted, use \usepackage[italic]{hepnames}.

\documentclass[12pt,a4paper,twoside]{report}

\usepackage{hepnames}

\usepackage[math-style=ISO]{unicode-math}

\setmainfont
[    Extension = .otf,
   UprightFont = *-regular,
      BoldFont = *-bold,
    ItalicFont = *-italic,
BoldItalicFont = *-bolditalic,
]{xits}

% With setmainfont here, the \Pl macro works
\setmathfont
[    Extension = .otf,
      BoldFont = *bold,
      Ligatures=TeX,
]{xits-math}

\usepackage{xpatch}
\makeatletter
\xpatchcmd\@HepGenStyle{\mathit}{\symit}{}{\show\fail}
\xpatchcmd\@HepConStyle{\mathup}{\symup}{}{\show\fail}
\makeatother
\begin{document}

Macros being tested:
\begin{itemize}
  \item $\Pgm$ should print $\mu$-like
  \item $\Pgt$ should print $\tau$-like
  \item $\Pgn$ should print $\nu$-like % I was unable to get this macro, \Pgn, to work under any circumstance
  \item $\Pl$ should print an ell
\end{itemize}

\end{document}

enter image description here

Related Question