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 https://tex.stackexchange.com/a/163149 – Troy Dec 21 '17 at 05: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. –  Dec 21 '17 at 05: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’ –  Dec 21 '17 at 05:49
  • While Troy's comment would be the cause of your problem, xeCJK is an overkill if your document primarily is not in CJK. \newfontfamily from fontspec would be sufficient. – yudai-nkt Dec 21 '17 at 05:49
  • @yudai-nkt I use xeCJK just to avoid the hassle of changing font manually anytime I use a CJK character –  Dec 21 '17 at 05:51
  • 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 06:03
  • I didn’t know that, I’ll definitely check for harmful side effects! –  Dec 21 '17 at 06:07
  • If it's fontspec that's the problem (rather than xeCJK) perhaps it's similar to Lowercase gamma in mhchem environment with fontspec. – Nicola Talbot Dec 21 '17 at 11:09
  • Reproduced on my up-to-date TeX Live; it seems that when fontspec changes the math font, the \alpha disappears inside \ce. – Hironobu YAMASHITA Dec 21 '17 at 12:57
  • 1
    I recommend you change your MWE to replace xeCJK with fontspec. – Nicola Talbot Dec 21 '17 at 12:59
  • @NicolaTalbot Done (with improved explanation as well) –  Dec 25 '17 at 07:13
  • should this be written as $\alpha$, rather than the wrapping the entire formula in $ ... $? – Ross Dec 25 '17 at 08:57
  • The default font used under XeLaTeX + fontspec does not contain “α” glyph, and the argument of \ce command does not change font from the default (unlike the “raw” math equations); that is the reason why \alpha disappears. If you add \setmainfont{SourceHanSans-Regular} (obtain the free font from Adobe), the α will appear. – Hironobu YAMASHITA Dec 25 '17 at 09:59

1 Answers1

4

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.

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