I am the inventor and author of the LaTeX/mhchem package. You know, the \ce macro. Last year, I took over MathJax/mhchem from the core MathJax team and reimplemented it to match the original in more details and to add features that were added to LaTeX/mhchem in the meantime.

I am a huge fan of the design principle of desire paths, see how people use \ce and if a pattern occurs, support it. One example: to get $\mathrm{NO}_x$ one originally had to enter NO_$x$. But I observed that many people just typed NO_x, so I changed mhchem.

Chemistry.SE was a great source of test cases. I downloaded the posts and could see how real users use the \ce command – and also how they misused it. Many fine improvements came from this opportunity (and one or two compromizes as well).

This way, I learned that people type in orbital configurations and things like pH, K_a etc. I thought it a good idea to support these as well.

I think, a LaTeX document is different in many aspects from a SE post. In the former, people are used to have a lot of specialized commands (e.g. for bullet lists or quotes), while in the latter most commands are 'implicit' (typing * or - for a bullet point, or > for a quote). Therfore I am quite happy if people here know 'use \ce for chemical things'. And if the input is unambiguous, I am happy to support it. I wouldn't expect them to learn 4 or 5 chemical commands. This difference between LaTeX and posts, by the way, is one reason that MathJax/mhchem has more features than LaTeX/mhchem (and the other reason is that JavaScript is more suited that LaTeX to do complex string operations).

Because I am not a chemist, I rely on what I can find in Standard documents. But things get more and more tricky, so I asked here at Chemistry.SE, several times. (Thanks!) During the discussions about orbitals and Ka, I got some comments saying "Don't do it. K_a and orbitals should not be part of \ce."

Because I think comments and upvotes are not the best tool to discuss this topic, I create this discussion. Do you think, it is a good thing that $\ce{K_a = \frac{[H3O+][A-]}{[HA]}}$ is rendered as $$\ce{K_a = \frac{[H3O+][A-]}{[HA]}}$$ Or would you say K_a and \frac are no chemical entities and should not be part of the \ce syntax (and, hence, should be K_\mathrm{a} = \frac{\ce{[H3O+][A-]}}{\ce{[HA]}} instead)? What about orbials, pH and the like?

Addendum: At the beginning, I thought \frac should not be part of \ce, but it was already supported when I took over MathJax/mhchem. I started with fixing some posts here, but realized how tedious it is and that it would be easier to implement this quickly than fixing 200+ further posts (out of 43k). I think the existence of this feature doesn't hurt too much.

  • $\begingroup$ In using $K_\rm{a}$ is K supposed to be slanted or upright? $\endgroup$
    – MaxW
    Commented Mar 2, 2017 at 17:48
  • 1
    $\begingroup$ @MaxW It's a variable/parameter, so it's supposed to be italicized. $\endgroup$
    – hBy2Py
    Commented Mar 3, 2017 at 5:26
  • $\begingroup$ (It was enabled in 2012.) $\endgroup$ Commented Sep 7, 2022 at 12:47

4 Answers 4


I'm using $\LaTeX$ frequently and I'm not planning to stop that ;-)

While I do appreciate the good intention to make the life of the users easier, which you already have done, thanks a bunch for your great work, I think it is not a good idea to allow
$\ce{K_a = \frac{[H3O+][A-]}{[HA]}}$, .

Call me a spoilsport and a purist, but I'd rather prefer the math part and the chemistry part to be separated, just as you outlined in the second option.

From a more practical view, would the simplified syntax play well when trying to align \ce and non-\ce equations in split or align environments using & as a marker?


Yes, I was that user. So it is only fitting that I answer here.

In general, all types of software can go one of two different ways:

  1. set up a set of rules and require the user to conform to them; throw up errors or give weird behaviour if the user does not

  2. set up rules but have them be weak. If something is ambiguous, try to guess what the user intended and do it that way rather than throwing errors of giving weird behaviour.

Most people on the Linux and $\require{begingroup}\mathrm{\LaTeX}$ side of things heavily support the first while a ubiquitous operating system from Redmond went the second route and many software developers (e.g. CambridgeSoft’s ChemDraw) followed it. It will probably not come as a surprise that I support the first path.

mhchem is originally a package for $\mathrm{\LaTeX}$; $\mathrm{\LaTeX}$ is a markup language that is supposed to take care of formatting and typography, giving the user full control of (and responsibility for) the content. The user should, however, still be in control of what type the content is they add — i.e. they should have full control of saying: ‘I want this to be formatted as a chemical. This should be a mathematical equation and this is a descriptive text added.’ The underlying $\mathrm{\LaTeX}$ should only be taking care of accepting what the user passes it and formatting it in the desired way. In a different way of phrasing it, the machine side of things should perform minimal checks to determine patterns but then format patterns consistently.

What you are essentially proposing is the second option. You let the user type in anything in a very generic command and then attempt to interpret it in some way, guessing what the user wanted. You will probably manage to guess $90~\%$ of all cases correctly, maybe even $95~\%$. And then one user will have to work against your guesses because they have a clear idea of what they want but no easy way of telling mhchem to ignore its guessing mechanisms.


\ce{A + B ->[K_2] AB}

\ce{aC + bN + \dots + pH -> C5H5N}

In the first case, is mhchem supposed to typeset $\ce{K2}$ or $K_2$ above the reaction arrow; i.e. are we talking about an equilibrium constant or does dipotassium mediate the reaction? In the second case, is pH supposed to be rendered as $p\ce{H}$ — analogous to $\ce{aC}$ — or as $\mathrm{pH}$, i.e. is it $p$ hydrogen atoms or a $\mathrm{pH}$ value? How do I choose the other?

We get a lot of posts of many users that write maths wrongly. The two most common errors are:

  • not using \ce, i.e. just typing $H_2O$ instead of $\ce{H2O}$

  • using \ce for everything i.e. typing $\ce{V=5ml}$ instead of $V=\pu{5ml}$

We could now start guessing whether $H_2O$ is supposed to be a chemical or not. But we shouldn’t. We can’t. We’ll fail at $V$ (vanadium or volume?). We should correct the users and point them to guides where they can learn. Believe it or not, some initially don’t know that there are differences but could benefitby doing so!

As a direct consequence, we shouldn’t overconvolute a single command with a lot of different ‘features’. We should define realms and stick to them. As uncommon as it may be, some exercise may talk about the fictional element kalevalium or $\begingroup\require{cancel}\cancel{\ce{Ka}}~\mathrm{Ka}\endgroup$. (I realise to my unhappyness that the bug has already been implemented! D=) And now what? If kalevalium reacts with oxygen …

Balance the following equation: $$\begingroup\require{cancel}\cancel{\ce{? Ka + ? O2 -> Ka2O5}}\\ \ce{? {Ka} + ? O2 -> {Ka}_2O5}\endgroup$$

For reference, the code:

$$\begingroup\require{cancel}\cancel{\ce{? Ka + ? O2 -> Ka2O5}}\\
\ce{? {Ka} + ? O2 -> {Ka}_2O5}\endgroup$$

To be honest, I am a bit mixed on the topic of orbitals/electronic configuration. One the one hand, I would appreciate them getting their own \elecon{...} command. On the other hand, they might just be distinct enough to allow support by \ce.

Note, by the way, that you already had the chance to overconvolute \ce but thankfully decided against it and introduced \pu. Support for these expressions would be cool, but not as part of \ce; instead consider \chemvar or something.

Final note: call me pedantic, but the example should be coded as follows:

$$K_\mathrm{a} = \frac{[\ce{H3O+}][\ce{A-}]}{[\ce{A-}]}$$

$$K_\mathrm{a} = \frac{[\ce{H3O+}][\ce{A-}]}{[\ce{A-}]}$$

Since $[\ce{X}]$ is just a shorthand for $c(\ce{X}), a(\ce{X})$ or $x(\ce{X})$.

To stick with the picture of desire paths: they are great. But sometimes, the desire path requires you to pave across a waterway (no, building a bridge is cheating!) or over dangerous areas. Know when to stop listening to desire paths.

  • $\begingroup$ THIS is what I was groping towards -- the difficulty of formatting ambiguous cases correctly. Well said. $\endgroup$
    – hBy2Py
    Commented Mar 3, 2017 at 21:55
  • $\begingroup$ Well said! I was about to take apart that ka Expression. $\endgroup$ Commented Mar 4, 2017 at 8:28

I've used a lot of markup languages through my career, but never LaTeX. So I am not a LaTeX purist by any means.

I'd sure like using \frac and \dfrac within \ce and without having to use multiple \ce expressions. The markup would be shorter and easier to read.


I am on the same side as Klaus: please do not revise mhchem to allow things like $\ce{K_a = \frac{[H3O+][A-]}{[HA]}}$. I agree with his sentiment that the math and the chemistry should be semantically separate.

As far as fractions are concerned, I use the \over construction regularly within \ce, which makes for what seems to me a perfectly simple and readable syntax:

$$K_\mathrm a = {\ce{[H3O+][A-]\over [HA]}}$$

$$ K_\mathrm a = {\ce{[H3O+][A-]\over [HA]}} $$

If anything, all you'd need to do is change \ce so that its curly braces act as boundaries for the \over command, since currently they don't:

$$K_\mathrm a = \ce{[H3O+][A-]\over [HA]}$$

$$ K_\mathrm a = \ce{[H3O+][A-]\over [HA]} $$

Now that I know that I can define new commands isolated to just my post (thanks, Mart!), if I'm going to be using \mathrm a lot I'll just \def\r#1\mathrm{#1} &c. with \begingroup / \endgroup:

\def\pK#1{\r p\K{#1}}
\K{a,A} &= {\ce{[H3O+][A-]\over [HA]}} \\
\K{a,B} &= {\ce{[H3O+][B-]\over [HB]}} \\
\pK{a,A} &= 12.2 \\
\pK{a,B} &= 6.5

$$ \require{begingroup} \begingroup \def\r#1{\mathrm{#1}} \def\K#1{K_\r{#1}} \def\pK#1{\r p\K{#1}} \def\rar{\rightarrow} \begin{align} \K{a,A} &= {\ce{[H3O+][A-]\over [HA]}} \\ \K{a,B} &= {\ce{[H3O+][B-]\over [HB]}} \\ \pK{a,A} &= 12.2 \\ \pK{a,B} &= 6.5 \end{align} $$

Actually, having now typed out the above, more than anything I think adding a judicious set of new commands to mhchem might be valuable, something sort of like:

  • \K{a,A} $\rar$ K_\mathrm{a,A} $\rar K_\r{a,A}$

  • \pK{a} $\rar$ \mathrm p K_\mathrm a $\rar \pK{a}$

  • \p{H} $\rar$ \mathrm p \ce{H} $\rar \mathrm p \ce{H}$

  • \p{Cu+} $\rar$ \mathrm p \ce{Cu+} $\rar \mathrm p \ce{Cu+}$

  • \Ea $\rar$ E_\mathrm a $\rar E_\mathrm a$


These may or may not be good candidates -- that would be a topic for another discussion post. Any finalists would obviously have to be chosen so as not to collide with any other common commands.


  • $\begingroup$ I would not need to revise MathJax/mhchem. It always allowed \frac input. // I don't understand why \frac is bad, but \over is okay in your eyes. // 'curly braces act as boundaries' is already implemented (because of other reasons) in the dev branch. $\endgroup$
    – mhchem
    Commented Mar 3, 2017 at 18:49
  • $\begingroup$ @mhchem Yeah, I sort of thought about that while I was writing the post, but not clearly ... you're quite right, both forms mix math and chemistry formatting. $\endgroup$
    – hBy2Py
    Commented Mar 3, 2017 at 21:54
  • $\begingroup$ Short commands should be used sparingly and only for very common cases. A lot can break in $\mathrm{\LaTeX}$, for example, if \c is redefined. Therefore, short commants need utmost scrutiny. A slightly longer command would work better imho. $\endgroup$
    – Jan
    Commented Mar 3, 2017 at 21:58
  • 1
    $\begingroup$ @Jan <nod>, I didn't mean to imply that I thought all of the definitions in my examples were ones that should be adopted. I did think about short commands wreaking havoc. $\endgroup$
    – hBy2Py
    Commented Mar 3, 2017 at 23:13

You must log in to answer this question.

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