Hacker News new | ask | show | jobs
by kisonecat 1823 days ago
I implemented a TeX engine in WebAssembly so you really can run TeX in the browser. You can see a demo of this at https://tex.rossprogram.org/ and at https://github.com/kisonecat/web2js you can find a Pascal compiler that targets WebAssembly which can compile Knuth's TeX. Interesting primitives like \directjs are also implemented, so you can execute javascript from inside TeX. The rendering is handled with https://github.com/kisonecat/dvi2html for which I finally fixed some font problems.

To make it relatively fast, the TeX engine gets snapshotted and shipped to the browser with much of TeXlive already loaded. So even things like TikZ work reasonably well. There is of course a lot more to do! The plan is to convert ximera.osu.edu to this new backend by the fall.

5 comments

>> I implemented a TeX engine in WebAssembly so you really can run TeX in the browser.

Kinda misses the point of the blog post. The idea is to do that server side, preferably only once.

You can use the same code for server-side rendering: with dvi2html, you can run TeX on a .tex file to get .dvi and then produce a static html file. This renders not just the math but also the text using TeX's layout engine, which is often not what people want. Running it in the client means the TeX knows the width of the view.
>> Running it in the client means the TeX knows the width of the view.

+1 for running it server side. But knowing the width of the view was never supposed to be a thing with HTML and the web. That's what the layout engine in the browser is for. Also, knowing those metrics is a bit or two of fingerprinting information that the server should never be made aware of in the first place.

This is wonderful!

I'm a wasm neophyte -- even the set up seems complex to me. Do you have any recommendations for where to dive in?

I started by playing with emscripten which manages some of the initial complexity.
> The plan is to convert ximera.osu.edu to this new backend by the fall.

Have you verified that this will not be an accessibility regression for blind users?

Unfortunately, the current platform we're using isn't very accessible: the \answer{...} blanks aren't converted to speech and don't navigate correctly. The new backend parses some TeX anyway to an AST, and the idea is that we could use that to produce alt text for the math. There's certainly a lot to do.
Sorry to sidetrack, but I'm also interested in implementing a Tex engine (in another language though). Any suggestions on how to learn how an engine works?
You could also check the implementation in Rust - fully "oxidized" fork[1] of Tectonic.

[1] https://github.com/crlf0710/tectonic/

TeX's source code is available in a "literate" format, so you can download tex.web, run weave tex.web, and then pdftex tex.tex. This will produce a large book with the TeX source code. A challenge is that the resulting TeX engine won't support LaTeX3, so you'll need to use "changefiles" to modify tex.web to add epsilon-TeX and other updates.
Hello! I'm an osu student that has taken the calc sequence and very much appreciates the effort. I'll be watching the repo for sure