3

I am using the package mhchem in order to typeset chemical formulas and equations. If I directly insert a state of aggregation into an mhchem \ce command, it appears as upright. If I insert it via a macro, it becomes italicized. I don't have any aesthetic objections to either, but why does this behavior occur?

The only thing I could think of that would cause this is that the macro is being automatically put into math mode, but I don't understand why this would occur, since newcommand does not ordinarily automatically set text in math mode. Otherwise, it must be some part of the parsing behavior.

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage[version=4]{mhchem}

% States of aggregation
\newcommand{\liq}{(l)}
\newcommand{\aq}{(aq)}

\begin{document}

\noindent
\ce{H2O(l) <=> H+(aq) + OH^-(aq)}\\
\ce{H2O\liq{} <=> H+\aq{} + OH^-\aq{}}

\end{document}

  • 1
    This picture shows what I get. Version mhchem 2015/04/07 v4.00. No italics. – egreg Apr 10 '15 at 17:47
  • @egreg: I can reproduce the error in miktex. I do get italics. – user11232 Apr 10 '15 at 23:51
  • Can you add \listfiles at the top of the example and show the result you get in the .log file after ** File List **? – egreg Apr 11 '15 at 13:37
  • @egreg, I checked the .log file and it saws mhchem.sty 2015/03/13 v3.21 for typesetting chemical formulae. My mistake; I am using ShareLatex, an online version of LaTeX, and did not consider the possibility that the package had not been updated yet. However, this does not really answer the question as to why mhchem is performing this italicizing, so I am hesitant to post this as an answer. Should I answer the question myself? – Bob Apr 11 '15 at 21:35
8

Honest Answer: The answer is straightforward. Because I, the package author, did not have any sensible use case in mind, when writing the macro handling.

Explanation: Because the mhchem syntax is so different from normal LaTeX, I have to parse it before any inner expansion. So I have to take care of macros early on and decide how to handle them. I had some 'academic' use cases in mind that insert some special symbols. However, it never occurred to me that one would like to insert text with macros. Therefore, the macro expansion somehow ended up in math mode.

Recent Changes: During transition to version 4, I realized this inconsistency (= your bug report) and put all macros into the correct mode before expansion, i.e. text mode for \ce{...} and math mode for $\ce{...}$. So it is more consistent now. But still, I was not aware of anyone actually using macros for text.

Future Changes: Now, that I learned a new use case (using macros to insert \ce syntax—I've also found this question), I will try to create some magic that can distinguish between macros that contain a replacement in \ce syntax and macros that do something different, like inserting a special symbol.

Note: If you are the type of person that would like to type the 6 characters \liq{} to get the 3 characters (l) of output, you'd probably be much more satisfied with the features and philosophy of the chemmacros package.

  • IMHO phase descriptors are a perfect example of why the number of keys to type are not the best reason for using macros: since there are different ways to typeset phase descriptors (subscripts, running text, language differences) using macros can save a lot of work later on, even if it means hitting a few more keys. But of course that's just my opinion... :) – clemens Apr 12 '15 at 15:00
  • 2
    @clemens And this is exactly, why I recommended your chemmacros. Your philosophy is control to the user. You provide a thousand little macros. A user can write C\sld{} and then decide with a couple of options (position, language, spacing) what the output should be. mhchem's philosophy, on the other hand, is ease of use. The user types C(s). That is easier, but not as powerful. Therefore, I think, our packages have a good reason to co-exist. Because each of it has a different 'target group'. – mhchem Apr 12 '15 at 18:39
1

It looks like mhchem itself uses the math environment and that the macros are somehow not parsed in the same way as normal text.

A test of this is to use some kind of math-exclusive command in the macro (like \mathcal{X}), which doesn't produce an error.

A clumsy fix (if one is needed) is to just use the \text{} command inside the macros:

\newcommand{\liq}{\text{(l)}}
\newcommand{\aq}{\text{(aq)}}
  • If I try \ce{H2O\mathcal{X}} I get a \mathcal allowed only in math mode error. – egreg Apr 11 '15 at 14:33
  • By the 'macro' I meant the \liq and \aq macros. Writing \newcommand{\liq}{\mathcal{X}} then \ce{H2O\liq} gives no error for me. But this may be different in the newest versions of the package (if I understand the answer from mhchem above right). – Jpm Apr 12 '15 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.