Skip to content

Commit efa4b75

Browse files
typsium:0.0.1 (#1635)
Co-authored-by: Ana Gelez <[email protected]>
1 parent 9f5ed99 commit efa4b75

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 β-吲哚基丙氨酸
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Typst Chemical Formula Package
2+
3+
A Typst package (alpha) for typesetting chemical formulas, supporting both inorganic and organic chemistry notation.
4+
5+
## Features
6+
7+
- Basic chemical formula typesetting
8+
- Ion charge notation
9+
- Physical state markers
10+
- Organic chemistry structures
11+
- Reaction arrows and equilibrium symbols
12+
- Special support for phenyl group (Ph)
13+
- Support for C(n) general formula expressions
14+
15+
## License
16+
17+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/// Chemical formula typesetting package
2+
/// Version: 0.0.1
3+
4+
// 修改预编译的正则表达式模式
5+
#let rx = regex("([A-Z][a-z]?)(?:\\(([A-Z][A-Za-z]+|[0-9]*n(?:[+-][0-9]+)*)\\))?([0-9]+)?(?:\\^\\(([0-9+-]+)\\))?(?:\\(([a-z]+)\\))?")
6+
7+
// 新增有机化学正则表达式
8+
#let organic = (
9+
// 基本结构 - 改进对Cn模式的支持
10+
basic: regex("([A-Z][a-z]?)(?:\\(([A-Z][A-Za-z]+|[0-9]*n(?:[+-][0-9]+)*)\\))?([0-9]+|n)?(?:[A-Z][0-9]+)*"),
11+
// 双键和三键
12+
bonds: regex("(=|≡)"),
13+
// 有机基团 (如 -CH3, -OH, -COOH 等)
14+
groups: regex("\\-([A-Z][A-Za-z0-9]+)"),
15+
// 取代基位置
16+
position: regex("([0-9]+)\\-"),
17+
// 苯环缩写
18+
phenyl: regex("Ph"),
19+
)
20+
21+
// 预定义连接符样式
22+
#let bond-styles = (
23+
single: $minus$, // 单键
24+
double: $=$, // 双键
25+
triple: $$, // 三键
26+
)
27+
28+
/// 格式化化学式
29+
#let format(content) = {
30+
// 基础替换规则
31+
show "react": $arrow$
32+
show "equilibrium": math.arrow.l.r
33+
show "Delta": sym.Delta
34+
show "+": $+$
35+
show "-": bond-styles.single
36+
show "=": bond-styles.double
37+
show "": bond-styles.triple
38+
show "Ph": ""
39+
40+
// 有机化学特殊处理
41+
show organic.groups: it => {
42+
let group = it.text.slice(1)
43+
h(0.15em) + $minus$ + h(0.1em) + $upright(#group)$ // 优化基团连接
44+
}
45+
46+
show organic.position: it => {
47+
let pos = it.text.slice(0,-1)
48+
$upright(#pos)$ + h(0.1em) + $minus$ + h(0.15em) // 优化位置标记连接
49+
}
50+
51+
// 修改统一的匹配与处理部分
52+
show rx: it => {
53+
let groups = it.text.match(rx)
54+
if groups == none { return it }
55+
56+
let (base, group, num, charge, state) = groups.captures.slice(0)
57+
58+
let result = $upright(#base)$
59+
if group != none {
60+
// 处理括号中的表达式
61+
if group.contains("n") {
62+
// 改进对数学表达式的处理
63+
let expr = group
64+
.replace("n+", "n plus ") // 临时替换以避免与化学加号冲突
65+
.replace("n-", "n minus ")
66+
expr = $#expr.replace("plus", "+").replace("minus", "-")$
67+
result = $#result _(#expr)$
68+
} else {
69+
result = $#result thin (#group)$
70+
}
71+
}
72+
if num != none {
73+
// 特殊处理n的情况
74+
if num == "n" {
75+
result = $#result_n$
76+
} else {
77+
result = $#result _#num$
78+
}
79+
}
80+
if charge != none {
81+
show "+": math.plus
82+
show "-": math.minus
83+
result = $#result^#charge$
84+
}
85+
if state != none {
86+
// 修改状态标记显示方式
87+
let pure_state = state.trim("()")
88+
result = $#result _(#pure_state)$ // 使用双括号确保正确显示
89+
}
90+
91+
result
92+
}
93+
94+
$upright(#content)$
95+
}
96+
97+
// 导出接口
98+
#let chemical = format
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "typsium"
3+
version = "0.0.1"
4+
license = "MIT"
5+
entrypoint = "lib.typ"
6+
authors = ["β-吲哚基丙氨酸"]
7+
description = "Typeset chemical formulas, both with inorganic and organic chemistry notation."

0 commit comments

Comments
 (0)