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

Markdown Extension to include LaTeX support #6

Closed
ArvinSKushwaha opened this issue Oct 18, 2023 · 17 comments
Closed

Markdown Extension to include LaTeX support #6

ArvinSKushwaha opened this issue Oct 18, 2023 · 17 comments
Labels
enhancement New feature or request

Comments

@ArvinSKushwaha
Copy link

ArvinSKushwaha commented Oct 18, 2023

Would it be possible to introduce some mathematical expression support? I have seen unicode rendering done in the past of math expressions, but perhaps even some iterm/sexel magic could be done to embed equations.

Thank you for all your work on this project, by the way! It's quite phenomenal!

@mfontanini
Copy link
Owner

I will look into this! If there's a crate/tool that can turn latex expressions into images, then it should be fairly straightforward to add here. We'd just need a step that takes latex code blocks, runs them through this and emits an operation to render an image.

@mfontanini mfontanini added the enhancement New feature or request label Oct 18, 2023
@ArvinSKushwaha
Copy link
Author

I think if you're willing to have a temporary folder instantiated, it could be very cool and worthwhile to have the ability to summon the native PDFLaTeX renderer and pass the generated document through ImageMagick to get a fully rendered LaTeX image, especially for the scientists who I feel like are pretty likely to be attracted to a utility like this (like in my case).

However, for nice and compact solution, the https://docs.rs/mathjax/latest/mathjax/ crate seems to closely approximates what is already done by GitHub-Flavored Markdown, and you could leave the responsibility of rendering more complex LaTeX expressions to people to handle on their own.

I'm personally biased toward solution 1 (or a mix of the two) but mostly because it would be really frickin awesome, but otherwise either sounds pretty chill and and awesome enhancement to have!

@mfontanini
Copy link
Owner

Great, thanks for the pointers. I'll look into this after I fix outstanding issues, this would be a great addition for sure.

@mfontanini
Copy link
Owner

mfontanini commented Oct 25, 2023

Alright so I looked a bit into this. My thoughts on each of these:

Mathjax

This is as simple as integration could get. The output of the render operation for that crate gives you a DynamicImage from the image crate which is the same presenterm uses internally so it would be trivial to integrate it. However, I do have some concerns:

  1. Dependencies: this crate alone with the default features (uses a headless chrome under the hood) pulls in 280+ dependencies. This is not necessarily terrible but that'd more than double the number of crates used by this tool so it isn't great.
  2. Reliability: this requires an internet connection to render formulas because it points to the mathjax js library hosted on some CDN. This to me is quite bad as I expect presentations to work above anything else. If your formula is correct and you have whatever tools are needed, it should be rendered. I don't want to give a presentation in a place with spotty wifi and suddenly have my live presentation break. Granted, we could tweak the code to have a local version of that script instead but that reduces the "simplicity" selling point of this solution (e.g. we need to bundle that file, copy it somewhere, reference it, etc).
  3. Speed: rendering a tiny formula takes around 4 seconds in release mode in my computer. This is a bunch of time that would stall the app when it opens (and yes, this could render in the background but I'd rather avoid this). We could cache it in the filesystem somewhere (e.g. ~/.presenterm/cache) though, so you'd only really pay for this once per formula. The cache also helps with the "reliability" point above: if you render it once you don't need to render it again so unless you're giving the presentation in a new computer, it would work. But then again, it goes against the simplicity selling point.

pdflatex + convert

This works quite nicely as well. I followed this idea (let me know if there's a better way to do this!) and it seems to work fine. How this could work would be we'd have a template .tex file, and then whenever we find a code block that contains latex the template file gets the user defined latex code replaced, written somewhere, then pdflatex it, convert, read it, display it. This would mean that you could not necessarily just have formulas which is... I guess okay? We could try to limit this to formulas but it's going to be quite tricky to restrict that you're not somehow escaping it so I think we might as well render whatever latex you write and there's that.

Notes on this one:

  1. Dependencies: you need to have pdflatex + the standalone class installed + imagemagick's convert for this to work, but at least these are external dependencies and don't bloat the app binary.
  2. Reliability: this is self contained so as long as you have the dependencies it should work.
  3. Speed: rendering is very fast; we wouldn't need caching or anything else.
  4. Security: I'm maybe a bit concerned about people escaping the latex file and doing bad stuff (e.g. writing to a file, running a command, etc). I'm not very savvy in latex so I don't know what you could do or how to avoid it, but the idea here is that presenterm would replace latex code blocks with the output image as it parses through the input markdown file. Therefore I don't want to allow for people uploading a malicious presentation file that does something bad. Could anyone provide thoughts on this one? Is there any way to prevent this? It seems like passing in -no-shell-escape should do the trick? Is there any other way to escape pdflatex that I should be concerned about? Think reading/writing arbitrary files.
  5. Portability: we should make sure this works in all platforms, or at least in mac given images don't currently work on windows anyway.

What are people's thoughts on this?

@ArvinSKushwaha
Copy link
Author

I am honestly quite a fan of this pdflatex + convert solution, but I'd love to hear thoughts from other people in the community.

@arcstur
Copy link

arcstur commented Nov 24, 2023

What about using the tectonic engine? It's written in Rust, and can be embedded as a Rust dependency in the project.

@mfontanini
Copy link
Owner

Yeah that's worth exploring too. I was also considering typst which, while not LaTeX, will let you write formulas. I managed to make this work in like 2 lines of typst the other day + I only had to install a single tool, while I had to use a bunch of boilerplate and install a bunch of stuff to make the pdflatex one work.

@arcstur
Copy link

arcstur commented Nov 24, 2023

Yeah, installing tex nowadays is really painful comparing to other modern tools. Tectonic aims to be easy to install and use and still produce latex output.

@ArvinSKushwaha
Copy link
Author

Could an interchangable backend, perhaps a plugin-friendly interface be suitable for future steps with this and other potential connections to external applications?

@mfontanini
Copy link
Owner

perhaps a plugin-friendly interface

I don't really want to go this route until there's enough use cases that makes this worthwhile. For now I think it makes sense to use the best tool available here, which may be tectonic given besides being a single tool, it can be installed in every platform easily.

I have some stashed code that already works to some extent here but I need to pick it back up and polish it. I'll get to this again soon.

@ArvinSKushwaha
Copy link
Author

ArvinSKushwaha commented Nov 28, 2023

Would it be possible to get a sneak peek at this rough code, say so I could give a presentation tomorrow with it? If not, that's very understandable.

Also, I think I agree with this assessment, Tectonic could be (hopefully) quite seamless integration.

@mfontanini
Copy link
Owner

lol no, sorry, it's a bit too early. I'm prioritizing other stuff for now but I'll get to this soon. What I had was pretty crappy, as in, the size of the rendered formula was tiny. You can generate images and embed them if you want to use presenterm for now

@ArvinSKushwaha
Copy link
Author

ArvinSKushwaha commented Nov 28, 2023

That's fair, thank you for the response, and for all your hard work on this project!

@mfontanini
Copy link
Owner

mfontanini commented Dec 4, 2023

After #75 the latest code in master currently supports rendering latex (and typst!). I ended up using typst as the rendering engine. It is so much simpler to use and it generates a png image directly so there's no need to go through the pdf -> png conversion. As for latex, it is transformed into typst via pandoc which is great because I can use the simplicity of typst without caring at all about the source being latex.

I'll write docs on this but for now if you want to test it, install typst and pandoc and then create a code block with language typst or latex and add a +render attribute like

```latex +render
$e = {mc}^2$
```

This gets replaced by an image. There's some customization you can make like colors in the theme:

typst:
  colors:
    background: ff0000
    foreground: 00ff00

If the output looks too tiny vertically, you can edit the configuration in ~/.config/presenterm/config.yaml

typst:
  ppi: 400 # or higher values

This is a bit unfortunate but basically because the image gets rendered in multiple of terminal rows (e.g. 1 row or 2, not 1.5) if your image is too small vertically it looks a bit squished. The default ppi (300) works for me but it probably depends on your font size. Please let me know if it doesn't work.

PS: I considered tectonic but a) it also has the intermediate PDF step and b) I had issues running it both via the pre-compiled binary and via cargo install and I don't really want people fighting to install the necessary tools. pandoc/typst seem to be better packaged.

PS2: I need to work on the default styles and probably add some caching here but at least it's working (but let me know otherwise!)

@mfontanini
Copy link
Owner

mfontanini commented Dec 4, 2023

Forgot the image:

image

edit: I just adjusted the colors on built-in themes so it now looks a bit better.

@mfontanini
Copy link
Owner

Closing this. Docs are here

@ArvinSKushwaha
Copy link
Author

Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants