I’m trying to write a simple chemical equation with oxidation numbers. In latex I managed to get it to work by using the overset
function. Although this was not the ideal solution it did get the job done without having to install a package.
Here is what I typed in latex:
$\overset{\rm 0}{Zn} + 2\overset{\rm I}{Na} \overset{\rm -II}{O} \overset{\rm I}{H} \longrightarrow \overset{\rm I}{Na_2} \overset{\rm II}{Zn} \overset{\rm -II}{O_2} + \overset{\rm 0}{H_2}$
And here is what I did so far in typst:
$ \Zn + 2\Na \O \H --> \Na_2 \Zn \O_2 + \H_2$
What I hoped to achieve was something like this:

Does anyone know how to recreate this? I also thought about creating a feature request on GitHub but wanted to consult the community first.
You can use math.attach()
to write the oxidation numbers on top of the elements. Two comments to explain the code below:
- Wrapping the base in
math.limits()
is required to force the attachment to actually be on top. Otherwise the attachments would be formatted like an exponent.
- You can use strings in math mode to get regular (upright) text. As far as I know the chemical elements should be formatted upright?
#let ox(base, attachement) = {
math.attach(math.limits(base), t: math.upright(attachement))
}
$ ox("Zn", 0) + 2 ox("Na", I) ox("O", -"II") ox("H", I) -> ox("Na"_2, I) ox("Zn", "II") ox("O"_2, -"II") + ox("H"_2, 0) $
There are also two chemistry packages (that I know of) available in the Typst universe, namely whalogen and typsium. The whalogen manual even mentions oxidation states but they are formatted as exponents there. I don’t know if there is an easy way to modify this.
In any case I would recommend you to check out those packages if you are writing chemical formulas. The features you need might already be implemented there.
2 Likes
As a comment, there is a call for a Typst Chemistry Community Typsium - Typst for chemistry.
1 Like
Hey Tobias!
I’m one of the core maintainers of both the Typsium Organisation and wrote the Oxidation number support for Whalogen.
As @janekfleper mentioned the attach method would work just fine, even without needing any packages, but if you’re doing lots of chemistry stuff I’d definitely recommend usingWhalogen for now as it’s most stable. I just recently added oxidation number support to it and you can get the newest package from the github (it may take a bit until the newest version is on typst universe, so just get the newest version here: Whalogen Github).
The main method is #ce (chemical equation)
If you wrap your element in vertical bars | and a comma. Anything that’s after the comma but still inside the bars will be placed ontop.
#ce("H2O") <-normal H2O with proper formatting
#ce("|H2,+I| |O, -II|")<- H2 with oxidation state of +I on top of it and O with -II
We understand that this is not perfect syntax and are currently working on a bigger more wholistic approach to writing chemistry in typst. As @quachpas mentioned we have a small but great chemistry package developer community working on multiple packages that will work together and are more aware of the meaning of the written symbols. We’ve actually been talking about adding an oxidation number calculator into the rendering package similar to how these online calculators do it: Oxidation Number Calculator
But right now there’s bigger fish to fry and we’re not sure if it’s worth the effort right now.
The typsium package is currently still work in progress and not as stable as whalogen, but long term we’re planning to make Typsium the main formula rendering package. The Whalogen core maintainer just recently joined us, so things are going really well! If anyone is interested, we are actively working on things and could use any help, wether experienced in chemistry or not!