5

This looks like a unicode-math bug to me, but perhaps an expert here can confirm this.

I am presently using the Libertinus math font family with luatex for my PhD thesis. For chemical equations, I am using the mhchem package.

The problem is that inside align environments, I cannot obtain any characters in math-mode, i.e. italics (even after explicitly coaxing it by surrounding desired characters with $ ... $). Here is a minimum example.

%! TEX program = lualatex
\documentclass[varwidth=true, border=10pt, convert={size=640x}]{standalone}
\usepackage[version=4,arrows=pgf]{mhchem}
\usepackage{unicode-math}
\setmathfont{Libertinus Math} % doesn't matter. All unicode math font behaves the same way
%\setmathfont{texgyrepagella-math.otf} % doesn't matter. All unicode math font behaves the same way

\begin{document}

\begin{center}
\ce{A_x <=> B + y C} \\
\end{center}

\begin{align}
    \ce{A_x &<=> B + y C} \\
    \ce{X + Y &-> Z}
\end{align}

\begin{align}
    \ce{A_$x$ &<=> B + $y$ C} \\
    \ce{X + Y &-> Z}
\end{align}

\end{document}

In the first equation, I obtain the correct behaviour, i.e. both x and y are correctly italicised. But in the next two equations, I cannot get this desired behaviour. Especially in the third equation, even after surrounding x and y with $..$, it still typesets them upright as shown below

wrong_typesetting

If I comment the lines the lines pertaining to use of unicode math, as shown below

%! TEX program = lualatex
\documentclass[varwidth=true, border=10pt, convert={size=640x}]{standalone}
\usepackage[version=4,arrows=pgf]{mhchem}
% \usepackage{unicode-math}
% \setmathfont{Libertinus Math}  % doesn't matter. All unicode fonts have this issue
% \setmathfont{texgyrepagella-math.otf} % doesn't matter. All unicode fonts have this issue

and retain the rest of the source code, a luatex run gives me the correct output for all 3 cases, as shown below

correct typesetting without libertinus

whereupon x and y are typeset correctly in italic. Can I confirm that this is a unicode-math bug?

  • I can reproduce the behaviour. Let me add that you get the undesired output with Roman x and y as soon as you switch to math mode (also with $...$ or equation). – moewe Apr 26 '18 at 17:11
  • @moewe Thank you for confirming. The Libertinus-math developer Khaled Hosny thinks that it is an issue with unicode-math . I am wondering if I should file a bug in their github repo. – Krishna Apr 26 '18 at 17:17
  • 1
    You should definitely try and file a bug report about this. The difficult bit is where you should file the bug report. As the problem can be reproduced with other Unicode math fonts, it is not the Libertinus you should complain to. The choice is between unicode-math and mhchem. My hunch is that the mhchem developer should be the first to contact. But I see all important people know about this already. – moewe Apr 26 '18 at 21:32
3

This has been fixed with mhchem v4.08 (2018-06-22).

  • Thank you. Does align also work? – Krishna Jun 24 '18 at 20:33
  • It should. (align wasn't the cause, but math mode.) – mhchem Jun 25 '18 at 5:40
1

Although it does not answer the original question of why this issue shows up with unicode-math and mhchem combo, my answer here using the alternative chemformula package should provide a viable workaround for anyone facing this (or a similar) issue.

%! TEX-program = lualatex
\documentclass[varwidth=true, border=10pt, convert={size=640x}]{standalone}
% \usepackage[version=4,arrows=pgf]{mhchem}
\usepackage{chemformula}
\usepackage{unicode-math}
\setmainfont[Numbers={Proportional},Ligatures={TeX, Common%, Historic, Contextual, Rare, Discretionary
}]{Libertinus Serif}
\setmonofont[Scale=0.8]{Libertinus Mono}
\setsansfont{Libertinus Sans}

\begin{document}

\begin{center}
    \ch{A_{$x$} <=> B + $y$ C} \\
\end{center}

\begin{align}
    \ch{A_{$x$} &<=> B + $y$ C} \\
    \ch{X + Y &-> Z}
\end{align}

\end{document}

Note that mhchem has been commented out and instead we use chemformula. Note also the additional grouping required for the subscripted math-mode x when using chemformula.

Anyway, we obtain the desired output as shown here:

output_using_chemformula

Perhaps a reasonable alternative would be to use chemformula until this issue is fixed. (It is unclear by who: unicode-math team or mhchem developer?).

1

The problem is that in math mhchem surrounds everything with \mathrm. And this change the \Umathcode. You could try to add as a hook \symit. But it doesn't work if the argument doesn't consist of a single char (mhchem actively sets in this case the argument in roman also without unicode-math).

\documentclass{article}
\usepackage[version=4,arrows=pgf]{mhchem}
\usepackage{unicode-math}

\begin{document}

$\ce{A_{\symit{x}}}$

\makeatletter
\def\mhchem@hook@beforeItalicMath{\symit}

$ \ce{A_x} \ce{A_{xxxx}} $

 %not a solution but only to demonstrate that removing the \mathrm avoids the problem:
\def\mhchem@option@mathFont{}%{\mathrm}

${\ce{A_x}}$


\end{document}

enter image description here

  • Yes. If it doesn't suffer from this issue, maybe I can use chemformula instead of mhchem ? I need to proceed with my PhD thesis anyway. The removal of \mathrm is not a solution for me since it makes everything italic. – Krishna Apr 26 '18 at 18:11
  • I can't tell you if you should switch. Be aware that explicit markup ($\ce{A_{\symit{xxxx}}}$) will work. – Ulrike Fischer Apr 26 '18 at 18:24
  • Hmm, reading this from page 3 of the official chemformula manual gives me some hope: "Another notable dierence: chemformula tries to avoid math mode whenever possible" – Krishna Apr 26 '18 at 18:30
  • 1
    But you wan't math mode for the subscript. So don't let go hope to high and run some tests first. – Ulrike Fischer Apr 26 '18 at 18:32
  • Yes. It works!! As per page 22 of the official chemformula manual: "If you especially want to input math you just enclose it with $ $". I tried it and it works. I shall post the code with chemformula as an answer (suggesting it as a possible workaround to others facing similar issues). – Krishna Apr 26 '18 at 19:17

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.