2

I use the mhchem package for chemical formulas (with XeTeX). For one year or so, I’ve been experiencing some really strange bugs, seemingly without reason. Some characters in the chemical formulas, especially greek letters (\alpha, \delta, etc.) would disappear. This was really annoying and there was no way to solve the bug, so I ended up replacing \delta with d, \alpha with a, etc. Not optimal, but I felt like it was not worth more hassle.

Today I spent a long time trying to do a minimal working exemple to finally solve this issue and I found the cause of the problem: an evil interaction with fontspec and packages linked to fontspec such as xeCJK (a package provides support for small parts in Chinese, Japanese or Korean).

Here are MWEs. In this case, no issue is encountered.

\documentclass[12pt]{article}

\usepackage{mhchem}

\begin{document}
$\ce{BaCe_{0.8}Y_{0.2}O_{3-\alpha}}$
\end{document}

In this case, the alpha is not typeset.

\documentclass[12pt]{article}

\usepackage{fontspec}
\usepackage{mhchem}

\begin{document}
$\ce{BaCe_{0.8}Y_{0.2}O_{3-\alpha}}$
\end{document}

Where could this problem come from, and how to solve it?

  • If you check your log file, it should say that Latin Modern font has no α character. Try loading a font that does support Greek, see tex.stackexchange.com/a/163149 – Troy Dec 21 '17 at 5:03
  • I don’t really need Greek as a language though, I just want to use mathmode elements in chemical formulas (in other words I want to use $\alpha$ and $\delta$, not actual greek characters as we would find in a greek font or in a text in Greek). It’s strange that these elements are not typeset in formulas, even in math mode, just because fontspec is loaded — outside mhchem chemical formulas they do work. – user96829 Dec 21 '17 at 5:46
  • To be a little bit more precise, I’d say the actual question is ‘how to prevent fontspec from replacing math characters in mhchem formulas with characters that don’t exist’ rather than ‘how to find fonts where these characters actually exist’ – user96829 Dec 21 '17 at 5:49
  • 1
    xeCJK is a package to typeset documents whose main language is CJK (especially Chinese). It does lots of things other than switching fonts (e.g., automatic insertion of spaces that are needed in CJK typography), and these can be harmful in Western language documents (I'm not sure what's your main language though). I think you shouldn't avoid the hassle, but this is just a personal opinion of course ;-) – yudai-nkt Dec 21 '17 at 6:03
  • 1
    I recommend you change your MWE to replace xeCJK with fontspec. – Nicola Talbot Dec 21 '17 at 12:59
5
+50

Answer to your modified question

how to prevent fontspec from replacing math characters in mhchem formulas with characters that don’t exist (excerpted from this comment)

  1. [Recommend] Re-enter the math mode
  2. Cancel the Greek font mapping globally

Example:

\documentclass{article}
\usepackage{fontspec}
\usepackage[version=4]{mhchem}

\begin{document}
Re-enter the math mode: \par
$\ce{BaCe_{0.8}Y_{0.2}O_{3-$\alpha$}}$

\mhchemoptions{text-greek=default, math-greek=default}
\bigskip

Cancel the Greek font mapping: \par
$\ce{BaCe_{0.8}Y_{0.2}O_{3-\alpha}}$
\end{document}

Result: result of solutions


Explanations for the solution, and others

  1. Why does fontspec interact with mhchem?

    mhchem loads package chemgreek, the later one provides an interface for upright greek letters for use in chemistry (excerpted from subtitle of the doc of chemgreek).

    Inside the chemgreek, it sets up several mappings, each one corresponds to a separate package. As you may guess, there is a mapping corresponds to fontspec. chemgreek then uses macro \AtBeginDocument (at this time package loadings should have been done) to auto-load a mapping if one and only one of "those" packages is loaded.

  2. Why is the \alpha inside mhchem upright by default?

    By doc of mhchem, Sec. Greek Characters (on page 8), if a greek character is not a variable, it should be typeset in upright font, by typographical conventions. If an italic greek letter is required, you should re-enter the math mode.

  3. What does the one-line configuration do?

    By doc of mhchem, Sec. Greek Font (on pages 17-18), this configuration cancels the mappings of greek fonts, both in text and math mode.

    By the way, mhchem defines aliases (called "mhchem-text" and "mhchem-math") from the current active mappings. So canceling the mapping by configuring mhchem is more appropriate than configuring chemgreek.

  4. As an conclusion of the above three explanations, there does not exist any "incompatibility" between mhchem and fontspec/xeCJK.

  • Re-entering the math mode did not work. Adding \mhchemoptions{text-greek=default, math-greek=default} did work. Thanks! – user96829 Dec 27 '17 at 7:54
  • @Zozor I have completed the answer, and how do you encounter with the trouble "re-entering the math mode did not work"? – Johannes Z Jan 9 '18 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy