Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

m-jaxon:v0.1.1 #350

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/preview/m-jaxon/0.1.1/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 mgt

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

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

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
429 changes: 429 additions & 0 deletions packages/preview/m-jaxon/0.1.1/NOTICE

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions packages/preview/m-jaxon/0.1.1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# [M-Jaxon](https://github.com/Enter-tainer/m-jaxon)

Render LaTeX equation in typst using MathJax.

**Note:** This package is made for fun and to demonstrate the capability of typst plugins.
And it is **slow**. To actually convert LaTeX equations to typst ones, you should use **pandoc** or **texmath**.

![](mj.svg)


````typ
#import "./typst-package/lib.typ" as m-jaxon
// Uncomment the following line to use the m-jaxon from the official package registry
// #import "@preview/m-jaxon:0.1.1"

= M-Jaxon

Typst, now with *MathJax*.

The equation of mass-energy equivalence is often written as $E=m c^2$ in modern physics.

But we can also write it using M-Jaxon as: #m-jaxon.render("E = mc^2", inline: true)

````

## Limitations

- The baseline of the inline equation still looks a bit off.


## Documentation

### `render`

Render a LaTeX equation string to an svg image. Depending on the `inline` argument, the image will be rendered as an inline image or a block image.

#### Arguments

* `src`: `str` or `raw` block - The LaTeX equation string
* `inline`: `bool` - Whether to render the image as an inline image or a block image

#### Returns

The image, of type `content`
1 change: 1 addition & 0 deletions packages/preview/m-jaxon/0.1.1/lib.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#import "./mj.typ": render
50 changes: 50 additions & 0 deletions packages/preview/m-jaxon/0.1.1/mj.js

Large diffs are not rendered by default.

1,530 changes: 1,530 additions & 0 deletions packages/preview/m-jaxon/0.1.1/mj.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions packages/preview/m-jaxon/0.1.1/mj.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#import "@preview/jogs:0.2.2": compile-js, call-js-function

#let mj-src = read("./mj.js")
#let mj-bytecode = compile-js(mj-src)

#let natural-image(..args) = style(styles => {
let (width, height) = measure(image.decode(..args), styles)
image.decode(..args, width: width, height: height)
})

#let get-text(src) = {
if type(src) == str {
src
} else if type(src) == content {
src.text
}
}

#let render(src, inline: false) = style(styles => {
let src = get-text(src)
let (width, height) = measure(h(1em), styles)
let size = width
let passed-size = size / 1.21 // katex is 1.21x larger, I dont find things about mathjax but it seems to be the same
// https://katex.org/docs/font
let result = call-js-function(mj-bytecode, "mj", src, inline, passed-size.pt())
let img = natural-image(result.svg, format: "svg")
let ex = result.vertical_align
if inline {
box(move(box(img), dy: -ex * 0.5em))
} else {
align(center, img)
}
})
12 changes: 12 additions & 0 deletions packages/preview/m-jaxon/0.1.1/typst.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "m-jaxon"
version = "0.1.1"
entrypoint = "lib.typ"
authors = ["Wenzhuo Liu"]
license = "MIT"
description = "Render LaTeX equation in typst using MathJax."

homepage = "https://github.com/Enter-tainer/m-jaxon"
repository = "https://github.com/Enter-tainer/m-jaxon"
keywords = ["js", "javascript", "mathjax", "LaTeX", "equation"]
exclude = ["mj.svg"]