Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CH4typ:0.0.1 #1572

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/preview/CH4typ/0.0.1/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.
17 changes: 17 additions & 0 deletions packages/preview/CH4typ/0.0.1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Typst Chemical Formula Package

A Typst package (alpha) for typesetting chemical formulas, supporting both inorganic and organic chemistry notation.

## Features

- Basic chemical formula typesetting
- Ion charge notation
- Physical state markers
- Organic chemistry structures
- Reaction arrows and equilibrium symbols
- Special support for phenyl group (Ph)
- Support for C(n) general formula expressions

## License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
98 changes: 98 additions & 0 deletions packages/preview/CH4typ/0.0.1/lib.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/// Chemical formula typesetting package
/// Version: 0.0.1

// 修改预编译的正则表达式模式
#let rx = regex("([A-Z][a-z]?)(?:\\(([A-Z][A-Za-z]+|[0-9]*n(?:[+-][0-9]+)*)\\))?([0-9]+)?(?:\\^\\(([0-9+-]+)\\))?(?:\\(([a-z]+)\\))?")

// 新增有机化学正则表达式
#let organic = (
// 基本结构 - 改进对Cn模式的支持
basic: regex("([A-Z][a-z]?)(?:\\(([A-Z][A-Za-z]+|[0-9]*n(?:[+-][0-9]+)*)\\))?([0-9]+|n)?(?:[A-Z][0-9]+)*"),
// 双键和三键
bonds: regex("(=|≡)"),
// 有机基团 (如 -CH3, -OH, -COOH 等)
groups: regex("\\-([A-Z][A-Za-z0-9]+)"),
// 取代基位置
position: regex("([0-9]+)\\-"),
// 苯环缩写
phenyl: regex("Ph"),
)

// 预定义连接符样式
#let bond-styles = (
single: $minus$, // 单键
double: $=$, // 双键
triple: $≡$, // 三键
)

/// 格式化化学式
#let format(content) = {
// 基础替换规则
show "react": $arrow$
show "equilibrium": math.arrow.l.r
show "Delta": sym.Delta
show "+": $+$
show "-": bond-styles.single
show "=": bond-styles.double
show "≡": bond-styles.triple
show "Ph": "⌬"

// 有机化学特殊处理
show organic.groups: it => {
let group = it.text.slice(1)
h(0.15em) + $minus$ + h(0.1em) + $upright(#group)$ // 优化基团连接
}

show organic.position: it => {
let pos = it.text.slice(0,-1)
$upright(#pos)$ + h(0.1em) + $minus$ + h(0.15em) // 优化位置标记连接
}

// 修改统一的匹配与处理部分
show rx: it => {
let groups = it.text.match(rx)
if groups == none { return it }

let (base, group, num, charge, state) = groups.captures.slice(0)

let result = $upright(#base)$
if group != none {
// 处理括号中的表达式
if group.contains("n") {
// 改进对数学表达式的处理
let expr = group
.replace("n+", "n plus ") // 临时替换以避免与化学加号冲突
.replace("n-", "n minus ")
expr = $#expr.replace("plus", "+").replace("minus", "-")$
result = $#result _(#expr)$
} else {
result = $#result thin (#group)$
}
}
if num != none {
// 特殊处理n的情况
if num == "n" {
result = $#result_n$
} else {
result = $#result _#num$
}
}
if charge != none {
show "+": math.plus
show "-": math.minus
result = $#result^#charge$
}
if state != none {
// 修改状态标记显示方式
let pure_state = state.trim("()")
result = $#result _(#pure_state)$ // 使用双括号确保正确显示
}

result
}

$upright(#content)$
}

// 导出接口
#let chemical = format
7 changes: 7 additions & 0 deletions packages/preview/CH4typ/0.0.1/typst.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "CH4typ"

Check failure on line 2 in packages/preview/CH4typ/0.0.1/typst.toml

View check run for this annotation

Typst package check / @preview/CH4typ:0.0.1

packages/preview/CH4typ/0.0.1/typst.toml#L2

Please use kebab-case for package names.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to to use lower-kebab-case for your package name. The current name would become ch-4-typ, but (if I remember my chemistry class correctly) that would ruin the joke in the name. Maybe you can change the name entirely if you are not satisfied with that.

version = "0.0.1"
license = "MIT"
entrypoint = "lib.typ"
authors = ["β-吲哚基丙氨酸"]
description = "an alternative for chem-par packet"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion the description you wrote in your README is clearer to explain the purpose of the package, I would suggest copying it here.

Suggested change
description = "an alternative for chem-par packet"
description = "Typeset chemical formulas, both with inorganic and organic chemistry notation."

Loading