The OP's formula (Ca,Mg)CO3 is completely fine and meets IUPAC recommendations [1, p. 21], section IR-2.2.3.1:
(d) In solid-state chemistry, to enclose symbols of atoms occupying the same type of site in a random fashion. The symbols themselves are separated by a comma, with no space.
Example:
- K(Br,Cl)
To achieve this with mhchem
, the sane way would be to use braces around the comma (as mentioned in the comments): {,}
. Note that chemformula
doesn't require the comma to be escaped and renders the formula correctly "as is":

\documentclass{article}
\usepackage[version=3]{mhchem}
\usepackage{chemmacros}
\chemsetup{
modules = all
}
\begin{document}
\ce{(Ca{,}Mg)CO3} (\texttt{mhchem})
\ch{(Ca,Mg)CO3} (\texttt{chemformula})
\end{document}
References
- IUPAC “Red Book” Nomenclature of Inorganic Chemistry, 1st ed.; Connelly, N. G., Damhus, T., Hartshorn, R. M., Hutton, A. T., Eds.; IUPAC Recommendations; Royal Society of Chemistry: Cambridge, UK, 2005. ISBN 978-0-85404-438-2. PDF