Skip to content

Commit

Permalink
typsium:0.1.0 (#1795)
Browse files Browse the repository at this point in the history
  • Loading branch information
tryptophawa authored Feb 24, 2025
1 parent b30f6e4 commit bb660a8
Show file tree
Hide file tree
Showing 5 changed files with 410 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/preview/typsium/0.1.0/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2025 β-吲哚基丙氨酸

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
35 changes: 35 additions & 0 deletions packages/preview/typsium/0.1.0/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Typst Chemical Formula Package

A Typst package (alpha) for typesetting chemical formulas, currently working on inorganic.

## Features

- Typeset chemical formulas with ease
- Reactions and equations, including reversible reactions
- Support for complex reaction conditions (e.g. temperature (T=), pressure (P=), etc.)

## Usage

To use Typsium, you need to include the package in your document:

```typst
#import "@preview/typsium:0.1.0": ce
#ce("[Cu(H2O)4]^(2+) + 4NH3 -> [Cu(NH3)4]^(2+) + 4H2O")
```

![result](resource/lib.svg)

## TODO

- [x] Subscript and superscript notation in boundary cases
- [x] Enhanced support for complex reaction conditions
- [x] Optimize reversible reaction arrow styles
- [x] Add support to brackets and parentheses (testing)
- [x] Add support to oxidation states
- [ ] Improve organic chemistry structure support
- [ ] Add documentation and examples
- [ ] Open an repo for this packages

## License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
119 changes: 119 additions & 0 deletions packages/preview/typsium/0.1.0/lib.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// === Declarations & Configurations ===
#let regex_patterns = (
element: regex("^\s?([A-Z][a-z]?|[a-z])(\d*)"),
bracket: regex("^\s?([\(\[\]\)])(\d*)"),
charge: regex("^\^?\(?([0-9|+-]+)\)?"),
arrow: regex("^\s?(<->|->)"),
coef: regex("^\s?(\d+)"),
plus: regex("^\s?\+"),
)
#let config = (
arrow: (arrow_size: 120%, reversible_size: 150%),
conditions: (
bottom: (
symbols: ("Delta", "delta", "Δ", "δ", "heat", "hot"),
identifiers: (("T=", "t="), ("P=", "p=")),
units: ("°C", "K", "atm", "bar"),
),
),
)
// === Basic Processing Functions ===
#let process_element(element, count) = { $element_count$ }
#let process_bracket(bracket, count) = { $bracket_count$ }
#let process_charge(input, charge) = context {
show "+": math.plus
show "-": math.minus
$#block(height: measure(input.children.last()).height)^charge$
}

// === Formula Parser ===
#let parse_formula(formula) = {
let remaining = formula.trim()
let result = none
while remaining.len() > 0 {
let matched = false
for pattern in ("coef", "element", "bracket") {
let match = remaining.match(regex_patterns.at(pattern))
if match != none {
result += if pattern == "coef" { $#match.text$ } else if pattern == "element" {
process_element(match.captures.at(0), match.captures.at(1))
} else { process_bracket(match.captures.at(0), match.captures.at(1)) }
remaining = remaining.slice(match.end)
matched = true
break
}
}
if not matched {
result += text(remaining.first())
remaining = remaining.slice(1)
}
}
return if result == none { formula } else { result }
}

// === Condition Processing ===
#let process_condition(cond) = {
let cond = cond.trim()
if cond in config.conditions.bottom.symbols {
return (none, if cond in ("heat", "hot") { sym.Delta } else { sym.Delta })
}
let is_bottom = (
config.conditions.bottom.identifiers.any(ids => ids.any(id => cond.starts-with(id)))
or config.conditions.bottom.units.any(unit => cond.ends-with(unit))
)
return if is_bottom { (none, cond) } else { (parse_formula(cond), none) }
}

// === Arrow Processing ===
#let process_arrow(arrow_text, condition: none) = {
let arrow = if arrow_text.contains("<-") {
$stretch(#sym.harpoons.rtlb, size: #config.arrow.reversible_size)$
} else {
$stretch(->, size: #config.arrow.arrow_size)$
}
let top = ()
let bottom = ()
if condition != none {
for cond in condition.split(",") {
let (t, b) = process_condition(cond)
if t != none { top.push(t) }
if b != none { bottom.push(b) }
}
}
$arrow^top.join(",")_bottom.join(",")$
}

// === Main Function ===
#let ce = (formula, condition: none) => {
let remaining = formula.trim()
let result = none
while remaining.len() > 0 {
let matched = false
for pattern in ("coef", "element", "arrow", "bracket", "plus", "charge") {
let match = remaining.match(regex_patterns.at(pattern))
if match != none {
result += if pattern == "plus" { $+$ } else if pattern == "coef" { $#match.text$ } else if (
pattern == "element"
) { process_element(match.captures.at(0), match.captures.at(1)) } else if pattern == "bracket" {
process_bracket(match.captures.at(0), match.captures.at(1))
} else if pattern == "charge" { process_charge(result, match.captures.at(0)) } else {
process_arrow(match.text, condition: condition)
}
remaining = remaining.slice(match.end)
matched = true
break
}
}
if not matched {
panic(
"Parse error at position "
+ str(formula.len() - remaining.len())
+ ": '"
+ remaining.first()
+ "' in formula: "
+ formula,
)
}
}
$upright(display(result))$
}
Loading

0 comments on commit bb660a8

Please sign in to comment.