Chemistry markup

97 views
Skip to first unread message

David Farmer

unread,
Sep 16, 2020, 8:30:02 AM9/16/20
to pretext...@googlegroups.com

Vijay Vulava and I are converting his LaTeX book on Geochemistry
into PreTeXt.

Most of it is routine, but the LaTeX uses a chemistry package
which supports markup like

\ch{CO2}
\ch{O2 \gas}

Those are inline and are akin to inline math. The first converts
to something like $C O_2$, except it is more complicated because
letters representing chemical elements are not in italics.

And also

\begin{reaction}
CuS\sld{} + O2 \gas{} -> Cu\sld{} + SO2 \gas
\end{reaction}

which is reminiscent of display math. That one says something like
copper monosulfide reacts with oxygen to produce copper and sulfur
dioxide.

Also,

\begin{reactions*}
HCl\aq{} &-> H+ \aq{} + Cl- \aq{}\\
HNO3\aq{} &-> H+ \aq{} + NO3- \aq{}\\
NaOH\aq{} &-> Na+ \aq{} + OH- \aq{}\\
Ca(OH)2\aq{} &-> Ca^2+ \aq{} + 2 OH- \aq{}\\
\end{reactions*}

which is sort-of like multiline display math.

The LaTeX also uses siunitx .

We would like advice, or questions to help figure out appropriate
advice, for how to translate this to PreTeXt. Should those be
new tags, or existing tags with something like a "chem" attribute?

I assume the appropriate chemistry packages are available when making
PDF, or available to MathJax for PDF. My expectation is that the
"between the dollar signs it is just LaTeX" principle applies
to chemistry as well. So it is just an issue of how to wrap the
chemistry content in PreTeXt tags.

Regards,

David

Rob Beezer

unread,
Sep 16, 2020, 8:48:55 AM9/16/20
to pretext...@googlegroups.com, David Farmer
Dear David,

Excellent! I'd like to branch out to more STEM fields that are not math/CS/physics/engineering.

I think your comments are right on, but it'll be next week before I can explore implementation.

If I had it to do over, math would be just:

m

md, with no mrow (current me)

md, with mrow

and numbering options would be given with attributes.

So perhaps chemistry needs two or three new tags?

And "laboratory" that is analogous to "worksheet"?

Rob

far...@aimath.org

unread,
Sep 17, 2020, 6:57:18 AM9/17/20
to PreTeXt support
Neil Soiffer pointed out that MathJax knows the mhchem package, while
the book we are converting uses the chemmacros package.

There are some similarities:  mhchem provides `\ce` which (at least for simple expressions)
seems functionally identical to chemmacros `\ch`.  chemmacros has semantic support for
isotopes and oxidation numbers, but I did not find that in mhchem .  I did not find the
chemistry analogue of display math in mhchem .

It looks like we want to support at least some of the extra features in chemmacros,
knowing that we have to deal with a current limitation of MathJax.  That seems similar
to the situation with siunitx .

Rob Beezer

unread,
Sep 21, 2020, 3:11:55 PM9/21/20
to pretext...@googlegroups.com
Aah, not so straight-forward. :-(

But perhaps we can make some constructions behave with some markup. For
example, it seems an isotope can be constructed (not semantically):

https://docs.moodle.org/39/en/Chemistry_notation_using_mhchem#Isotopes

Cursory poking around makes it appear that LaTeX display math (align
environment, etc) can be used to impersonate the reaction environment:

https://tex.stackexchange.com/questions/305567/aligning-labelled-reactions-in-mhchem

Do you know which chemmacros constructions are present in the Geochemistry text,
but lacking in mhchem? Along with estimates of frequency, necessity, and
apparent complexity of impersonation via mhchem?

Rob
> --
> You received this message because you are subscribed to the Google Groups
> "PreTeXt support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email
> to pretext-suppo...@googlegroups.com
> <mailto:pretext-suppo...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/pretext-support/f1d19a05-bc55-4dc7-93ab-b45f5cac2935n%40googlegroups.com
> <https://groups.google.com/d/msgid/pretext-support/f1d19a05-bc55-4dc7-93ab-b45f5cac2935n%40googlegroups.com?utm_medium=email&utm_source=footer>.

far...@aimath.org

unread,
Oct 30, 2020, 10:06:26 AM10/30/20
to PreTeXt support
I have looked through the Geochemistry book. See below for representative markup.
I include examples of SI units, because the book uses them, and also I see that as
basically the same issue.

I also looked at the mhchem package, to see if it could handle everything in that
particular book.  The answer is no:  mhchem does not have built-in support for
isotopes and it does not seem to handle bonds in the same way.  The syntax for
multiline chemistry displays is superficially different.

Does the following seem feasible (assuming someone is willing to do it), and are
there any competing options? 
Introduce PreTeXt markup for inline and display chemistry, analogous to "m" and
"md" for math.  In the preprocessing step, extract all the chemistry and convert it
to TeX math markup.  Then process as usual.

Regards,

David


Below is a pretty good representation of the chemistry markup in one book.

Inline:

\ch{H+}
\pH\
\pOH\
\SI{25}{\celsius}
\num{1.0e-7}
\SI{3.47e-3}{\moLar}
\ch{H2CO3^* \aq}
\SI{1.0}{M}
\SI{e-5}{\mole}
\SI{0.0821}{\atmosphere\liter\per\mole\kelvin}
\enthalpy(rxn){-41}
\isotope*{14,C}
\isotope{3,H}
\ch{C_n H_{2n+2}}
\si{\micro\gram\per\liter}
\SIrange{e-4}{e-8}{\atmosphere}

Display:

\begin{reaction*}
        HX -> H+ + X-
\end{reaction*}

\begin{reaction*}
        CaSO4.2 H2O \sld ->[\water] Ca^2+ \aq{} + SO4^2- \aq{} + 2 H2O\lqd
\end{reaction*}

\begin{reaction*}
      \isotope{137,Cs} -> \isotope{137,Ba}\, (excited state) -> \isotope{137,Ba} + $\gamma$
\end{reaction*}

\begin{reactions*}
    CO2 \gas{} <=>[\water\lqd] CO2 \aq\\
    CO2 \aq{} + H2O \lqd <=> H2CO3 \aq \\
    H2CO3\aq{} <=> H+ \aq{} + HCO3- \aq \\
    HCO3- \aq <=> H+ \aq{} + CO3^2- \aq
\end{reactions*}

\begin{reactions*}
    2 SO2 \gas{} + O2 \gas{} &-> 2 SO3 \gas{}&\\
    SO3 \gas{} + H2O \lqd{} &-> H2SO4 \aq{}&\\
    H2SO4 \aq{} &-> H+ \aq{} + HSO4^- \aq{}&
\end{reactions*}

\begin{reactions*}
    2 Na + Cl2 &-> 2 Na+ + 2 Cl-\\
    2 "\ox*{0,Na}" + "\ox*{0,Cl2}" &-> 2 "\ox*{+1,Na+}" + 2 "\ox*{-1,Cl-}"
\end{reactions*}

\begin{reactions*}
    O + 2 \el &-> O^2- \\
    N + 3 \el &-> N^3-
\end{reactions*}

\begin{reactions*}
    "\chlewis{0.}{H}" +  "\chlewis{180.}{H}"  &-> H\bond{sb}H ->  H2\\
    "\chlewis{0.90,180.270,}{O}" + "\chlewis{0.90,180.270,}{O}" &-> "\chlewis{90,270,}{O}" \bond{db} "\chlewis{90,270,}{O}" ->  O2\\
    "\chlewis{0.90.180,270.}{N}"  +  "\chlewis{0,90.180.270.}{N}" &-> "\chlewis{180,}{N}" \bond{tp} "\chlewis{0,}{N}" ->  N2
\end{reactions*}


Rob Beezer

unread,
Oct 30, 2020, 12:25:46 PM10/30/20
to pretext...@googlegroups.com
Thanks, David.

Simple questions about markup:

A. The quotation marks are for what?

B. Is (rxn) an argument to \enthalpy? And either way, then what is (excited state)?

Yes, I think analogs of "m" and "md" are the place to start. (As you know, our
"me" could be unnecessary.)

Now, what goes in there, and what we do with it, are the open questions.

Does a LaTeX package (which one?) make the most sense for all chemists?

Do we dump it into the LaTeX output unmolested?

If we process it, then when and to what? In SL2X or PTX assembly? What do we
consider PTX source, and what do we consider needing translation? Make LaTeX
out of it, or ...?

There is XML markup for chemistry:

https://en.wikipedia.org/wiki/Chemical_Markup_Language

Of course, I doubt we want authors to write this by hand, but I once looked into
editors (building molecules) and viewers (think 3D rotatable images) that used
this format internally. So if we are going to parse your examples at the
assembly stage, maybe a subset of this XML will give us the most leverage.
(Which begs the question of why we don't translate LaTeX into MathML?)

So...if "m" and "md" analogs make sense:

1. What syntax goes inside (which we would then implicitly commit to long-term
for authors)?

2. Advantages and disadvantages of wrangling that syntax into usable or
necessary forms?

Rob
> --
> You received this message because you are subscribed to the Google Groups
> "PreTeXt support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email
> to pretext-suppo...@googlegroups.com
> <mailto:pretext-suppo...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/pretext-support/4b1f7041-3688-4cd8-861b-d7d9f7ef997bn%40googlegroups.com
> <https://groups.google.com/d/msgid/pretext-support/4b1f7041-3688-4cd8-861b-d7d9f7ef997bn%40googlegroups.com?utm_medium=email&utm_source=footer>.

David Farmer

unread,
Oct 30, 2020, 1:34:20 PM10/30/20
to pretext...@googlegroups.com

My long-term plan is that the math in the PreTeXt source is not
LaTeX, but something similar (but better!) which gets turned into
LaTeX for PDF and maybe LaTeX or something else for other output
formats.

Authors will be able to choose to write LaTeX math if they want
to do extra work and have lower quality output, of course.

So, it is not a big step for me to assume that the markup for
chemical formulas may require some processing.

Some aspects of the existing chemistry LaTeX markup strike me
as excellently designed. For example:

2 Na + Cl2 -> 2 Na+ + 2 Cl-

Note what is missing: curly brackets and specific characters
for subscripts and superscripts. Unambiguous, human readable,
no wasted characters, and clever use of spaces for disambiguation.

That is what math source markup is currently missing.

Regards,

David
> email to pretext-suppo...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/pretext-support/e77e0964-31f3-8422-1fe5-b32d9f5ce615%40ups.edu.
>

Rob Beezer

unread,
Oct 30, 2020, 1:45:54 PM10/30/20
to pretext...@googlegroups.com
Got it. No surprises here. ;-)

Do you think we can (reliably) make a better syntax for chemistry, out of the gate?

Our technical hurdle is that XSLT 1.0 has really limited tools for parsing "raw
text." We can move more and more to our two Python front-ends and mix XSLT 1.0
via the lxml library with Python's tools for things like regex.

That removes "xsltproc" as a tool for authors (I don't see any tears being
shed), though as a developer I'd miss it for efficiency reasons (which should
not be a consideration).

Will you have time to discuss more at drop-in development?

Rob

Rick Roesler

unread,
Oct 31, 2020, 12:24:43 PM10/31/20
to PreTeXt support
\enthalpy(rxn){-41} 

the 'rxn' argument is a subscript (in this example, the enthalpy of the reaction)

Rob Beezer

unread,
Oct 31, 2020, 1:10:58 PM10/31/20
to pretext...@googlegroups.com

Rob Beezer

unread,
Nov 12, 2020, 3:37:23 PM11/12/20
to pretext...@googlegroups.com
Couple hours of experimentation.

Source:

<p>Water: <ch>H2O</ch></p>
<p>Alkanes: <ch>C_n H_{2n+2}</ch></p>
<p>Isotope: <ch>^{227}_{90}Th+</ch></p>
<p>Inline Reaction: <ch>HX -> H+ + X-</ch></p>
<p>Displayed reaction:<chd>HX -> H+ + X-</chd>followed by discussion.</p>

LaTeX: mhchem package explicitly set to version 4. Screenshot attached.

MathJax: v2.7, mhchem extension, set to version 3 (newest possible). Screenshot
attached.

Observations:

1. The necessity of upright letters, and especially upright Greek letters, is
the first hesitation I have for converting chemistry syntax to LateX.

2. It seems there are plenty of opportunities to clean-up allowed mhchem
syntax. Such as converting double-headed arrows to two stacked one-way arrows.
And ambiguous hyphens can be bonds or used for charges on ions.

3. Aligned reactions seems straightforward, but I did not go that far. This is
the bare minimum, no numbering, etc. But I think punctuation will be absorbed,
since I had to adjust some related routines.

4. I'm not suggesting I think an inline reaction is a good idea, but I don't
know the customs for writing about chemistry.

5. Sounds like MathJax 3 has a rewrite of the mhchem extension/package (version
4?).

Question:

Your earlier examples look like they use the "chemmacros" package. How
feasible would it be to convert that to "mhchem" (given the latter is supported
within MathJax)?

Useful comparison:
https://www.tug.org/TUGboat/tb36-3/tb114niederberger.pdf

Rob
chem-html.png
chem-pdf.png
Reply all
Reply to author
Forward
0 new messages