# Mhchem errors in LyX: kernel/command not defined and undefined control sequence

I'm using MiKTeX (Console) 2.9.6673 on a Windows 10 Pro. Consider the following minimal-not-working-example.

\documentclass{article}

\usepackage{mhchem}

\begin{document}
\ce{H2O}
\end{document}


The document body contains nothing else but \ce{}, and it doesn't compile, throwing tons of errors. I don't understand the problem. I have every required package (mhchem, chemgreek, l3kernel, l3packages). Some errors:

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! LaTeX error: "kernel/command-not-defined"
!
! Control sequence \regex_match:NnTF undefined.
!
! See the LaTeX3 documentation for further information.
!
! For immediate help type H <return>.
!...............................................

l.53 ...nerate_variant:Nn \regex_match:NnTF { NV }

|'''''''''''''''''''''''''''''''''''''''''''''''
| This is a coding error.
|
| LaTeX has been asked to use a control sequence '\regex_match:NnTF':
| this has not been defined yet.
|...............................................


Here's the complete log (first error on line 375). They all seem to contain something regex-related. What can I do to solve this?

• This is a more-than-minimal example. Start throwing out all unnecessary packages and package options while the errors are still showing, or stop showing: Magyar, geometry, babel, ams packages, version=3 in mhchem, hyperref, etc. You can't call the same package with different options twice. Any difference between \ce{H2O} and $\ce{H2O}$? – PhilipPirrip Mar 28 '18 at 7:01
• Threw out everything. Everything stayed the same. – tatsletni Mar 28 '18 at 15:22
• Instead of l3regex, you could try updating the l3-packages via the MikTeX console, for both user and admin. You have an expl3 from 2016, a lot of things has happened since then. – Torbjørn T. Apr 4 '18 at 21:56

It seems placing \usepackage{l3regex} before \usepackage{mhchem} solves the problem. I'm not sure why l3regex is not included by default by mhchem if it makes use of the former, I didn't have a problem with this before reinstalling MiKTeX.

• I quote from my LaTeX output: "Package l3regex is obsolete and has been removed! Its functionality is now only provided as part of the expl3 package. The old packages will be removed entirely at the end of 2018. Therefore, please replace '\usepackage{l3regex}' with '\usepackage{expl3}' in your documents as soon as possible." – mhchem Mar 30 '18 at 18:25

You do not have the current expl3 package installed and are missing the l3regex functionality.

You could try using \usepackage{l3regex}, but that is not a good solution. In my output, this writes

** Package l3regex is obsolete and has been removed!
**
** Its functionality is now only provided as part of the expl3 package.
**
** The old packages will be removed entirely at the end of 2018.
**
** Therefore, please replace '\usepackage{l3regex}'
** with '\usepackage{expl3}' in your documents as soon as possible.

• I have expl3 installed as part of l3kernel, and mhchem.sty also contains the line \RequirePackage{expl3}, but it doesn't work like that. If I add \RequirePackage{l3regex} to mhchem.sty, everything works perfect. I understand it's obsolete, but why doesn't it work with just expl3 if it contains l3regex's functionality? – tatsletni Mar 31 '18 at 15:43

Adding \RequirePackage{l3regex} to mhchem.sty solved the problem. This is useful if you can't put \usepackage{l3regex} before \usepackage{mhchem}. For example in LyX, if LyX automatically loads the package when you insert a chemical formula.