Nested Chemformula exceeds TeX Capacity

Here is an MWE of the problem at hand:

\documentclass{article}
\usepackage{chemformula}
\begin{document}

$\varphi_{\ch{N2}}$%chemformula as index works

\ch{C + O2 + N2 -> CO2 + N2}%regular equation

\ch{C + O2 + $\varphi$ N2 -> CO2 + N2}%escaped math works

%\ch{C + O2 + $\varphi_{\ch{N2}}$ N2 -> CO2 + N2}%throws error when nested

\end{document}


In the code with its comments, the problem is explained: using chemformula and its command \ch, indices may be created, even in math mode, see the first line of code.

Formulating a longer equation within \ch but then also trying to escape math symbols who themselves have indices typeset using \ch results in nested \ch commands. These throw and error

TeX capacity exceeded, sorry [main memory size=3000000]. ...+ O2 + $\varphi_{\ch{N2}}$ N2 -> CO2 + N2}


when compiled.

How do I solve this?

• Roughly: \ch calls \chemformula_ch:nn, which parses the formula and stores it into \g__chemformula_output_tl before using that global variable. Parsing the big formula puts \__chemformula_ch_main:n {C + O2 + $\varphi _{\ch {N2}}$ N2 -> CO2 + N2} in the global variable. Among other things this runs \ch{N2}, which adds \__chemformula_ch_main:n {N2} to the global variable. When the variable is run it contains two \__chemformula_ch_main:n. The first includes \ch{N2}, which adds a third \__chemformula_ch_main:n and runs the variable... Sorry, I won't have time to dig more. – Bruno Le Floch Oct 6 '18 at 12:53
• I hope I'm showing my full ignorance here but ... Perhaps this means, one of the calls needs to be expanded immediately rather than stored? – Jeffrey J Weimer Oct 7 '18 at 13:52

The solution is quite easy actually: use \chcpd inside:

\documentclass{article}
\usepackage{chemformula}
\begin{document}

$\varphi_{\ch{N2}}$%chemformula as index works

\ch{C + O2 + N2 -> CO2 + N2}%regular equation

\ch{C + O2 + $\varphi$ N2 -> CO2 + N2}%escaped math works

\ch{C + O2 + $\varphi_{\chcpd{N2}}$ N2 -> CO2 + N2}

\end{document}


• Thanks, but notice how this is not what I am looking for. The index for \varphi is now set in plain math-mode, removing the capabilities of chemformula I was originally after. – Hans Lollo Oct 9 '18 at 7:56
• @HansLollo indeed. It should work, though, so this looks like a small bug… There is another clean solution, see my updated answer. – clemens Oct 9 '18 at 10:36

A bit of playing with \ensuremath and \mathrm seems to do the job.

\documentclass{article}
\usepackage{chemformula}

\newcommand{\varphich}[1]{\ensuremath{\varphi_{\mathrm{#1}}}}

\begin{document}

without ch - shows as text: \varphich{N2}

ch in it - shows as chemistry: $$\varphi_{\ch{N2}}$$%chemformula as index works

equation 1: \ch{C + O2 + N2 -> CO2 + N2}%regular equation

equation 2: \ch{C + O2 + $$\varphi$$ N2 -> CO2 + N2}%escaped math works

equation 3: \ch{C + O2 + \varphich{\ch{N2}} -> CO2 + N2}%throws error when nested

\end{document}


I also used the $$ $$ designations for in-line math based on this article.

• This does in fact not work for me. XeLaTex throws the error TeX capacity exceeded, sorry [semantic nest size=500] (which is likely what @BrunoLeFloch referred to), while pdfLatex returns TeX capacity exceeded, sorry [grouping levels=255], similar but still different to the one above. I am not knowledgeable enough to understand why these are different. I doubt that extending the TeX capacity works here. – Hans Lollo Oct 7 '18 at 8:46
• Gosh. Then I am lost. The above compiles on my TeXLive install under macOS. – Jeffrey J Weimer Oct 7 '18 at 13:47
• I get ! TeX capacity exceeded, sorry [semantic nest size=500]. on macOS. I tried with TeX Live from 2012 to 2018; up to 2017 the failure is with grouping levels. – egreg Oct 9 '18 at 11:36