diff --git a/packages/preview/mitex/0.2.6/CHANGELOG.md b/packages/preview/mitex/0.2.6/CHANGELOG.md new file mode 100644 index 0000000000..0c37953b8f --- /dev/null +++ b/packages/preview/mitex/0.2.6/CHANGELOG.md @@ -0,0 +1,59 @@ +# CHANGELOG + +## 0.2.6 + +- Fix: fix "kai is deprecated" warning for Typst v0.14.0 (https://github.com/mitex-rs/mitex/pull/201) + + +## 0.2.5 + +- Fix: assign correct class for lvert and rvert (https://github.com/mitex-rs/mitex/pull/172) +- Fix: convert `bf` to `bold(upright(..))` (https://github.com/mitex-rs/mitex/pull/183) +- Fix: remove xarrow and use stretch in typst v0.12.0 (https://github.com/mitex-rs/mitex/pull/184) +- Fix: fix some symbols `\not`, `\gtreqqless` and `\gtrapprox` (https://github.com/mitex-rs/mitex/pull/185) + + +## 0.2.4 + +- Fix `\boxed` command. +- Support basic figure and tabular environments. +- Add escape symbols for plus, minus and percent. +- Fix `\left` and `\right`: size of lr use em. + + +## 0.2.3 + +- Support nesting math equation like `\text{$x$}`. +- Fix broken `\left` and `\right` commands. #143 +- Correct the symbol mapping for `\varphi`, `phi`, `\epsilon` and `\varepsilon`. + + +## 0.2.2 + +### Text Mode + +- Add `\(\)` and `\[\]` support. +- Add `\footnote` and `\cite`. + +### Fixes + +- Remove duplicate keys for error when a dict has duplicate keys in Typst 0.11. +- Fix bracket/paren group parsing. +- Remove extra spacing for ceiling symbols. + + +## 0.2.1 + +- Remove unnecessary zws. + + +## 0.2.0 + +- Add more symbols for equations. +- Basic macro support. +- Basic text mode support. + + +## 0.1.0 + +- LaTeX support for Typst, powered by Rust and WASM. We can now render LaTeX equations in real-time in Typst. \ No newline at end of file diff --git a/packages/preview/mitex/0.2.6/LICENSE b/packages/preview/mitex/0.2.6/LICENSE new file mode 100644 index 0000000000..a7e77cb28d --- /dev/null +++ b/packages/preview/mitex/0.2.6/LICENSE @@ -0,0 +1,176 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS \ No newline at end of file diff --git a/packages/preview/mitex/0.2.6/README.md b/packages/preview/mitex/0.2.6/README.md new file mode 100644 index 0000000000..491eedcf9f --- /dev/null +++ b/packages/preview/mitex/0.2.6/README.md @@ -0,0 +1,154 @@ +# [MiTeX](https://github.com/mitex-rs/mitex) + +**[LaTeX](https://www.latex-project.org/) support for [Typst](https://typst.app/), powered by [Rust](https://www.rust-lang.org/) and [WASM](https://webassembly.org/).** + +[MiTeX](https://github.com/mitex-rs/mitex) processes LaTeX code into an abstract syntax tree (AST). Then it transforms the AST into Typst code and evaluates code into Typst content by `eval` function. + +MiTeX has been proved to be practical on a large project. It has already correctly converted 32.5k equations from [OI Wiki](https://github.com/OI-wiki/OI-wiki). Compared to [texmath](https://github.com/jgm/texmath), MiTeX has a better display effect and performance in that wiki project. It is also more easy to use, since importing MiTeX to Typst is just one line of code, while texmath is an external program. + +In addition, MiTeX is not only **SMALL** but also **FAST**! MiTeX has a size of just about 185 KB, comparing that texmath has a size of 17 MB. A not strict but intuitive comparison is shown below. To convert 32.5k equations from OI Wiki, texmath takes about 109s, while MiTeX WASM takes only 2.28s and MiTeX x86 takes merely 0.085s. + +Thanks to [@Myriad-Dreamin](https://github.com/Myriad-Dreamin), he completed the most complex development work: developing the parser for generating AST. + +## MiTeX as a Typst Package + +- Use `mitex-convert` to convert LaTeX code into Typst code in string. +- Use `mi` to render an inline LaTeX equation in Typst. +- Use `mitex(numbering: none, supplement: auto, ..)` or `mimath` to render a block LaTeX equation in Typst. +- Use `mitext` to render a LaTeX text in Typst. + +PS: `#set math.equation(numbering: "(1)")` is also valid for MiTeX. + +Following is [a simple example](https://github.com/mitex-rs/mitex/blob/main/packages/mitex/examples/example.typ) of using MiTeX in Typst: + +```typst +#import "@preview/mitex:0.2.6": * + +#assert.eq(mitex-convert("\alpha x"), "alpha x ") + +Write inline equations like #mi("x") or #mi[y]. + +Also block equations (this case is from #text(blue.lighten(20%), link("https://katex.org/")[katex.org])): + +#mitex(` + \newcommand{\f}[2]{#1f(#2)} + \f\relax{x} = \int_{-\infty}^\infty + \f\hat\xi\,e^{2 \pi i \xi x} + \,d\xi +`) + +We also support text mode (in development): + +#mitext(` + \iftypst + #set math.equation(numbering: "(1)", supplement: "equation") + \fi + + \section{Title} + + A \textbf{strong} text, a \emph{emph} text and inline equation $x + y$. + + Also block \eqref{eq:pythagoras}. + + \begin{equation} + a^2 + b^2 = c^2 \label{eq:pythagoras} + \end{equation} +`) +``` + +![example](examples/example.png) + +## MiTeX as a CLI Tool + +### Installation + +Install latest nightly version by `cargo install --git https://github.com/mitex-rs/mitex mitex-cli`. + +### Usage + +```bash +mitex compile main.tex +# or (same as above) +mitex compile main.tex mitex.typ +``` + +## MiTeX as a Web App + +### MiTeX Online Math Converter + +We can convert LaTeX equations to Typst equations in web by wasm. https://mitex-rs.github.io/mitex/ + +### Underleaf + +We made a proof of concept online tex editor to show our conversion speed in text mode. The PoC loads files from a git repository and then runs typst compile in browser. As you see, each keystroking get response in preview quickly. + +https://mitex-rs.github.io/mitex/tools/underleaf.html + +https://github.com/mitex-rs/mitex/assets/34951714/0ce77a2c-0a7d-445f-b26d-e139f3038f83 + +## Implemented Features + +- [x] User-defined TeX (macro) commands, such as `\newcommand{\mysym}{\alpha}`. +- [x] LaTeX equations support. + - [x] Coloring commands (`\color{red} text`, `\textcolor{red}{text}`). + - [x] Support for various environments, such as aligned, matrix, cases. +- [x] Basic text mode support, you can use it to write LaTeX drafts. + - [x] `\section`, `\textbf`, `\emph`. + - [x] Inline and block math equations. + - [x] `\ref`, `\eqref` and `\label`. + - [x] `itemize` and `enumerate` environments. + +## Features to Implement + +- [ ] Pass command specification to MiTeX plugin dynamically. With that you can define a typst function `let myop(it) = op(upright(it))` and then use it by `\myop{it}`. +- [ ] Package support, which means that you can change set of commands by telling MiTeX to use a list of packages. +- [ ] Better text mode support, such as figure, algorithm and description environments. + +To achieve the latter two goals, we need a well-structured architecture for the text mode, along with intricate work. Currently, we don't have very clear ideas yet. If you are willing to contribute by discussing in the issues or even submitting pull requests, your contribution is highly welcome. + +## Differences between MiTeX and other solutions + +MiTeX has different objectives compared to [texmath](https://github.com/jgm/texmath) (a.k.a. [pandoc](https://pandoc.org/)): + +- MiTeX focuses on rendering LaTeX content correctly within Typst, leveraging the powerful programming capabilities of WASM and typst to achieve results that are essentially consistent with LaTeX display. +- texmath aims to be general-purpose converters and generate strings that are more human-readable. + +For example, MiTeX transforms `\frac{1}{2}_3` into `frac(1, 2)_3`, while texmath converts it into `1 / 2_3`. The latter's display is not entirely correct, whereas the former ensures consistency in display. + +Another example is that MiTeX transforms `(\frac{1}{2})` into `\(frac(1, 2)\)` instead of `(frac(1, 2))`, avoiding the use of automatic Left/Right to achieve consistency with LaTeX rendering. + +**Certainly, the greatest advantage is that you can directly write LaTeX content in Typst without the need for manual conversion!** + +## Submitting Issues + +If you find missing commands or bugs of MiTeX, please feel free to submit an issue [here](https://github.com/mitex-rs/mitex/issues). + +## Contributing to MiTeX + +Currently, MiTeX maintains following three parts of code: + +- A TeX parser library written in **Rust**, see [mitex-lexer](https://github.com/mitex-rs/mitex/tree/main/crates/mitex-lexer) and [mitex-parser](https://github.com/mitex-rs/mitex/tree/main/crates/mitex-parser). +- A TeX to Typst converter library written in **Rust**, see [mitex](https://github.com/mitex-rs/mitex/tree/main/crates/mitex). +- A list of TeX packages and comamnds written in **Typst**, which then used by the typst package, see [MiTeX Command Specification](https://github.com/mitex-rs/mitex/tree/main/packages/mitex/specs). + +For a translation process, for example, we have: + +``` +\frac{1}{2} + +===[parser]===> AST ===[converter]===> + +#eval("$frac(1, 2)$", scope: (frac: (num, den) => $(num)/(den)$)) +``` + +You can use the `#mitex-convert()` function to get the Typst Code generated from LaTeX Code. + +### Add missing TeX commands + +Even if you don't know Rust at all, you can still add missing TeX commands to MiTeX by modifying [specification files](https://github.com/mitex-rs/mitex/tree/main/packages/mitex/specs), since they are written in typst! You can open an issue to acquire the commands you want to add, or you can edit the files and submit a pull request. + +In the future, we will provide the ability to customize TeX commands, which will make it easier for you to use the commands you create for yourself. + +### Develop the parser and the converter + +See [CONTRIBUTING.md](https://github.com/mitex-rs/mitex/blob/main/CONTRIBUTING.md). diff --git a/packages/preview/mitex/0.2.6/examples/bench.typ b/packages/preview/mitex/0.2.6/examples/bench.typ new file mode 100644 index 0000000000..5b7b78f6c4 --- /dev/null +++ b/packages/preview/mitex/0.2.6/examples/bench.typ @@ -0,0 +1,44 @@ +#import "@preview/mitex:0.2.6": * + +#set page(width: 500pt) + +#assert.eq(mitex-convert("\alpha x"), "alpha x ") + +Write inline equations like #mi("x") or #mi[y]. + +Also block equations: + +#mitex("\alpha x" * 8000) + +/* +last^1 +17000 +Benchmark 1: typst compile --root . packages\mitex\examples\bench.typ + Time (mean ± σ): 323.1 ms ± 18.0 ms [User: 84.4 ms, System: 14.1 ms] + Range (min … max): 302.1 ms … 353.9 ms 10 runs +8000 +Benchmark 1: typst compile --root . packages\mitex\examples\bench.typ + Time (mean ± σ): 198.3 ms ± 6.5 ms [User: 50.2 ms, System: 24.6 ms] + Range (min … max): 188.5 ms … 207.1 ms 14 runs + +last^2 +17000 +Benchmark 1: typst compile --root . packages\mitex\examples\bench.typ + Time (mean ± σ): 638.8 ms ± 10.4 ms [User: 143.8 ms, System: 32.8 ms] + Range (min … max): 616.5 ms … 652.5 ms 10 runs +8000 +Benchmark 1: typst compile --root . packages\mitex\examples\bench.typ + Time (mean ± σ): 503.2 ms ± 15.1 ms [User: 109.4 ms, System: 28.1 ms] + Range (min … max): 485.8 ms … 535.5 ms 10 runs + +init +17000 +Benchmark 1: typst compile --root . typst-package\examples\bench.typ + Time (mean ± σ): 972.4 ms ± 28.3 ms [User: 223.4 ms, System: 62.2 ms] + Range (min … max): 938.4 ms … 1029.7 ms 10 runs +8000 +Benchmark 1: typst compile --root . typst-package\examples\bench.typ + Time (mean ± σ): 687.6 ms ± 20.6 ms [User: 154.4 ms, System: 24.8 ms] + Range (min … max): 668.2 ms … 731.7 ms 10 runs + +*/ diff --git a/packages/preview/mitex/0.2.6/examples/example.png b/packages/preview/mitex/0.2.6/examples/example.png new file mode 100644 index 0000000000..bbd0904fd6 Binary files /dev/null and b/packages/preview/mitex/0.2.6/examples/example.png differ diff --git a/packages/preview/mitex/0.2.6/examples/example.typ b/packages/preview/mitex/0.2.6/examples/example.typ new file mode 100644 index 0000000000..7cc658942f --- /dev/null +++ b/packages/preview/mitex/0.2.6/examples/example.typ @@ -0,0 +1,46 @@ +#import "@preview/mitex:0.2.6": * + +#set page(width: 500pt, height: auto, margin: 1em) + +Write inline equations like #mi(`x`). + +Also block equations (this case is from #text(blue.lighten(20%), link("https://katex.org/")[katex.org])): + +#mitex(```latex + \newcommand{\f}[2]{#1f(#2)} + \f\relax{x} = \int_{-\infty}^\infty + \f\hat\xi\,e^{2 \pi i \xi x} + \,d\xi +```) + +We also support text mode (in development): + +#mitext(```latex + \iftypst + #set math.equation(numbering: "(1)", supplement: "Equation") + \fi + + \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} + a^2 + b^2 = c^2 \label{eq:pythagoras} + \end{equation} + + \begin{table}[ht] + \centering + \begin{tabular}{|c|c|} + \hline + \textbf{Name} & \textbf{Age} \\ + \hline + John & 25 \\ + Jane & 22 \\ + \hline + \end{tabular} + \caption{This is an example table.} + \label{tab:example} + \end{table} +```) diff --git a/packages/preview/mitex/0.2.6/lib.typ b/packages/preview/mitex/0.2.6/lib.typ new file mode 100644 index 0000000000..3e33673a6f --- /dev/null +++ b/packages/preview/mitex/0.2.6/lib.typ @@ -0,0 +1 @@ +#import "mitex.typ": mitex-wasm, mitex-convert, mitex-scope, mitex, mitext, mimath, mi \ No newline at end of file diff --git a/packages/preview/mitex/0.2.6/mitex.typ b/packages/preview/mitex/0.2.6/mitex.typ new file mode 100644 index 0000000000..d22d04dc8c --- /dev/null +++ b/packages/preview/mitex/0.2.6/mitex.typ @@ -0,0 +1,45 @@ +#import "specs/mod.typ": mitex-scope +#let mitex-wasm = plugin("./mitex.wasm") + +#let get-elem-text(it) = { + { + if type(it) == str { + it + } else if type(it) == content and it.has("text") { + it.text + } else { + panic("Unsupported type: " + str(type(it))) + } + } +} + +#let mitex-convert(it, mode: "math", spec: bytes(())) = { + if mode == "math" { + str(mitex-wasm.convert_math(bytes(get-elem-text(it)), spec)) + } else { + str(mitex-wasm.convert_text(bytes(get-elem-text(it)), spec)) + } +} + +// Math Mode +#let mimath(it, block: true, ..args) = { + let res = mitex-convert(mode: "math", it) + let eval-res = eval("$" + res + "$", scope: mitex-scope) + math.equation(block: block, eval-res, ..args) +} + +// Text Mode +#let mitext(it) = { + let res = mitex-convert(mode: "text", it) + eval(res, mode: "markup", scope: mitex-scope) +} + +#let mitex(it, mode: "math", ..args) = { + if mode == "math" { + mimath(it, ..args) + } else { + mitext(it, ..args) + } +} + +#let mi = mimath.with(block: false) diff --git a/packages/preview/mitex/0.2.6/mitex.wasm b/packages/preview/mitex/0.2.6/mitex.wasm new file mode 100755 index 0000000000..13fc6953f7 Binary files /dev/null and b/packages/preview/mitex/0.2.6/mitex.wasm differ diff --git a/packages/preview/mitex/0.2.6/specs/README.md b/packages/preview/mitex/0.2.6/specs/README.md new file mode 100644 index 0000000000..891ab0da43 --- /dev/null +++ b/packages/preview/mitex/0.2.6/specs/README.md @@ -0,0 +1,250 @@ +# MiTeX Command Specification + +This includes ways to define specs, which can be used to define everything in the existing standard packages [latex-std](./latex/standard.typ). + +Even if you don't know Rust at all, you can still add missing TeX commands to MiTeX by modifing [latex-std](./latex/standard.typ), since they are written in typst! You can open an issue to acquire the commands you want to add, or you can edit the files and submit a pull request. + + +## Introduce + +For a translation process, for example, we have: + +``` +\frac{1}{2} + +===[parser]===> AST ===[converter]===> + +#eval("$frac(1, 2)$", scope: (frac: (num, den) => $(num)/(den)$)) +``` + +You can use the `#mitex-convert()` function to get the Typst Code generated from LaTeX Code. + +To achieve this, we need to define four components for LaTeX commands: + +- `cmd`: The name of the LaTeX command. Since LaTeX commands all start with `\`, we remove the leading `\` and use the command name as the key in the dictionary. + - There is also the concept of environments, for example, `\begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}` is a matrix environment. +- `args`: The argument pattern of the LaTeX command. For example: + - `\alpha` has no arguments; + - `\hat{x}` matches one argument on the right; + - `\frac{1}{2}` matches two arguments on the right; + - `\sqrt[3]{2}` includes optional arguments; + - `\sum\limits` has `limits` matching one argument on the left; + - `\displaystyle` greedily matches all arguments on the right; + - `x \over y` as an infix operator greedily matches all arguments on both sides. +- `alias`: The alias of the LaTeX command in Typst. + - The alias can be an existing symbol or function, for example, `alpha` and `binom(n, k)`; + - It can also be a key in the `mitex-scope` for `eval`, for example, our self-defined `frac`. +- `handle`: The value in the `mitex-scope`, which is our self-defined symbol or processing function. + - For example, the `frac` key corresponds to the value `(num, den) => $(num)/(den)$`. + +MiTeX would need them for converting your TeX commands into typst code. + +At the Typst level, we need `alias` and `handle`, which are then combined into the `mitex-scope` passed to the `eval` function as the `scope` parameter. + +In short, currently a command spec is a typst dict, with its keys are name of the tex command, and its values are opaque spec item, which contains necessary information for MiTeX. + +In [specification file for standard latex](./latex/standard.typ), It constructs and exports a such command spec. + +Next, we provides a bunch of convenient functions for constructing opaque spec items. + + +## Reference + +### `define-sym` + +Define a normal symbol, as no-argument commands like `\alpha`. + +```typst +#let define-sym(s, sym: none) = { .. } +``` + +**Arguments:** +- s (str): Alias command for typst handler. + For example, alias `\prod` to typst's `product`. +- sym (content): The specific content, as the value of alias in mitex-scope. + For example, there is no direct alias for \negthinspace symbol in typst, + but we can add `h(-(3/18) * 1em)` ourselves + +**Return:** A opaque spec item and a scope item (none for no scope item) + + +### `define-greedy-cmd` + +Define a greedy command, like `\displaystyle`. + +```typst +#let define-greedy-cmd(s, handle: none) = { .. } +``` + +**Arguments:** +- s (str): Alias command for typst handler. + For example, alias `\displaystyle` to typst's `mitexdisplay`, as the key in mitex-scope. +- handle (function): The handler function, as the value of alias in mitex-scope. + It receives a content argument as all greedy matches to the content + For example, we define `mitexdisplay` to `math.display` + +**Return:** A opaque spec item and a scope item (none for no scope item) + + +### `define-infix-cmd` + +Define an infix command, like `\over`. + +```typst +#let define-infix-cmd(s, handle: none) = { .. } +``` + +**Arguments:** +- s (str): Alias command for typst handler. + For example, alias `\over` to typst's `frac`, as the key in mitex-scope. +- handle (function): The handler function, as the value of alias in mitex-scope. + It receives two content arguments, as (prev, after) arguments. + For example, we define `\over` to `frac: (num, den) => $(num)/(den)$` + +**Return:** A opaque spec item and a scope item (none for no scope item) + + +### `define-glob-cmd` + +Define a glob (Global Wildcard) match command with a specified pattern for matching args +Kind of item to match: + +- Bracket/b: [] +- Parenthesis/p: () +- Term/t: any rest of terms, typically {} or single char + +```typst +#let define-glob-cmd(pat, s, handle: none) = { .. } +``` + +**Arguments:** +- pat (pattern): The pattern for glob-cmd + For example, `{,b}t` for `\sqrt` to support `\sqrt{2}` and `\sqrt[3]{2}` +- s (str): Alias command for typst handler. + For example, alias `\sqrt` to typst's `mitexsqrt`, as the key in mitex-scope. +- handle (function): The handler function, as the value of alias in mitex-scope. + It receives variable length arguments, for example `(2,)` or `([3], 2)` for sqrt. + Therefore you need to use `(.. arg) = > {..}` to receive them. + +**Return:** A opaque spec item and a scope item (none for no scope item) + + +### `define-cmd` + +Define a command with a fixed number of arguments, like `\hat{x}` and `\frac{1}{2}`. + +```typst +#let define-cmd(num, alias: none, handle: none) = { .. } +``` + +**Arguments:** +- num (int): The number of arguments for the command. +- alias (str): Alias command for typst handler. + For example, alias `\frac` to typst's `frac`, as the key in mitex-scope. +- handle (function): The handler function, as the value of alias in mitex-scope. + It receives fixed number of arguments, for example `frac(1, 2)` for `\frac{1}{2}`. + +**Return:** A opaque spec item and a scope item (none for no scope item) + + +### `define-env` + +Define an environment with a fixed number of arguments, like `\begin{array}{lr}`. + +```typst +#let define-env(num, alias: none, handle: none) = { .. } +``` + +**Arguments:** +- num (int): The number of arguments as environment options for the environment. +- alias (str): Alias command for typst handler. + For example, alias `\begin{array}{lr}` to typst's `mitexarray`, + and alias `\begin{aligned}` to typst's `aligned`, as the key in mitex-scope. +- kind (str): environment kind, it could be "is-math", "is-cases", "is-matrix", + "is-itemize", "is-enumerate" +- handle (function): The handler function, as the value of alias in mitex-scope. + It receives fixed number of named arguments as environment options, + for example `array(arg0: ..)` or `array(arg0: .., arg1: ..)`. + And it receives variable length arguments as environment body, + Therefore you need to use `(.. arg) = > {..}` to receive them. + +**Return:** A opaque spec item and a scope item (none for no scope item) + + +### `sym` + +Define a symbol without alias and without handler function, like \alpha => alpha + +```typst +#let sym = ((kind: "sym"), none) +``` + +**Return:** A opaque spec item and no scope item (none for no scope item) + + +### `of-sym` + +```typst +#let of-sym(handle) = ((kind: "sym"), (handle: handle)) +``` + +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 + +**Return:** A symbol spec and a scope item + + +### `left1-op` + +Define a left1-op command without handler, like `\limits` for `\sum\limits` + +```typst +#let left1-op(alias) = ((kind: "cmd", args: ( kind: "left1" ), alias: alias), none) +``` + +**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) + + +### `cmd1` + +Define a cmd1 command like \hat{x} => hat(x) + +```typst +#let cmd1 = ((kind: "cmd1"), none) +``` + +**Return:** A cmd1 spec and a scope item (none for no scope item) + + +### `cmd2` + +Define a cmd2 command like \binom{1}{2} => binom(1, 2) + +```typst +#let cmd2 = ((kind: "cmd2"), none) +``` + +**Return:** A cmd2 spec and a scope item (none for no scope item) + + +### `matrix-env` + +Define a matrix environment without handler + +```typst +#let matrix-env = ((kind: "matrix-env"), none) +``` + +**Return:** A matrix-env spec and a scope item (none for no scope item) + diff --git a/packages/preview/mitex/0.2.6/specs/latex/standard.typ b/packages/preview/mitex/0.2.6/specs/latex/standard.typ new file mode 100644 index 0000000000..4e390101a4 --- /dev/null +++ b/packages/preview/mitex/0.2.6/specs/latex/standard.typ @@ -0,0 +1,1145 @@ +#import "../prelude.typ": * + +// 0. Some useful internal variables or functions +#let mitex-color-map = ( + "red": rgb(255, 0, 0), + "green": rgb(0, 255, 0), + "blue": rgb(0, 0, 255), + "cyan": rgb(0, 255, 255), + "magenta": rgb(255, 0, 255), + "yellow": rgb(255, 255, 0), + "black": rgb(0, 0, 0), + "white": rgb(255, 255, 255), + "gray": rgb(128, 128, 128), + "lightgray": rgb(192, 192, 192), + "darkgray": rgb(64, 64, 64), + "brown": rgb(165, 42, 42), + "orange": rgb(255, 165, 0), + "pink": rgb(255, 182, 193), + "purple": rgb(128, 0, 128), + "teal": rgb(0, 128, 128), + "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-color-from-arr(arr) = { + 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(stretch(#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 ignore-sym = define-sym("") + +// 2. Standard package definitions, generate specs and scopes, +// for parser/convert and typst respectively +#let ( + spec, + scope, +) = process-spec(( + // Text mode + section: define-cmd(1, alias: "#heading(level: 1)"), + subsection: define-cmd(1, alias: "#heading(level: 2)"), + subsubsection: define-cmd(1, alias: "#heading(level: 3)"), + footnote: define-cmd(1, alias: "#footnote"), + cite: define-cmd(1, alias: "#mitexcite", handle: it => cite(label(get-tex-str(it)))), + emph: define-cmd(1, alias: "#emph"), + label: define-cmd(1, alias: "mitexlabel", handle: ignore-me), + tag: define-cmd(1, alias: "mitexlabel", handle: ignore-me), + ref: define-cmd(1, alias: "#mitexref", handle: it => ref(label(get-tex-str(it)))), + eqref: define-cmd(1, alias: "#mitexref"), + item: ignore-sym, + // environments for text mode + itemize: define-env(none, kind: "is-itemize"), + enumerate: define-env(none, kind: "is-enumerate"), + quote: define-env(none, alias: "quote(block: true)"), + abstract: define-env(none, alias: "quote(block: true)"), + figure: define-glob-env("{,b}", kind: "is-figure", alias: "figure"), + table: define-glob-env("{,b}", kind: "is-figure", alias: "figure"), + tabular: define-env(1, kind: "is-table", alias: "table"), + // commands for figure + centering: ignore-sym, + textwidth: sym, + caption: define-cmd(1, alias: "mitexcaption", handle: ignore-me), + includegraphics: define-glob-cmd("{,b}t", "#miteximage", handle: ignore-me), + // commands for tabular + toprule: define-glob-cmd("{,b}", "toprule"), + midrule: define-glob-cmd("{,b}", "midrule"), + bottomrule: define-glob-cmd("{,b}", "bottomrule"), + hline: ignore-sym, + vline: ignore-sym, + // Spaces: \! \, \> \: \; \ \quad \qquad + "!": define-sym("negthinspace", sym: h(-(3/18) * 1em)), + negthinspace: of-sym(h(-(3/18) * 1em)), + negthinmedspace: of-sym(h(-(3/18) * 1em)), + negmedspace: of-sym(h(-(4/18) * 1em)), + negthickspace: of-sym(h(-(5/18) * 1em)), + ",": define-sym("thin"), + thinspace: define-sym("thin"), + ">": define-sym("med"), + ":": define-sym("med"), + medspace: define-sym("med"), + ";": define-sym("thick"), + "": define-sym("thick"), + thickspace: define-sym("thick"), + enspace: of-sym(h((1/2) * 1em)), + nobreakspace: define-sym("space.nobreak"), + space: sym, + quad: sym, + qquad: define-sym("wide"), + phantom: define-cmd(1, handle: hide), + hphantom: define-cmd(1, handle: it => box(height: 0pt, hide(it))), + vphantom: define-cmd(1, handle: it => box(width: 0pt, hide(it))), + // Escape symbols + "_": define-sym("\\_"), + "^": define-sym("hat"), + "*": define-sym(""), + "|": define-sym("||"), + "&": define-sym("amp"), + "#": define-sym("hash"), + "%": define-sym("%"), + "-": define-sym("-"), + "+": define-sym("+"), + "$": define-sym("dollar"), + "{": define-sym("\\{"), + "}": define-sym("\\}"), + vert: define-sym("|"), + lvert: of-sym(math.class("opening", "|")), + rvert: of-sym(math.class("closing", "|")), + Vert: define-sym("||"), + lVert: of-sym(math.class("opening", "||")), + rVert: of-sym(math.class("closing", "||")), + lparen: define-sym("paren.l"), + rparen: define-sym("paren.r"), + lceil: define-sym("⌈"), + rceil: define-sym("⌉"), + lfloor: define-sym("⌊"), + rfloor: define-sym("⌋"), + // Sizes and styles + displaystyle: greedy-handle("mitexdisplay", math.display), + textstyle: greedy-handle("mitexinline", math.inline), + scriptstyle: greedy-handle("mitexscript", math.script), + scriptscriptstyle: greedy-handle("mitexsscript", math.sscript), + bf: greedy-handle("mitexbold", it => math.bold(math.upright(it))), + rm: greedy-handle("mitexupright", math.upright), + it: greedy-handle("mitexitalic", math.italic), + sf: greedy-handle("mitexsans", math.sans), + frak: greedy-handle("mitexfrak", math.frak), + tt: greedy-handle("mitexmono", math.mono), + cal: greedy-handle("mitexcal", math.cal), + bold: define-cmd(1, alias: "bold"), + mathbf: define-cmd(1, alias: "mitexmathbf", handle: it => math.bold(math.upright(it))), + bm: define-cmd(1, alias: "bold"), + boldsymbol: define-cmd(1, alias: "bold"), + pmb: define-cmd(1, alias: "bold"), + mathrm: define-cmd(1, alias: "upright"), + mathit: define-cmd(1, alias: "italic"), + mathnormal: define-cmd(1, alias: "italic"), + mathsf: define-cmd(1, alias: "sans"), + mathfrak: define-cmd(1, alias: "frak"), + mathtt: define-cmd(1, alias: "mono"), + Bbb: define-cmd(1, alias: "bb"), + mathbb: define-cmd(1, alias: "bb"), + mathcal: define-cmd(1, alias: "cal"), + mathbin: define-cmd(1, handle: it => math.class("binary", it)), + mathclose: define-cmd(1, handle: it => math.class("closing", it)), + mathinner: define-cmd(1, handle: it => math.class("fence", it)), + mathop: define-cmd(1, handle: it => math.class("unary", it)), + mathopen: define-cmd(1, handle: it => math.class("opening", it)), + mathord: define-cmd(1, handle: it => math.class("normal", it)), + mathpunct: define-cmd(1, handle: it => math.class("punctuation", it)), + mathrel: define-cmd(1, handle: it => math.class("relation", it)), + big: define-cmd(1, handle: it => math.lr(size: 1.2em, it)), + Big: define-cmd(1, handle: it => math.lr(size: 1.8em, it)), + bigg: define-cmd(1, handle: it => math.lr(size: 2.4em, it)), + Bigg: define-cmd(1, handle: it => math.lr(size: 3em, it)), + bigl: define-cmd(1, alias: "big"), + Bigl: define-cmd(1, alias: "Big"), + biggl: define-cmd(1, alias: "bigg"), + Biggl: define-cmd(1, alias: "Bigg"), + bigm: define-cmd(1, alias: "big"), + Bigm: define-cmd(1, alias: "Big"), + biggm: define-cmd(1, alias: "bigg"), + Biggm: define-cmd(1, alias: "Bigg"), + bigr: define-cmd(1, alias: "big"), + Bigr: define-cmd(1, alias: "Big"), + biggr: define-cmd(1, alias: "bigg"), + Biggr: define-cmd(1, alias: "Bigg"), + // todo: size, especially multi-line, ignore it for now. + Huge: ignore-sym, + normalsize: ignore-sym, + huge: ignore-sym, + small: ignore-sym, + footnotesize: ignore-sym, + Large: ignore-sym, + LARGE: ignore-sym, + scriptsize: ignore-sym, + large: ignore-sym, + tiny: ignore-sym, + // Colors + color: define-greedy-cmd("mitexcolor", handle: (texcolor, ..args) => { + let color = get-tex-color(texcolor) + if color != none { + text(fill: color, args.pos().sum()) + } else { + args.pos().sum() + } + }), + textcolor: define-cmd(2, alias: "colortext", handle: (texcolor, body) => { + let color = get-tex-color(texcolor) + if color != none { + text(fill: get-tex-color(texcolor), body) + } else { + body + } + }), + colorbox: define-cmd(2, handle: (texcolor, body) => { + let color = get-tex-color(texcolor) + if color != none { + box(fill: get-tex-color(texcolor), $body$) + } else { + body + } + }), + // Limits + limits: left1-op("limits"), + nolimits: left1-op("scripts"), + // Commands + frac: define-cmd(2, handle: (num, den) => $(num)/(den)$), + // todo: cfrac, dfrac are same? + cfrac: define-cmd(2, handle: (num, den) => $display((num)/(den))$), + dfrac: define-cmd(2, handle: (num, den) => $display((num)/(den))$), + tfrac: define-cmd(2, handle: (num, den) => $inline((num)/(den))$), + binom: define-cmd(2), + dbinom: define-cmd(2, handle: (n, k) => $display(binom(#n, #k))$), + tbinom: define-cmd(2, handle: (n, k) => $inline(binom(#n, #k))$), + stackrel: define-cmd(2, handle: (sup, base) => $limits(base)^(sup)$), + substack: define-cmd(1, handle: it => it), + overset: define-cmd(2, handle: (sup, base) => $limits(base)^(sup)$), + underset: define-cmd(2, handle: (sub, base) => $limits(base)_(sub)$), + // Accents + "not": define-cmd(1, alias: "mitexnot", handle: it => math.cancel(angle: 20deg, it)), + cancel: define-cmd(1), + xcancel: define-cmd(1, handle: math.cancel), + bcancel: define-cmd(1, handle: math.cancel.with(inverted: true)), + sout: define-cmd(1, handle: math.cancel.with(angle: 90deg)), + grave: define-cmd(1, alias: "grave"), + acute: define-cmd(1, alias: "acute"), + hat: define-cmd(1, alias: "hat"), + widehat: define-cmd(1, alias: "hat"), + tilde: define-cmd(1, alias: "tilde"), + widetilde: define-cmd(1, alias: "tilde"), + bar: define-cmd(1, alias: "macron"), + breve: define-cmd(1, alias: "breve"), + dot: define-cmd(1, alias: "dot"), + ddot: define-cmd(1, alias: "dot.double"), + dddot: define-cmd(1, alias: "dot.triple"), + ddddot: define-cmd(1, alias: "dot.quad"), + H: define-cmd(1, alias: "acute.double"), + check: define-cmd(1, alias: "caron"), + widecheck: define-cmd(1, alias: "caron"), + u: define-cmd(1, alias: "breve"), + v: define-cmd(1, alias: "caron"), + r: define-cmd(1, alias: "circle"), + vec: define-cmd(1, alias: "arrow"), + overrightarrow: define-cmd(1, alias: "arrow"), + overleftarrow: define-cmd(1, alias: "arrow.l"), + overline: cmd1, + underline: cmd1, + overbrace: limits-handle("mitexoverbrace", math.overbrace), + underbrace: limits-handle("mitexunderbrace", math.underbrace), + overbracket: limits-handle("mitexoverbracket", math.overbracket), + underbracket: limits-handle("mitexunderbracket", math.underbracket), + boxed: define-cmd(1, handle: it => box(stroke: 0.5pt, inset: 6pt, $it$)), + // Greeks + alpha: sym, + beta: sym, + gamma: sym, + delta: sym, + epsilon: define-sym("epsilon.alt"), + zeta: sym, + eta: sym, + theta: sym, + iota: sym, + kappa: sym, + lambda: sym, + mu: sym, + nu: sym, + xi: sym, + omicron: sym, + pi: sym, + rho: sym, + sigma: sym, + tau: sym, + upsilon: sym, + phi: define-sym("phi.alt"), + chi: sym, + psi: sym, + omega: sym, + Alpha: sym, + Beta: sym, + Gamma: sym, + Delta: sym, + Epsilon: sym, + Zeta: sym, + Eta: sym, + Theta: sym, + Iota: sym, + Kappa: sym, + Lambda: sym, + Mu: sym, + Nu: sym, + Xi: sym, + Omicron: sym, + Pi: sym, + Rho: sym, + Sigma: sym, + Tau: sym, + Upsilon: sym, + Phi: sym, + Chi: sym, + Psi: sym, + Omega: sym, + varbeta: define-sym("beta.alt"), + varepsilon: define-sym("epsilon"), + varkappa: define-sym("kappa.alt"), + varphi: define-sym("phi"), + varpi: define-sym("pi.alt"), + varrho: define-sym("rho.alt"), + varsigma: define-sym("sigma.alt"), + vartheta: define-sym("theta.alt"), + ell: sym, + // Function symbols + sin: sym, + cos: sym, + tan: sym, + cot: sym, + sec: sym, + csc: sym, + arcsin: sym, + arccos: sym, + arctan: sym, + sinh: sym, + cosh: sym, + tanh: sym, + coth: sym, + ln: sym, + log: sym, + lg: sym, + lim: sym, + limsup: sym, + liminf: sym, + hom: sym, + det: sym, + exp: sym, + deg: sym, + gcd: sym, + lcm: sym, + dim: sym, + ker: sym, + arg: sym, + Pr: sym, + // Limits + max: sym, + min: sym, + argmax: sym, + argmin: sym, + sup: sym, + inf: sym, + sum: sym, + prod: define-sym("product"), + int: define-sym("integral"), + iint: define-sym("integral.double"), + iiint: define-sym("integral.triple"), + oint: define-sym("integral.cont"), + oiint: define-sym("integral.surf"), + oiiint: define-sym("integral.vol"), + // Symbols + mod: define-sym("mod"), + bmod: define-sym("mod"), + cdot: define-sym("dot.c"), + cdotp: define-sym("dot.c"), + sdot: define-sym("dot.c"), + times: define-sym("times"), + oplus: define-sym("plus.circle"), + ominus: define-sym("minus.circle"), + pm: define-sym("plus.minus"), + plusmn: define-sym("plus.minus"), + mp: define-sym("minus.plus"), + div: define-sym("div"), + star: define-sym("star"), + cap: define-sym("sect"), + cup: define-sym("union"), + "in": define-sym("in"), + isin: define-sym("in"), + notin: define-sym("in.not"), + subset: define-sym("subset"), + subseteq: define-sym("subset.eq"), + subsetneqq: define-sym("⫋"), + ne: define-sym("!="), + neq: define-sym("!="), + lt: define-sym("<"), + gt: define-sym(">"), + le: define-sym("<="), + ge: define-sym(">="), + leq: define-sym("<="), + geq: define-sym(">="), + leqslant: define-sym("lt.eq.slant"), + geqslant: define-sym("gt.eq.slant"), + eqslantgtr: define-sym("⪖"), + eqslantless: define-sym("⪕"), + approx: define-sym("approx"), + leftarrow: define-sym("<-"), + rightarrow: define-sym("->"), + leftrightarrow: define-sym("<->"), + Leftarrow: define-sym("arrow.l.double"), + Rightarrow: define-sym("=>"), + Leftrightarrow: define-sym("<=>"), + larr: define-sym("<-"), + rarr: define-sym("->"), + lrarr: define-sym("<->"), + lArr: define-sym("arrow.l.double"), + rArr: define-sym("=>"), + lrArr: define-sym("<=>"), + Larr: define-sym("arrow.l.double"), + Rarr: define-sym("=>"), + Lrarr: define-sym("<=>"), + longleftarrow: define-sym("<--"), + longrightarrow: define-sym("-->"), + longleftrightarrow: define-sym("<-->"), + Longleftarrow: define-sym("<=="), + Longrightarrow: define-sym("==>"), + Longleftrightarrow: define-sym("<==>"), + to: define-sym("->"), + implies: define-sym("==>"), + impliedby: define-sym("<=="), + gets: define-sym("<-"), + mapsto: define-sym("|->"), + infty: define-sym("oo"), + lbrack: define-sym("bracket.l"), + rbrack: define-sym("bracket.r"), + lgroup: define-sym("⟮"), + rgroup: define-sym("⟯"), + llbracket: define-sym("bracket.l.double"), + rrbracket: define-sym("bracket.r.double"), + angle: define-sym("angle"), + lang: define-sym("angle.l"), + rang: define-sym("angle.r"), + langle: define-sym("angle.l"), + rangle: define-sym("angle.r"), + measuredangle: define-sym("angle.arc"), + sphericalangle: define-sym("angle.spheric"), + ast: define-sym("ast"), + checkmark: define-sym("checkmark"), + circledast: define-sym("ast.circle"), + backslash: define-sym("backslash"), + dagger: define-sym("dagger"), + ddagger: define-sym("dagger.double"), + circleddash: define-sym("dash.circle"), + odot: define-sym("dot.circle"), + bigodot: define-sym("dot.circle.big"), + boxdot: define-sym("dot.square"), + dots: define-sym("dots.h"), + cdots: define-sym("dots.h.c"), + ldots: define-sym("dots.h"), + vdots: define-sym("dots.v"), + ddots: define-sym("dots.down"), + dotsb: define-sym("dots.h.c"), + dotsc: define-sym("dots.h"), + dotsi: define-sym("dots.h.c"), + dotsm: define-sym("dots.h.c"), + dotso: define-sym("dots.h"), + sim: define-sym("tilde"), + simeq: define-sym("tilde.eq"), + backsimeq: define-sym("tilde.eq.rev"), + cong: define-sym("tilde.equiv"), + ncong: define-sym("tilde.equiv.not"), + nsim: define-sym("tilde.not"), + backsim: define-sym("tilde.rev"), + prime: define-sym("prime"), + backprime: define-sym("prime.rev"), + bigoplus: define-sym("plus.circle.big"), + dotplus: define-sym("plus.dot"), + boxplus: define-sym("plus.square"), + boxminus: define-sym("minus.square"), + eqsim: define-sym("minus.tilde"), + otimes: define-sym("times.circle"), + bigotimes: define-sym("times.circle.big"), + divideontimes: define-sym("times.div"), + leftthreetimes: define-sym("times.three.l"), + rightthreetimes: define-sym("times.three.r"), + ltimes: define-sym("times.l"), + rtimes: define-sym("times.r"), + boxtimes: define-sym("times.square"), + triangleq: define-sym("eq.delta"), + curlyeqprec: define-sym("eq.prec"), + curlyeqsucc: define-sym("eq.succ"), + gtrdot: define-sym("gt.dot"), + gg: define-sym("gt.double"), + gtreqless: define-sym("gt.eq.lt"), + gtreqqless: define-sym("⪌"), + ngeq: define-sym("gt.eq.not"), + geqq: define-sym("gt.equiv"), + gtrless: define-sym("gt.lt"), + gneqq: define-sym("gt.nequiv"), + ngtr: define-sym("gt.not"), + gnsim: define-sym("gt.ntilde"), + gtrsim: define-sym("gt.tilde"), + vartriangleright: define-sym("gt.tri"), + trianglerighteq: define-sym("gt.tri.eq"), + ntrianglerighteq: define-sym("gt.tri.eq.not"), + ntriangleright: define-sym("gt.tri.not"), + ggg: define-sym("gt.triple"), + lessdot: define-sym("lt.dot"), + ll: define-sym("lt.double"), + lesseqgtr: define-sym("lt.eq.gt"), + nleq: define-sym("lt.eq.not"), + leqq: define-sym("lt.equiv"), + lessgtr: define-sym("lt.gt"), + lneqq: define-sym("lt.nequiv"), + nless: define-sym("lt.not"), + lnsim: define-sym("lt.ntilde"), + lesssim: define-sym("lt.tilde"), + vartriangleleft: define-sym("lt.tri"), + trianglelefteq: define-sym("lt.tri.eq"), + ntrianglelefteq: define-sym("lt.tri.eq.not"), + ntriangleleft: define-sym("lt.tri.not"), + lll: define-sym("lt.triple"), + approxeq: define-sym("approx.eq"), + prec: define-sym("prec"), + precapprox: define-sym("prec.approx"), + preceq: define-sym("prec.eq"), + preccurlyeq: define-sym("prec.eq"), + npreceq: define-sym("prec.eq.not"), + precnapprox: define-sym("prec.napprox"), + nprec: define-sym("prec.not"), + precnsim: define-sym("prec.ntilde"), + precsim: define-sym("prec.tilde"), + succ: define-sym("succ"), + succapprox: define-sym("succ.approx"), + succeq: define-sym("succ.eq"), + succcurlyeq: define-sym("succ.eq"), + nsucceq: define-sym("succ.eq.not"), + succnapprox: define-sym("succ.napprox"), + nsucc: define-sym("succ.not"), + succnsim: define-sym("succ.ntilde"), + succsim: define-sym("succ.tilde"), + equiv: define-sym("equiv"), + propto: define-sym("prop"), + empty: define-sym("nothing"), + emptyset: define-sym("nothing"), + varnothing: define-sym("nothing"), + o: define-sym("nothing"), + O: define-sym("nothing"), + osplash: define-sym("⊘"), + setminus: define-sym("without"), + And: define-sym("amp"), + bigcirc: define-sym("circle.stroked.big"), + smallsetminus: define-sym("without"), + complement: define-sym("complement"), + ni: define-sym("in.rev"), + notni: define-sym("in.rev.not"), + Subset: define-sym("subset.double"), + nsubseteq: define-sym("subset.eq.not"), + sqsubseteq: define-sym("subset.eq.sq"), + subsetneq: define-sym("subset.neq"), + supset: define-sym("supset"), + Supset: define-sym("supset.double"), + supseteq: define-sym("supset.eq"), + nsupseteq: define-sym("supset.eq.not"), + sqsupseteq: define-sym("supset.eq.sq"), + supsetneq: define-sym("supset.neq"), + bigcup: define-sym("union.big"), + Cup: define-sym("union.double"), + uplus: define-sym("union.plus"), + biguplus: define-sym("union.plus.big"), + sqcup: define-sym("union.sq"), + bigsqcup: define-sym("union.sq.big"), + bigcap: define-sym("sect.big"), + Cap: define-sym("sect.double"), + sqcap: define-sym("sect.sq"), + partial: define-sym("diff"), + nabla: define-sym("nabla"), + coprod: define-sym("product.co"), + forall: define-sym("forall"), + exist: define-sym("exists"), + exists: define-sym("exists"), + nexists: define-sym("exists.not"), + top: define-sym("top"), + bot: define-sym("bot"), + neg: define-sym("not"), + lnot: define-sym("not"), + land: define-sym("and"), + wedge: define-sym("and"), + lor: define-sym("or"), + bigwedge: define-sym("and.big"), + curlywedge: define-sym("and.curly"), + vee: define-sym("or"), + bigvee: define-sym("or.big"), + curlyvee: define-sym("or.curly"), + models: define-sym("models"), + therefore: define-sym("therefore"), + because: define-sym("because"), + blacksquare: define-sym("qed"), + circ: define-sym("compose"), + multimap: define-sym("multimap"), + mid: define-sym("divides"), + nmid: define-sym("divides.not"), + wr: define-sym("wreath"), + parallel: define-sym("parallel"), + shortparallel: define-sym("parallel"), + nparallel: define-sym("parallel.not"), + perp: define-sym("perp"), + Join: define-sym("join"), + pounds: define-sym("pound"), + clubsuit: define-sym("suit.club"), + spadesuit: define-sym("suit.spade"), + bull: define-sym("bullet"), + bullet: define-sym("bullet"), + circledcirc: define-sym("circle.nested"), + rhd: define-sym("triangle.stroked.r"), + lhd: define-sym("triangle.stroked.l"), + triangle: define-sym("triangle.stroked.t"), + bigtriangleup: define-sym("triangle.stroked.t"), + bigtriangledown: define-sym("triangle.stroked.b"), + triangleright: define-sym("triangle.stroked.small.r"), + triangledown: define-sym("triangle.stroked.small.b"), + triangleleft: define-sym("triangle.stroked.small.l"), + vartriangle: define-sym("triangle.stroked.small.t"), + square: define-sym("square.stroked"), + Diamond: define-sym("diamond.stroked"), + diamond: define-sym("diamond.stroked.small"), + diamonds: define-sym("diamond.stroked"), + diamondsuit: define-sym("diamond.stroked"), + lozenge: define-sym("lozenge.stroked"), + blacklozenge: define-sym("lozenge.filled"), + bigstar: define-sym("star.stroked"), + longmapsto: define-sym("arrow.r.long.bar"), + nRightarrow: define-sym("arrow.r.double.not"), + hookrightarrow: define-sym("arrow.r.hook"), + looparrowright: define-sym("arrow.r.loop"), + nrightarrow: define-sym("arrow.r.not"), + rightsquigarrow: define-sym("arrow.r.squiggly"), + rightarrowtail: define-sym("arrow.r.tail"), + Rrightarrow: define-sym("arrow.r.triple"), + twoheadrightarrow: define-sym("arrow.r.twohead"), + nLeftarrow: define-sym("arrow.l.double.not"), + hookleftarrow: define-sym("arrow.l.hook"), + looparrowleft: define-sym("arrow.l.loop"), + nleftarrow: define-sym("arrow.l.not"), + leftarrowtail: define-sym("arrow.l.tail"), + Lleftarrow: define-sym("arrow.l.triple"), + twoheadleftarrow: define-sym("arrow.l.twohead"), + uparrow: define-sym("arrow.t"), + Uparrow: define-sym("arrow.t.double"), + downarrow: define-sym("arrow.b"), + Downarrow: define-sym("arrow.b.double"), + iff: define-sym("arrow.l.r.double.long"), + nLeftrightarrow: define-sym("arrow.l.r.double.not"), + nleftrightarrow: define-sym("arrow.l.r.not"), + leftrightsquigarrow: define-sym("arrow.l.r.wave"), + updownarrow: define-sym("arrow.t.b"), + Updownarrow: define-sym("arrow.t.b.double"), + nearrow: define-sym("arrow.tr"), + searrow: define-sym("arrow.br"), + nwarrow: define-sym("arrow.tl"), + swarrow: define-sym("arrow.bl"), + circlearrowleft: define-sym("arrow.ccw"), + curvearrowleft: define-sym("arrow.ccw.half"), + circlearrowright: define-sym("arrow.cw"), + curvearrowright: define-sym("arrow.cw.half"), + rightrightarrows: define-sym("arrows.rr"), + leftleftarrows: define-sym("arrows.ll"), + upuparrows: define-sym("arrows.tt"), + downdownarrows: define-sym("arrows.bb"), + leftrightarrows: define-sym("arrows.lr"), + rightleftarrows: define-sym("arrows.rl"), + rightharpoonup: define-sym("harpoon.rt"), + rightharpoondown: define-sym("harpoon.rb"), + leftharpoonup: define-sym("harpoon.lt"), + leftharpoondown: define-sym("harpoon.lb"), + upharpoonleft: define-sym("harpoon.tl"), + upharpoonright: define-sym("harpoon.tr"), + downharpoonleft: define-sym("harpoon.bl"), + downharpoonright: define-sym("harpoon.br"), + leftrightharpoons: define-sym("harpoons.ltrb"), + rightleftharpoons: define-sym("harpoons.rtlb"), + vdash: define-sym("tack.r"), + nvdash: define-sym("tack.r.not"), + vDash: define-sym("tack.r.double"), + nvDash: define-sym("tack.r.double.not"), + dashv: define-sym("tack.l"), + hbar: define-sym("planck.reduce"), + hslash: define-sym("planck.reduce"), + Re: define-sym("Re"), + Im: define-sym("Im"), + AA: define-sym("circle(A)"), + aa: define-sym("circle(A)"), + Box: define-sym("ballot"), + N: define-sym("NN"), + natnums: define-sym("NN"), + natural: define-sym("♮"), + P: define-sym("pilcrow"), + Q: define-sym("QQ"), + R: define-sym("RR"), + Z: define-sym("ZZ"), + S: define-sym("section"), + sect: define-sym("section"), + AE: define-sym("Æ"), + ae: define-sym("æ"), + alef: define-sym("aleph"), + alefsym: define-sym("aleph"), + aleph: define-sym("aleph"), + amalg: define-sym("product.co"), + arctg: of-sym(math.op("arctg")), + asymp: define-sym("≍"), + ch: of-sym(math.op("ch")), + circeq: define-sym("≗"), + colon: define-sym("colon"), + cth: of-sym(math.op("cth")), + dag: define-sym("dagger"), + Dagger: define-sym("dagger.double"), + ddag: define-sym("dagger.double"), + daleth: define-sym("ℸ"), + sharp: define-sym("♯"), + flat: define-sym("♭"), + i: define-sym("dotless.i"), + j: define-sym("dotless.j"), + imath: define-sym("dotless.i"), + jmath: define-sym("dotless.j"), + smallsmile: define-sym("⌣"), + smile: define-sym("⌣"), + ss: define-sym("ß"), + surd: define-sym("\√"), + tg: define-sym("tg"), + th: of-sym(math.op("th")), + weierp: define-sym("℘"), + wp: define-sym("℘"), + lbrace: define-sym("\\{"), + rbrace: define-sym("\\}"), + doteq: define-sym("≐"), + Vdash: define-sym("⊩"), + Doteq: define-sym("≑"), + Vvdash: define-sym("⊪"), + gnapprox: define-sym("⪊"), + ngeqslant: define-sym("gt.eq.not"), + precneqq: define-sym("prec.nequiv"), + gneq: define-sym("⪈"), + approxcolon: define-sym("approx:"), + approxcoloncolon: define-sym("approx::"), + backepsilon: define-sym("in.rev.small"), + barwedge: define-sym("⊼"), + beth: define-sym("beth"), + between: define-sym("≬"), + bigdot: define-sym("dot.circle.big"), + bigplus: define-sym("plus.circle.big"), + bigtimes: define-sym("times.circle.big"), + blacktriangle: define-sym("triangle.filled.t"), + blacktriangledown: define-sym("triangle.filled.b"), + blacktriangleleft: define-sym("triangle.filled.l"), + blacktriangleright: define-sym("triangle.filled.r"), + bowtie: define-sym("⋈"), + Bumpeq: define-sym("≎"), + bumpeq: define-sym("≏"), + centerdot: define-sym("dot.op"), + circledR: define-sym("®"), + circledS: define-sym("Ⓢ"), + clubs: define-sym("suit.club"), + cnums: define-sym("CC"), + Colonapprox: define-sym("::approx"), + colonapprox: define-sym(":approx"), + coloncolon: define-sym("::"), + coloncolonapprox: define-sym("::approx"), + coloncolonequals: define-sym("::="), + coloncolonminus: define-sym("::−"), + coloncolonsim: define-sym("::tilde.op"), + Coloneq: define-sym("::−"), + coloneq: define-sym(":−"), + colonequals: define-sym(":="), + Coloneqq: define-sym("::="), + coloneqq: define-sym(":="), + colonminus: define-sym(":−"), + Colonsim: define-sym("::tilde.op"), + colonsim: define-sym(":tilde.op"), + Complex: define-sym("CC"), + copyright: define-sym("copyright"), + ctg: define-sym("ctg"), + Darr: define-sym("arrow.b.double"), + dArr: define-sym("arrow.b.double"), + darr: define-sym("arrow.b"), + dashleftarrow: define-sym("arrow.l.dash"), + dashrightarrow: define-sym("arrow.r.dash"), + dbcolon: define-sym("::"), + degree: define-sym("degree"), + digamma: define-sym("ϝ"), + diagdown: define-sym("╲"), + diagup: define-sym("╱"), + doteqdot: define-sym("≑"), + doublebarwedge: define-sym("⩞"), + doublecap: define-sym("sect.double"), + doublecup: define-sym("union.double"), + eqcirc: define-sym("≖"), + Eqcolon: define-sym("−::"), + eqcolon: define-sym("dash.colon"), + Eqqcolon: define-sym("=::"), + eqqcolon: define-sym("=:"), + equalscolon: define-sym("=:"), + equalscoloncolon: define-sym("=::"), + eth: define-sym("ð"), + fallingdotseq: define-sym("≒"), + Finv: define-sym("Ⅎ"), + frown: define-sym("⌢"), + Game: define-sym("⅁"), + gggtr: define-sym(">>>"), + gimel: define-sym("gimel"), + Harr: define-sym("<=>"), + hArr: define-sym("<=>"), + harr: define-sym("<->"), + hearts: define-sym("♡"), + heartsuit: define-sym("♡"), + image: define-sym("Im"), + imageof: define-sym("⊷"), + infin: define-sym("infinity"), + intercal: define-sym("⊺"), + intop: define-sym("integral"), + lBrace: define-sym("⦃"), + ldotp: define-sym("."), + leadsto: define-sym("arrow.r.squiggly"), + lessapprox: define-sym("⪅"), + lesseqqgtr: define-sym("⪋"), + llcorner: define-sym("⌞"), + llless: define-sym("<<<"), + lnapprox: define-sym("⪉"), + gtrapprox: define-sym("⪆"), + lneq: define-sym("⪇"), + lrcorner: define-sym("⌟"), + lq: define-sym("quote.l.single"), + Lsh: define-sym("↰"), + maltese: define-sym("maltese"), + mathellipsis: define-sym("..."), + mathsterling: define-sym("pound"), + mho: define-sym("ohm.inv"), + minuscolon: define-sym("dash.colon"), + minuscoloncolon: define-sym("−::"), + minuso: define-sym("⦵"), + newline: define-sym("\\"), + nVDash: define-sym("⊯"), + nVdash: define-sym("⊮"), + OE: define-sym("Œ"), + oe: define-sym("œ"), + origof: define-sym("⊶"), + oslash: define-sym("⊘"), + owns: define-sym("in.rev"), + pitchfork: define-sym("⋔"), + ratio: define-sym(":"), + rBrace: define-sym("⦄"), + real: define-sym("Re"), + Reals: define-sym("RR"), + reals: define-sym("RR"), + restriction: define-sym("harpoon.tr"), + risingdotseq: define-sym("≓"), + rmoustache: define-sym("⎱"), + rq: define-sym("'"), + Rsh: define-sym("↱"), + simcolon: define-sym("tilde.op:"), + simcoloncolon: define-sym("tilde.op::"), + spades: define-sym("suit.spade"), + sqsubset: define-sym("subset.sq"), + sqsupset: define-sym("supset.sq"), + sub: define-sym("subset"), + sube: define-sym("subset.eq"), + subseteqq: define-sym("⫅"), + succneqq: define-sym("succ.nequiv"), + supe: define-sym("supset.eq"), + supseteqq: define-sym("⫆"), + supsetneqq: define-sym("⫌"), + textasciitilde: define-sym("~"), + textasciicircum: define-sym("\\^"), + textbackslash: define-sym("\\\\"), + textbar: define-sym("\\|"), + textbardbl: define-sym("‖"), + textbraceleft: define-sym("{"), + textbraceright: define-sym("}"), + textdagger: define-sym("dagger"), + textdaggerdbl: define-sym("dagger.double"), + textdegree: define-sym("degree"), + textdollarsign: define-sym("\\$"), + textellipsis: define-sym("..."), + textemdash: define-sym("---"), + textendash: define-sym("--"), + textgreater: define-sym("gt"), + textless: define-sym("lt"), + textquotedblleft: define-sym("quote.l.double"), + textquotedblright: define-sym("quote.r.double"), + textquoteleft: define-sym("quote.l.single"), + textquoteright: define-sym("quote.r.single"), + textregistered: define-sym("®"), + textsterling: define-sym("pound"), + textunderscore: define-sym("\\_"), + thetasym: define-sym("theta.alt"), + Uarr: define-sym("arrow.t.double"), + uArr: define-sym("arrow.t.double"), + uarr: define-sym("arrow.t"), + ulcorner: define-sym("⌜"), + unlhd: define-sym("lt.tri.eq"), + unrhd: define-sym("gt.tri.eq"), + urcorner: define-sym("⌝"), + varpropto: define-sym("prop"), + varsubsetneq: define-sym("subset.neq"), + varsubsetneqq: define-sym("⫋"), + varsupsetneq: define-sym("supset.neq"), + varsupsetneqq: define-sym("⫌"), + vcentcolon: define-sym(":"), + veebar: define-sym("⊻"), + yen: define-sym("yen"), + arcctg: of-sym(math.op("arcctg")), + begingroup: ignore-sym, + cosec: of-sym(math.op("cosec")), + cotg: of-sym(math.op("cotg")), + injlim: of-sym(math.op("inj\u{2009}lim", limits: true)), + mathclap: define-cmd(1, handle: it => box(width: 0pt, $it$)), + mathring: define-cmd(1, handle: it => math.circle(it)), + nobreak: ignore-sym, + noexpand: ignore-sym, + overgroup: define-cmd(1, handle: it => $accent(it, \u{0311})$), + undergroup: define-cmd(1, handle: it => $accent(it, \u{032e})$), + overleftharpoon: define-cmd(1, handle: it => $accent(it, \u{20d0})$), + overleftrightarrow: define-cmd(1, handle: it => $accent(it, \u{20e1})$), + overlinesegment: define-cmd(1, handle: it => $accent(it, \u{20e9})$), + overrightharpoon: define-cmd(1, handle: it => $accent(it, \u{20d1})$), + underbar: define-cmd(1, handle: it => $underline(it)$), + plim: of-sym(math.op("plim", limits: true)), + projlim: of-sym(math.op("proj\u{2009}lim", limits: true)), + raisebox: define-cmd(2, handle: (sp, it) => text(baseline: -eval(get-tex-str(sp)), it)), + sh: of-sym(math.op("sh")), + smallint: of-sym($inline(integral)$), + thickapprox: of-sym($bold(approx)$), + thicksim: of-sym($bold(tilde)$), + varDelta: of-sym($italic(Delta)$), + varGamma: of-sym($italic(Gamma)$), + varLambda: of-sym($italic(Lambda)$), + varOmega: of-sym($italic(Omega)$), + varPhi: of-sym($italic(Phi)$), + varPi: of-sym($italic(Pi)$), + varPsi: of-sym($italic(Psi)$), + varSigma: of-sym($italic(Sigma)$), + varTheta: of-sym($italic(Theta)$), + varUpsilon: of-sym($italic(Upsilon)$), + varXi: of-sym($italic(Xi)$), + xleftarrow: arrow-handle(math.arrow.l), + xrightarrow: arrow-handle(math.arrow.r), + xLeftarrow: arrow-handle(math.arrow.l.double), + xRightarrow: arrow-handle(math.arrow.r.double), + xleftrightarrow: arrow-handle(math.arrow.l.r), + xLeftrightarrow: arrow-handle(math.arrow.l.r.double), + xhookleftarrow: arrow-handle(math.arrow.l.hook), + xhookrightarrow: arrow-handle(math.arrow.r.hook), + xtwoheadleftarrow: arrow-handle(math.arrow.l.twohead), + xtwoheadrightarrow: arrow-handle(math.arrow.r.twohead), + xleftharpoonup: arrow-handle(math.harpoon.lt), + xrightharpoonup: arrow-handle(math.harpoon.rt), + xleftharpoondown: arrow-handle(math.harpoon.lb), + xrightharpoondown: arrow-handle(math.harpoon.rb), + xleftrightharpoons: arrow-handle(math.harpoons.ltrb), + xrightleftharpoons: arrow-handle(math.harpoons.rtlb), + xtofrom: arrow-handle(math.arrows.rl), + xmapsto: arrow-handle(math.arrow.r.bar), + xlongequal: arrow-handle(math.eq), + pmod: define-cmd(1, handle: it => $quad (mod thick it)$), + pod: define-cmd(1, handle: it => $quad (it)$), + "set": define-cmd(1, handle: it => $\{it\}$), + Set: define-cmd(1, handle: it => $lr(\{it\})$), + bra: define-cmd(1, handle: it => $angle.l it|$), + Bra: define-cmd(1, handle: it => $lr(angle.l it|)$), + ket: define-cmd(1, handle: it => $|it angle.r$), + Ket: define-cmd(1, handle: it => $lr(|it angle.r)$), + braket: define-cmd(1, handle: it => $angle.l it angle.r$), + Braket: define-cmd(1, handle: it => $lr(angle.l it angle.r)$), + fbox: define-cmd(1, handle: it => box(stroke: 0.5pt, $it$)), + hbox: define-cmd(1, handle: it => it), + // Matrices + matrix: matrix-handle(delim: none), + pmatrix: matrix-handle(delim: "("), + bmatrix: matrix-handle(delim: "["), + Bmatrix: matrix-handle(delim: "{"), + vmatrix: matrix-handle(delim: "|"), + Vmatrix: matrix-handle(delim: "||"), + smallmatrix: matrix-handle(handle: (..args) => math.inline(math.mat.with(delim: none, ..args))), + array: define-env(1, kind: "is-matrix", alias: "mitexarray", handle: (arg0: ("l",), ..args) => { + if args.pos().len() == 0 { + return + } + if type(arg0) != str { + if arg0.has("children") { + arg0 = arg0.children.filter(it => it != [ ] and it != [#math.zws]) + .map(it => it.text) + .filter(it => it == "l" or it == "c" or it == "r") + } else { + arg0 = (arg0.text,) + } + } + let matrix = args.pos().map(row => if type(row) == array { row } else { (row,) } ) + let n = matrix.len() + let m = calc.max(..matrix.map(row => row.len())) + matrix = matrix.map(row => row + (m - row.len()) * (none,)) + let array-at(arr, pos) = { + arr.at(calc.min(pos, arr.len() - 1)) + } + let align-map = ("l": left, "c": center, "r": right) + set align(align-map.at(array-at(arg0, 0))) + pad(y: 0.2em, grid( + columns: m, + column-gutter: 0.5em, + row-gutter: 0.5em, + ..matrix.flatten().map(it => $it$) + )) + }), + subarray: define-env(1, kind: "is-matrix", alias: "mitexarray"), + // Environments + aligned: define-env(none, kind: "is-math", alias: "aligned", handle: call-or-ignore(it => pad(y: 0.2em, block(math.op(math.display(it)))))), + alignedat: define-env(1, kind: "is-math", alias: "alignedat", handle: (arg0: none, it) => pad(y: 0.2em, block(math.op(it)))), + align: define-env(none, kind: "is-math", alias: "aligned"), + "align*": define-env(none, kind: "is-math", alias: "aligned"), + equation: define-env(none, kind: "is-math", alias: "aligned"), + "equation*": define-env(none, kind: "is-math", alias: "aligned"), + split: define-env(none, kind: "is-math", alias: "aligned"), + gather: define-env(none, kind: "is-math", alias: "aligned"), + gathered: define-env(none, kind: "is-math", alias: "aligned"), + cases: define-env(none, kind: "is-cases", alias: "cases"), + rcases: define-env(none, kind: "is-cases", alias: "rcases", handle: math.cases.with(reverse: true)), + // Specials + notag: ignore-sym, + relax: ignore-sym, + cr: ignore-sym, + expandafter: ignore-sym, + hskip: ignore-sym, + mskip: ignore-sym, + kern: ignore-sym, + mkern: ignore-sym, + mathstrut: ignore-sym, + nonumber: ignore-sym, + KaTeX: of-sym(math.upright($K A T E X$)), + LaTeX: of-sym(math.upright($L A T E X$)), + TeX: of-sym(math.upright($T E X$)), + middle: define-cmd(1, handle: it => math.mid(it)), + operatorname: define-cmd(1, handle: it => math.op(math.upright(it))), + operatornamewithlimits: define-cmd(1, alias: "operatornamewithlimits", handle: operatornamewithlimits), + "operatorname*": define-cmd(1, alias: "operatornamewithlimits", handle: operatornamewithlimits), + vspace: define-cmd(1, handle: it => v(eval(get-tex-str(it)))), + hspace: define-cmd(1, handle: it => h(eval(get-tex-str(it)))), + text: define-cmd(1, alias: "#textmath", handle: it => it), + textmd: define-cmd(1, alias: "#textmd", handle: it => it), + textnormal: define-cmd(1, alias: "#textnormal", handle: it => it), + textbf: define-cmd(1, alias: "#textbf", handle: math.bold), + textit: define-cmd(1, alias: "#textit", handle: math.italic), + textrm: define-cmd(1, alias: "#textrm", handle: math.upright), + textup: define-cmd(1, alias: "#textup", handle: math.upright), + textsf: define-cmd(1, alias: "#textsf", handle: math.sans), + texttt: define-cmd(1, alias: "#texttt", handle: math.mono), + over: define-infix-cmd("frac"), + atop: define-infix-cmd("atop", handle: (a, b) => $mat(delim: #none, #a; #b)$), + choose: define-infix-cmd("binom", handle: math.binom), + brace: define-infix-cmd("brace", handle: (n, k) => $mat(delim: "{", #n;; #k)$), + brack: define-infix-cmd("brack", handle: (n, k) => $mat(delim: "[", #n;; #k)$), + sqrt: define-glob-cmd("{,b}t", "mitexsqrt", handle: (..args) => { + if args.pos().len() == 1 { + $sqrt(#args.pos().at(0))$ + } else if args.pos().len() == 2 { + $root( + #args.pos().at(0).children.filter(it => it != [\[] and it != [\]]).sum(), + #args.pos().at(1) + )$ + } else { + panic("unexpected args in sqrt") + } + }), + // todo: macros + def: ignore-sym, + newcommand: ignore-sym, + "newcommand*": ignore-sym, + renewcommand: ignore-sym, + "renewcommand*": ignore-sym, + DeclareRobustCommand: ignore-sym, + "DeclareRobustCommand*": ignore-sym, + DeclareTextCommand: ignore-sym, + DeclareTextCommandDefault: ignore-sym, + ProvideTextCommand: ignore-sym, + ProvideTextCommandDefault: ignore-sym, + providecommand: ignore-sym, + "providecommand*": ignore-sym, + newenvironment: ignore-sym, + "newenvironment*": ignore-sym, + renewenvironment: ignore-sym, + "renewenvironment*": ignore-sym, + AtEndOfClass: ignore-sym, + AtEndOfPackage: ignore-sym, + AtBeginDocument: ignore-sym, + AtEndDocument: ignore-sym, + "@ifstar": ignore-sym, + "if": ignore-sym, + ifdim: ignore-sym, + iffalse: ignore-sym, + ifnum: ignore-sym, + ifodd: ignore-sym, + iftrue: ignore-sym, + ifx: ignore-sym, + DeclareOption: ignore-sym, + "DeclareOption*": ignore-sym, + CurrentOption: ignore-sym, + ProcessOptions: ignore-sym, + ExecuteOptions: ignore-sym, + RequirePackage: ignore-sym, + RequirePackageWithOptions: ignore-sym, + documentclass: ignore-sym, + PassOptionsToClass: ignore-sym, + PassOptionsToPackage: ignore-sym, + IfFileExists: ignore-sym, + InputIfFileExists: ignore-sym, + ProvidesFile: ignore-sym, + ignorespaces: ignore-sym, + ignorespacesafterend: ignore-sym, + ifvoid: ignore-sym, + ifinner: ignore-sym, + ifhbox: ignore-sym, + ifvbox: ignore-sym, + ifhmode: ignore-sym, + ifmmode: ignore-sym, + ifvmode: ignore-sym, + CheckCommand: ignore-sym, + "CheckCommand*": ignore-sym, + newcounter: ignore-sym, + newlength: ignore-sym, + newsavebox: ignore-sym, + newtheorem: ignore-sym, + newfont: ignore-sym, + ProvidesClass: ignore-sym, + LoadClass: ignore-sym, + LoadClassWithOptions: ignore-sym, +)) + +// export: include package name, spec and scope +#let package = (name: "latex-std", spec: (commands: spec), scope: scope) diff --git a/packages/preview/mitex/0.2.6/specs/mod.typ b/packages/preview/mitex/0.2.6/specs/mod.typ new file mode 100644 index 0000000000..6b5d957904 --- /dev/null +++ b/packages/preview/mitex/0.2.6/specs/mod.typ @@ -0,0 +1,12 @@ + +#import "prelude.typ": * +#import "latex/standard.typ": package as latex-std + +// 1. import all the packages and form a mitex-scope for mitex to use +#let packages = (latex-std,) +#let mitex-scope = packages.map(pkg => pkg.scope).sum() + +// 2. export all packages with specs by metadata and label, +// mitex-cli can fetch them by +// `typst query --root . ./packages/mitex/specs/mod.typ ""` +#metadata(packages) diff --git a/packages/preview/mitex/0.2.6/specs/prelude.typ b/packages/preview/mitex/0.2.6/specs/prelude.typ new file mode 100644 index 0000000000..70789f0f2b --- /dev/null +++ b/packages/preview/mitex/0.2.6/specs/prelude.typ @@ -0,0 +1,230 @@ +/// 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`. +/// - sym (content): The specific content, as the value of alias in mitex-scope. +/// For example, there is no direct alias for \negthinspace symbol in typst, +/// but we can add `h(-(3/18) * 1em)` ourselves +/// +/// 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 + }, + ) +} + +/// 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. +/// - handle (function): The handler function, as the value of alias in mitex-scope. +/// It receives a content argument as all greedy matches to the content +/// For example, we define `mitexdisplay` to `math.display` +/// +/// 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 + }, + ) +} + +/// 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. +/// - handle (function): The handler function, as the value of alias in mitex-scope. +/// It receives two content arguments, as (prev, after) arguments. +/// For example, we define `\over` to `frac: (num, den) => $(num)/(den)$` +/// +/// 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 + }, + ) +} + +/// Define a glob (Global Wildcard) match command with a specified pattern for matching args +/// Kind of item to match: +/// - Bracket/b: [] +/// - Parenthesis/p: () +/// - Term/t: any rest of terms, typically {} or single char +/// +/// Arguments: +/// - pat (pattern): The pattern for glob-cmd +/// For example, `{,b}t` for `\sqrt` to support `\sqrt{2}` and `\sqrt[3]{2}` +/// - s (str): Alias command for typst handler. +/// For example, alias `\sqrt` to typst's `mitexsqrt`, as the key in mitex-scope. +/// - handle (function): The handler function, as the value of alias in mitex-scope. +/// It receives variable length arguments, for example `(2,)` or `([3], 2)` for sqrt. +/// Therefore you need to use `(.. arg) = > {..}` to receive them. +/// +/// 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 + }, + ) +} + +/// 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. +/// For example, alias `\frac` to typst's `frac`, as the key in mitex-scope. +/// - handle (function): The handler function, as the value of alias in mitex-scope. +/// It receives fixed number of arguments, for example `frac(1, 2)` for `\frac{1}{2}`. +/// +/// 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 + }, + ) +} + +/// 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. +/// For example, alias `\begin{alignedat}{2}` to typst's `alignedat`, +/// and alias `\begin{aligned}` to typst's `aligned`, as the key in mitex-scope. +/// - kind (str): environment kind, it could be "is-math", "is-cases", "is-matrix", +/// "is-itemize", "is-enumerate" +/// - handle (function): The handler function, as the value of alias in mitex-scope. +/// It receives fixed number of named arguments as environment options, +/// for example `alignedat(arg0: ..)` or `alignedat(arg0: .., arg1: ..)`. +/// And it receives variable length arguments as environment body, +/// Therefore you need to use `(.. arg) = > {..}` to receive them. +/// +/// 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) + } else { + (kind: "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 + }, + ) +} + +/// 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 +/// +/// Return: A symbol spec and a scope item +#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) + +/// 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) + +/// Receives a list of definitions composed of the above functions, and processes them to return a dictionary containing spec and scope. +#let process-spec(definitions) = { + let spec = (:) + let scope = (:) + for (key, value) in definitions.pairs() { + let spec-item = value.at(0) + let scope-item = value.at(1) + spec.insert(key, spec-item) + if scope-item != none { + if "alias" in scope-item and type(scope-item.alias) == str { + let key = if scope-item.alias.starts-with("#") { + scope-item.alias.slice(1) + } else { + scope-item.alias + } + scope.insert(key, scope-item.handle) + } else { + scope.insert(key, scope-item.handle) + } + } + } + (spec: spec, scope: scope) +} diff --git a/packages/preview/mitex/0.2.6/typst.toml b/packages/preview/mitex/0.2.6/typst.toml new file mode 100644 index 0000000000..6d9365dc02 --- /dev/null +++ b/packages/preview/mitex/0.2.6/typst.toml @@ -0,0 +1,12 @@ +[package] +name = "mitex" +version = "0.2.6" +entrypoint = "lib.typ" +authors = ["Myriad-Dreamin", "OrangeX4", "Enter-tainer"] +license = "Apache-2.0" +description = "LaTeX support for Typst, powered by Rust and WASM." + +repository = "https://github.com/mitex-rs/mitex" +categories = ["utility"] +keywords = ["wasm", "rust", "LaTeX", "equation"] +exclude = ["examples"]