Skip to content

Commit

Permalink
dev: update formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin committed Jun 17, 2024
1 parent 6b2f0cc commit 0c5dda7
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 51 deletions.
2 changes: 1 addition & 1 deletion packages/mitex/examples/example.typ
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ We also support text mode (in development):
\section{Title}
A \textbf{strong} text, a \emph{emph} text and inline equation $x + y$.
Also block \eqref{eq:pythagoras} and \ref{tab:example}.
\begin{equation}
Expand Down
34 changes: 25 additions & 9 deletions packages/mitex/specs/latex/standard.typ
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,46 @@
"olive": rgb(128, 128, 0),
)
#let get-tex-str-from-arr(arr) = arr.filter(it => it != [ ] and it != [#math.zws]).map(it => it.text).sum()
#let get-tex-str(tex) = if tex.has("children") { get-tex-str-from-arr(tex.children) } else { tex.text }
#let get-tex-str(tex) = if tex.has("children") {
get-tex-str-from-arr(tex.children)
} else {
tex.text
}
#let get-tex-color-from-arr(arr) = {
mitex-color-map.at(lower(get-tex-str-from-arr(arr)), default: none)
mitex-color-map.at(lower(get-tex-str-from-arr(arr)), default: none)
}
#let get-tex-color(texcolor) = if texcolor.has("children") {
get-tex-color-from-arr(texcolor.children)
} else {
texcolor.text
}
}

// 1. functions created to make it easier to define a spec
#let operatornamewithlimits(it) = math.op(limits: true, math.upright(it))
#let arrow-handle(arrow-sym) = define-cmd(1, handle: it => $limits(xarrow(sym: #arrow-sym, it))$)
#let _greedy-handle(fn) = (..args) => $fn(#args.pos().sum())$
#let greedy-handle(alias, fn) = define-greedy-cmd(alias, handle: _greedy-handle(fn))
#let limits-handle(alias, wrap) = define-cmd(1, alias: alias, handle: (it) => math.limits(wrap(it)))
#let matrix-handle(delim: none, handle: none) = define-env(none, kind: "is-matrix", alias: none, handle: math.mat.with(delim: delim))
#let call-or-ignore(fn) = (..args) => if args.pos().len() > 0 { fn(..args) } else { math.zws }
#let ignore-me = (..args) => {}
#let limits-handle(alias, wrap) = define-cmd(1, alias: alias, handle: it => math.limits(wrap(it)))
#let matrix-handle(delim: none, handle: none) = define-env(
none,
kind: "is-matrix",
alias: none,
handle: math.mat.with(delim: delim),
)
#let call-or-ignore(fn) = (..args) => if args.pos().len() > 0 {
fn(..args)
} else {
math.zws
}
#let ignore-me = (..args) => { }
#let ignore-sym = define-sym("")

// 2. Standard package definitions, generate specs and scopes,
// for parser/convert and typst respectively
#let (spec, scope) = process-spec((
#let (
spec,
scope,
) = process-spec((
// Text mode
section: define-cmd(1, alias: "#heading(level: 1)"),
subsection: define-cmd(1, alias: "#heading(level: 2)"),
Expand Down Expand Up @@ -1126,5 +1142,5 @@
LoadClassWithOptions: ignore-sym,
))

// export: include package name, spec and scope
// export: include package name, spec and scope
#let package = (name: "latex-std", spec: (commands: spec), scope: scope)
10 changes: 5 additions & 5 deletions packages/mitex/specs/mod.typ
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#let mitex-scope = packages.map(pkg => pkg.scope).sum()

[
// 2. export all packages with specs by metadata and <mitex-packages> label,
// mitex-cli can fetch them by
// `typst query --root . ./packages/mitex/specs/mod.typ "<mitex-packages>"`
#metadata(packages) <mitex-packages>
#packages
// 2. export all packages with specs by metadata and <mitex-packages> label,
// mitex-cli can fetch them by
// `typst query --root . ./packages/mitex/specs/mod.typ "<mitex-packages>"`
#metadata(packages) <mitex-packages>
#packages
]
121 changes: 85 additions & 36 deletions packages/mitex/specs/prelude.typ
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// Define a normal symbol, as no-argument commands like \alpha
///
/// Define a normal symbol, as no-argument commands like \alpha
///
/// Arguments:
/// - s (str): Alias command for typst handler.
/// For example, alias `\prod` to typst's `product`.
Expand All @@ -9,11 +9,18 @@
///
/// Return: A spec item and a scope item (none for no scope item)
#let define-sym(s, sym: none) = {
((kind: "alias-sym", alias: s), if sym != none { (alias: s, handle: sym) } else { none })
(
(kind: "alias-sym", alias: s),
if sym != none {
(alias: s, handle: sym)
} else {
none
},
)
}

/// Define a greedy command, like \displaystyle
///
///
/// Arguments:
/// - s (str): Alias command for typst handler.
/// For example, alias `\displaystyle` to typst's `mitexdisplay`, as the key in mitex-scope.
Expand All @@ -23,11 +30,18 @@
///
/// Return: A spec item and a scope item (none for no scope item)
#let define-greedy-cmd(s, handle: none) = {
((kind: "greedy-cmd", alias: s), if handle != none { (alias: s, handle: handle) } else { none })
(
(kind: "greedy-cmd", alias: s),
if handle != none {
(alias: s, handle: handle)
} else {
none
},
)
}

/// Define an infix command, like \over
///
///
/// Arguments:
/// - s (str): Alias command for typst handler.
/// For example, alias `\over` to typst's `frac`, as the key in mitex-scope.
Expand All @@ -37,7 +51,14 @@
///
/// Return: A spec item and a scope item (none for no scope item)
#let define-infix-cmd(s, handle: none) = {
((kind: "infix-cmd", alias: s), if handle != none { (alias: s, handle: handle) } else { none })
(
(kind: "infix-cmd", alias: s),
if handle != none {
(alias: s, handle: handle)
} else {
none
},
)
}

/// Define a glob (Global Wildcard) match command with a specified pattern for matching args
Expand All @@ -57,11 +78,18 @@
///
/// Return: A spec item and a scope item (none for no scope item)
#let define-glob-cmd(pat, s, handle: none) = {
((kind: "glob-cmd", pattern: pat, alias: s), if handle != none { (alias: s, handle: handle) } else { none })
(
(kind: "glob-cmd", pattern: pat, alias: s),
if handle != none {
(alias: s, handle: handle)
} else {
none
},
)
}

/// Define a command with a fixed number of arguments, like \hat{x} and \frac{1}{2}
///
///
/// Arguments:
/// - num (int): The number of arguments for the command.
/// - alias (str): Alias command for typst handler.
Expand All @@ -71,15 +99,22 @@
///
/// Return: A spec item and a scope item (none for no scope item)
#let define-cmd(num, alias: none, handle: none) = {
((
kind: "cmd",
args: ( "kind": "right", "pattern": ( kind: "fixed-len", len: num ) ),
alias: alias,
), if handle != none { (alias: alias, handle: handle) } else { none })
(
(
kind: "cmd",
args: ("kind": "right", "pattern": (kind: "fixed-len", len: num)),
alias: alias,
),
if handle != none {
(alias: alias, handle: handle)
} else {
none
},
)
}

/// Define an environment with a fixed number of arguments, like \begin{alignedat}{2}
///
///
/// Arguments:
/// - num (int): The number of arguments as environment options for the environment.
/// - alias (str): Alias command for typst handler.
Expand All @@ -95,35 +130,49 @@
///
/// Return: A spec item and a scope item (none for no scope item)
#let define-env(num, kind: "none", alias: none, handle: none) = {
((
kind: "env",
args: if num != none {
( kind: "fixed-len", len: num )
(
(
kind: "env",
args: if num != none {
(kind: "fixed-len", len: num)
} else {
(kind: "none")
},
ctx_feature: (kind: kind),
alias: alias,
),
if handle != none {
(alias: alias, handle: handle)
} else {
( kind: "none" )
none
},
ctx_feature: ( kind: kind ),
alias: alias,
), if handle != none { (alias: alias, handle: handle) } else { none })
)
}

#let define-glob-env(pat, kind: "none", alias: none, handle: none) = {
((
kind: "glob-env",
pattern: pat,
ctx_feature: ( kind: kind ),
alias: alias,
), if handle != none { (alias: alias, handle: handle) } else { none })
(
(
kind: "glob-env",
pattern: pat,
ctx_feature: (kind: kind),
alias: alias,
),
if handle != none {
(alias: alias, handle: handle)
} else {
none
},
)
}

/// Define a symbol without alias and without handler function, like \alpha => alpha
///
///
/// Return: A spec item and no scope item (none for no scope item)
#let sym = ((kind: "sym"), none)

/// Define a symbol without alias and with handler function,
/// like \negthinspace => h(-(3/18) * 1em)
///
///
/// Arguments:
/// - handle (function): The handler function, as the value of alias in mitex-scope.
/// For example, define `negthinspace` to handle `h(-(3/18) * 1em)` in mitex-scope
Expand All @@ -132,27 +181,27 @@
#let of-sym(handle) = ((kind: "sym"), (handle: handle))

/// Define a left1-op command without handler, like `\limits` for `\sum\limits`
///
///
/// Arguments:
/// - alias (str): Alias command for typst handler.
/// For example, alias `\limits` to typst's `limits`
/// and alias `\nolimits` to typst's `scripts`
///
/// Return: A cmd spec and no scope item (none for no scope item)
#let left1-op(alias) = ((kind: "cmd", args: ( kind: "left1" ), alias: alias), none)
#let left1-op(alias) = ((kind: "cmd", args: (kind: "left1"), alias: alias), none)

/// Define a cmd1 command like \hat{x} => hat(x)
///
///
/// Return: A cmd1 spec and a scope item (none for no scope item)
#let cmd1 = ((kind: "cmd1"), none)

/// Define a cmd2 command like \binom{1}{2} => binom(1, 2)
///
///
/// Return: A cmd2 spec and a scope item (none for no scope item)
#let cmd2 = ((kind: "cmd2"), none)

/// Define a matrix environment without handler
///
///
/// Return: A matrix-env spec and a scope item (none for no scope item)
#let matrix-env = ((kind: "matrix-env"), none)

Expand Down

0 comments on commit 0c5dda7

Please sign in to comment.