0

I want to use two packages chemmacros and polyglossia together in my document, and for some reason I don't want to use mhchem and babel etc. instead of them. Here are some simplified examples.

Everything behaves fine when setting the language to english.

\documentclass{article}

\usepackage{polyglossia}
\usepackage{chemmacros}

\setotherlanguage{english} % Produces correct results under language `english'.

\begin{document}

\ch{H2O}

\end{document}

enter image description here

However, when I switch the language to russian, \ch{H2O} seems to produce unexpected results, resembling \operatorname{ch}H2O.

\documentclass{article}

\usepackage{polyglossia}
\usepackage{chemmacros}

\setotherlanguage{russian} % Produces incorrect results under language `russian'.

\begin{document}

\ch{H2O} % An error `Missing $ inserted.' is reported, and the result is incorrect.

$\ch{H2O}$ % No error is reported, but the result is still incorrect.

$\operatorname{ch}H2O$ % As a comparison.

\end{document}

enter image description here

How can I solve this problem and make chemmacros behave correctly when the document language is russian?

I am using XeLaTeX as the compiler and TeX Live version 2023.

6
  • 2
    you could try \setotherlanguage[mathfunctions=false]{russian}, then russian will no longer overwrite \ch. Commented Apr 8 at 14:56
  • If you encounter any issues with babel, feel free to ask for help. Commented Apr 8 at 15:03
  • @UlrikeFischer Thanks, it works well. And I apologize for not reading the documentation for package polyglossia very carefully. Commented Apr 8 at 15:15
  • @JavierBezos There's nothing wrong with babel, but polyglossia seems to provide better support for non-Latin scripts, which I use a lot. Commented Apr 8 at 15:20
  • babel provides support, with several levels of coverage, for about 300 languages, including RTL scripts, CJK, Indic languages... See the full list in the manual. Commented Apr 8 at 16:48

1 Answer 1

1

By default, the russian module for Polyglossia defines some operators according to Russian traditions, in particular \sh and \ch for the hyperbolic functions.

Of course, having \ch defined conflicts with chemmacros.

If you don't need them, you can just exclude them at loading time:

\documentclass{article}

\usepackage{polyglossia}
\usepackage{chemmacros}

\setmainfont{Libertinus Serif}

\setmainlanguage[mathfunctions=false]{russian}

\begin{document}

\ch{H2O}

\end{document}

enter image description here

What happens? chemmacros defines its own version of \ch at begin document, whereas polyglossia defines the meaning of \ch for the hyperbolic cosine operator at the point when it switches language, which happens later. So polyglossia wins.

If you want to keep the Russian-style operators, you need to change \ch from chemmacros (actually chemformula) to something else:

\documentclass{article}

\usepackage{polyglossia}
\usepackage{chemmacros}

\setmainfont{Libertinus Serif}

\setmainlanguage{russian}

\AddToHook{file/chemformula.sty/after}{%
  \NewCommandCopy{\CH}{\ch}%
}

\begin{document}

\CH{H2O}

$\ch x=(e^x+e^{-x})/2$

\end{document}

enter image description here

1
  • Thanks, and I had a go at it with the result I wanted. Commented Apr 8 at 15:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .