Coder Social home page Coder Social logo

mitex-rs / mitex Goto Github PK

View Code? Open in Web Editor NEW
243.0 3.0 8.0 1.03 MB

LaTeX support for Typst, powered by Rust and WASM. https://mitex-rs.github.io/mitex/

Home Page: https://mitex-rs.github.io/mitex/tools/underleaf.html

License: Apache License 2.0

Shell 0.05% Rust 66.67% Typst 9.66% PowerShell 0.17% HTML 0.10% TypeScript 21.46% CSS 1.03% JavaScript 0.26% TeX 0.59%
latex math rust typst typst-package wasm tex

mitex's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mitex's Issues

Unexpected trailing space after curly group

When curly group is used as an argument, a trailing space is read after the argument.

assert_debug_snapshot!(convert_text(r#"a \textbf{strong} text"#), @r###"
Ok(
"a #strong[strong ]text",
)
"###);

fn curly_group() {
assert_debug_snapshot!(parse(r#"a \textbf{strong} text"#), @r###"
root
|text(word'("a"),space'(" "))
|cmd
||cmd-name("\\textbf")
||args
|||curly
||||lbrace'("{")
||||text(word'("strong"))
||||rbrace'("}")
||||space'(" ")
|text(word'("text"))
"###);
}

Nesting math and normal text in LaTeX is NOT correctly rendered

#import "@preview/mitex:0.2.1": *
#set math.equation(numbering: "(1)", supplement: [#text(blue)[*Eq.*]])

#mitext(`
    \begin{align}
      \text{Stokes formula for Manifold $\mathcal M$}\quad \int_{\mathcal M}\mathrm{d}\omega&=\int_{\partial\mathcal M}\omega
    \end{align}
`)

Resulting
2024-02-15_16-34
where the nested math \mathcal M$ is not correctly rendered.

While

#import "@preview/mitex:0.2.1": *
#set math.equation(numbering: "(1)", supplement: [#text(blue)[*Eq.*]])

#mitext(`
    \begin{align}
      \text{Stokes formula for Manifold}~$\mathcal M$\quad \int_{\mathcal M}\mathrm{d}\omega&=\int_{\partial\mathcal M}\omega
    \end{align}
`)

is OK

Expand spacing for `\boxed`

In latex, something like $$\boxed{\frac12}$$ gives the below.
image

When just using typst's box, $#box($1/2$, stroke: 0.5pt)$ gives the below.
image

Ideally, the output would like this #box($1/2$, stroke: 0.5pt, inset: 6pt).
image

It seems that 6pt's of inset is enough to avoid clipping into contents: #box([abcd $1/2$ $1/2/2/2/2/2/2/2/2/2/2/2/(2/2)$], stroke: 0.5pt, inset: 6pt).
image

Does it make sense to change how \boxed is converted into typst to match the latex output, or is there a good workaround?

My thoughts for a workaround was to use an iftypst block to replace \boxed with my own call to #box, but I think that would then require another conversion inside of the #box argument from latex to typst.

Mitex watch

It would be useful if the CLI had a watch command to automatically update the .typ file when the .tex file changes.

Provide a native CLI program to convert LaTeX source into typst source.

If I get it correctly, for the time being, mitex only works as a WASM module and thus only works on the typst web app, but not for the CLI.

It would be better to provide a CLI program that runs like:

mitex -i foo.tex -o foo.typ

Then we can deal with the typst source with typst-cli.

Another benefit of this is that we can easily build a testset with a bunch of TeX sources, which will largely assure the quality and stability of mitex.

`lceil` and `rceil` have extra spacing.

It appears that theres extra spaces in the definitions for lceil and rceil. I'm not sure why they're there, and there may be a good reason for it. However, it causes the results in underleaf as shown in the images below (having a space after the first $ seems to make equations centered).

lceil: define-sym(" โŒˆ "),
rceil: define-sym("โŒ‰ "),
lfloor: define-sym("โŒŠ "),

image image

In overleaf, there is no middle alignment, and the results show up inline.
image

Architecture.md

mitex's design of spliting spec and parser/lexer is very clever and impressive. It however make it harder for people to understand what's happening. It would be better we can have a doc explaining what will happen when you type #mi(...)

Commands to add for OI Wiki test

Scanned by script

  • \notag
  • \brack
  • \triangle
  • \impliedby
  • \LaTeX
  • \space
  • \lnot
  • \mathop
  • \vert
  • \middle
  • \lvert
  • \rvert
  • \lVert
  • \Bbb
  • \implies
  • \boxed
  • \checkmark
  • \substack
  • \dotsb
  • \dbinom
  • \*
  • \wedge
  • \subsetneqq
  • \emptyset
  • \pod
  • \lor
  • \cr
  • \tag
  • \varphi*
  • \brace
  • \begin{alignedat}

Feature Requst: Support Citation `\cite{...}` in `#mitext`

Right now the reference is not parsed in mitext. But I think it can be easily supported by breaking the macro by the parser if \cite is encountered.

For example, if I have a bib file containing one item called anderson1972more,

#import "@preview/mitex:0.2.1": *
#mitext(`This is reference test \cite{anderson1972more}. This is the text after.`)

I would expect it to be parsed into

#import "@preview/mitex:0.2.1": *
#mitext(`This is reference test `) @anderson1972 #mitext('. This is the text after.`)

where we can simply take advantage of the standard support of bib-file citation.

Feature Request for `\footnote{...}` Support

Nowadays typst already support #footnote macro, but #mixtext still cannot parse \footnote{...} yet.

I wonder if it can be easily supported by splitting #mitext with the parser if \footnote is encountered, like following:

#import "@preview/mitex:0.2.1": *
#mitext(`
Now because only non-trivial braiding\footnote{Otherwise the relabeling among these overlapping pairs takes no effects.} processes are considered...
`)

be parsed by

#import "@preview/mitex:0.2.1": *
#mitext(`
Now because only non-trivial braiding`)#footnote[Otherwise the relabeling among these overlapping pairs takes no effects.]#mitext(` processes are considered...
`)

Problems reproducing example

I copied the example from the README and pasted it to the web app. The code, which is below.

#import "@preview/mitex:0.1.0": *

#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
`)

After trying to compile it I got "Plugin errored with error: error unknown command \f". Am I doing something wrong or is the library in the preview repository doesn't support the feature yet?

Load resources from inside mitex

Is it possible to load an image in a iftypst block?

I've been able to use typst.ts with mapShadow in the browser to render images. However, when I try to put this image in a iftypst block, I get a "failed to load file (access denied)", trace: [Call(Some("mimath"))], hints: ["cannot read file outside of project root" error.

It seems to me that the web demo is able to import both a bibliography and another template file. However, I cannot figure out where inpackages/mitex-web/src this virtual file system is handled. It appears that there is a VFS for the sidebar directory view, but I cannot tell how this integrates with mitex. Is there some documentation on this?

feature request to support raw macro or \texttt{} translation in mitex

For example,

#import "@preview/mitex:0.2.1": *
#mitext(`
        The torus geometry paratermized with fluxes $phi_1,phi_2$ can be described with the struct:
        ```julia
        struct Torus_Sample
            L1::Float64
            ฯ„::ComplexF64
            Ns::Int64
            mag_trans_units::Vector{ComplexF64}
            phase_twists_over_2ฯ€::Vector{Float64} # fluxes (ฯ•1,ฯ•2)
        end
        ```
        where we introduce the *minimal* quantized intervals of the sample along two oblique directions $(\delta L_1,\delta L_2)=(\frac{L_1}{N_s},\frac{L_1\tau}{N_s})$, such that each thin rectangular is pierced with just *one* quantum flux
        \begin{equation*}
            \delta L_1\times L_2 = 2\pi\ell^2,\quad \delta L_2\times L_1 = 2\pi\ell^2.
        \end{equation*}
`)

does not render at all.

I encounter this problem because I am transferring my academical notes from markdown (very long and hundreds of huge equations, so rendering slow in markdown) to typst (which is really fast).

Maybe #raw() macro support can be added for pair of `...` and ```...```, which is not the keyword in latex so should work out without any ambiguities.

Package support

Mitex cli should be able to support ctan, as most latex documents use a lot of packages. The initial possible implementation could include just outputting packages.typ file alongside the converted user file.

Add detailed Installation Instructions to README

I am genuinely impressed with potential of this project and excited about the possibilities it offers. However, I've encountered a bit of a roadblock. As a new user, I'm unsure about the installation process, as the current README file doesn't include an installation section. This has made it challenging for me to get started with the project.

Could you please consider adding an installation guide to the README?

Percent in text mode

\% is converted into percent, but this is correct only if in the equation environment.

Plan to convert users' TeX document into Typst code

Though I prefer to writing documents in pure typst, I need to write several LaTeX documents for formal publications. Typst has delightful future but we have to admit that it cannot be used for writing that documents at least in next few months.

Now, I still want to fully make use of typst, So I want to have a MiTeX which converts at least main body part of TeX code to typst code for me. So that I can preview a LaTeX document locally without help of TeXLive or TeXStudio. I also want to have capability to embed typst content, e.g. cetz pictures into the document, so that I don't have to use tikz.

In conclusion, I feel it useful for partially supporting to convert entire TeX document into typst. With that you can:

  • preview a TeX document instantly, without locally installed TeXLive or TeXStudio.
  • keep document in TeX format (which is still needed for formal publication).
  • export document by typst exporters, then we can share or distribute them informally.

Convert entire document into typst sounds crazy, but we still have a chance. We can support them with some limitation, since we are converting a user document.

  • Less TeX Styles: We can show TeX content by typst rules. Therefore, we can throw most styles of a TeX document away, so that we won't touch typesetting things during convertion.
  • Less TeX Macros: We can perform calcuation via typst scripting. Therefore, it is also not quite necessary to have a fully supported macros.

Idea follows.

  • support text mode.
  • convert commonly used text commands.
  • partially support context-insensitive \input{chapterN} with help of mitex(read('chapterN.tex')).
  • add conditional macros, \iftypst, so that I can make hacks in my TeX document.
    • \iftypst expands only if the document is compiled by typst.
  • add limited macro support, e.g. only with context-insensitive \newcommand.
    • so that I can replace some harsh commands with help of binding to typst functions

Not quite hard, no much efforts, but looks perfect for typst users writing TeX documents!


Postscript here. I don't mention more complex things, such as tables, since I plan to do some hacks on them. Personally, I may read and process data by typst scripting, then I either

  • render data by tablex.
  • or export them via typst query, and use TeX's tables showing data processed by typst.

Convert box commands

No plan, but some direction:

  • find a way of ignoring all box related commands, so that it just compiles in typst instead of really layout something.
  • implement kern commands
  • implement glue commands
  • implement paragraph commands

Materials and ideas

Initial Idea is following.

  • The boxes are allocated by converter.
  • We may have infinite virtual box registers, since holding limit of 256 is just less meaningful but depends on implementation.
  • We won't calculate any size of boxes, but converting them with typst scripting.
    That means, any failed conversions will just make MiTeX ignore layout request from TeX Code.
  • For code converting layout, measure for box and box conditions:
    #locate(loc => layout(size => {
      // converted code here
    }))

Examples

Example 1 (paragraph):

\everypar{\setbox\parbox=
\vbox\bgroup\everypar{}%
\def\par{\endgraf\HandleLines
\egroup\box\parbox}}
\def\HandleLines{ ... \lastbox ... }

Example 2 (box):

\newbox\linebox \newbox\snapbox
\def\eatlines{
\setbox\linebox\lastbox % check the last line
\ifvoid\linebox
\else % if it's not empty
\unskip\unpenalty % take whatever is
{\eatlines} % above it;
% collapse the line
\setbox\snapbox\hbox{\unhcopy\linebox}
% depending on the difference
\ifdim\wd\snapbox<.98\wd\linebox
\box\snapbox % take the one or the other,
\else \box\linebox \fi
\fi}

More helpful errors

I have a large, complicated document that I didn't expect would compile, but I was hoping MiTeX would tell me what it couldn't handle. Instead I was met with this error

image

It would be great if it would give some details :)

Correct `\mathbf` Rendering to Regular Fonts

Hi, I found that that both \mathbf . and \mathbf{...} cannot be correctly rendered in desired form as in LaTeX (bold AND regular). For example,

#import "@preview/mitex:0.2.2": *

#mitext(`Reciprocal vector is $\mathbf G$ or $\mathbf{G}$`)

gives
2024-03-20_14-13

But in LaTeX, it should be
2024-03-20_14-15

NOTE: \bm (as well as \boldsymbol(please also add support for this command)) behaves different from \mathbf!


And here is one more tricky thing:

See https://tex.stackexchange.com/questions/294561/using-textbf-vs-mathbf-in-math-mode. Precisely, \mathbf{...} and \textbf{...} are different in LaTeX, in that \mathbf{...} will remove ALL blank spaces within its arguments. So probably a more correct rendering is to parse, for example, for the LaTeX code:

$\sum_{i\in\mathcal I}\mathbf{Op With Space}=\text{blablabla}$

2024-03-20_15-10

as the Typst code:

$ sum_(i in cal(I))bold(#"Op With Spaces".replace(" ", ""))="blablabla" $ 

2024-03-20_15-18

I think this clearly demonstrate how to fix the \mathbf rendering problem : )

Thanks for your help!

Text mode support (stage 2)

Previous issue: #35

  • update README.md to clarify that for #130:

    mitex is well tested by fixtures extracted from oiwiki, which always use mimath (math mode), while you are using mitext (text mode), which is not well tested.

    Considering that, the introduction in README.md are misleading, since it wrongly tells mitex in text mode is also well tested. The introduction was correct as I wrote it when we only implemented math mode. However, once after introducing text mode, there should be quite misleading.

  • handle several typical and commonly used environments such as document, figure, algorithm and description.
    • don't take too much design first, but consider them concretely.
  • don't panic: randomly pick 10 papers from different research area, and convert them without panic.
  • do stub: for those environments that are hard to implement, e.g. algorithm and tikz, we should parse them correctly and put warning boxes in place.
  • beautify: correct visual effect, implement simple templates for them.
  • ๐ŸŽ‰

More commands that need to be supported

  • \AA
  • \Box
  • \C
  • \L
  • \LARGE
  • \N
  • \O
  • \P
  • \Q
  • \R
  • \S
  • \Vert
  • \Z
  • \ae
  • \aleph
  • \amalg
  • \arctg
  • \arraystretch
  • \arrowvert
  • \asymp
  • \atop
  • \atopwithdelims
  • \bbox
  • \begin
  • \bra
  • \buildrel
  • \cdotp
  • \ch
  • \check
  • \circeq
  • \cline
  • \colon
  • \cth
  • \dag
  • \ddag
  • \def
  • \diamondsuit
  • \em
  • \emph
  • \end
  • \eqslantgtr
  • \eqslantless
  • \expandafter
  • \fbox
  • \flat
  • \hbox
  • \hfil
  • \hfill
  • \hline
  • \hskip
  • \i
  • \idotsint
  • \includegraphics
  • \itshape
  • \j
  • \kern
  • \ket
  • \l
  • \left
  • \lgroup
  • \llap
  • \lower
  • \lq
  • \mathnormal
  • \mathstrut
  • \mbox
  • \mit
  • \mkern
  • \mskip
  • \multicolumn
  • \natural
  • \nonumber
  • \o
  • \oslash
  • \overwithdelims
  • \preceq
  • \r
  • \raise
  • \raisebox
  • \ref
  • \relax
  • \rgroup
  • \right
  • \rlap
  • \root
  • \rule
  • \sc
  • \setlength
  • \sharp
  • \shortparallel
  • \skew
  • \sl
  • \smallint
  • \smash
  • \smile
  • \ss
  • \strut
  • \succeq
  • \surd
  • \textcircled
  • \textup
  • \tg
  • \th
  • \u
  • \vcenter
  • \verb
  • \vline
  • \widehat
  • \widetilde
  • \wp

Correct label position for LaTeX align math environment

The minimal example for align environment is:

#import "@preview/mitex:0.2.1": *
#set math.equation(numbering: "(1)", supplement: [#text(blue)[*Eq.*]])

#mitext(`
    This is \emph{math} \textbf{equation} test for \eqref{eq:1}.
    \begin{align}
      \text{Stokes formula}\quad \int_{\mathcal M}\mathrm{d}\omega&=\int_{\partial\mathcal M}\omega\nonumber\\
      \textbf{Jacobi-}$\vartheta$ \textbf{Function}\quad f(z)&=e^{ik\cdot z}\prod_{\nu=1}^{N_s}\vartheta\left(2\pi\frac{z-z_\nu}{L_1}\middle|\tau\right).\label{eq:1}
    \end{align}
`)

What I got is
2024-02-15_16-28

Here the label is NOT correctly placed at the end of the second line, even if I use the command \nonumber for the first line.

Parser Inconsistency for Several Math Expressions

Hi, after several days trial of mitex, I found several annoying inconsistencies, even for standard math notation:

  1. In math, we use square bracket [ and parenthesis ( to represent the close and open set. But when I try to convert my notes including something like $[0,1,\cdots, N_s)$ blablabla, I got serious parse error (collapse to one line)

It seems because the parser found some unclosed/unpaired bracket. If so, that is really stupid to me (we should be free to use any symbol in math mode).

  1. The vertical line in latex is somewhat parse to wrong symbol, for example, Bloch states$|\boldsymbol k\rangle$ is wrongly parsed by mitex: I got extra spaces between k and the vertical line. But if I try to make it converting from $\ket{\boldsymbol k}$ by explicitly telling mitex what the composite symbol is (here is the ket state), then the conversion is OK.

In latex at least $|\boldsymbol k\rangle$ and $\ket{\boldsymbol k}$ looks the same, without knowing the meaning for the composite symbol |...\rangle, and there are indeed some situation when $\bra{...}$ or $\ket{...}$ or $\braket{...}$ cannot handle, that is why most people still control the size of vertical line | and the size of angle bracket \langle and \rangle by hand.

`textcolor` crashed in 0.2.0

Example:

#import "@preview/mitex:0.2.0": *
#mitex(`\textcolor{orange}{test}`)

caused this problem:

Unknown variable: tex

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.