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

Export to Typst/Fletcher diagram #262

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Conversation

FliiFe
Copy link

@FliiFe FliiFe commented Jan 9, 2025

I've implemented initial support to export to Typst diagrams. Incompatibilities are reported through the incompatibilities metadata object (that is: shortened arrows, empty arrow body, and k-cells for k > 1)

This still needs some UI adjustments to show the incompatibilities and instructions just like with latex.

I marked this as Draft because I feel like this feature cannot be fully used as-is since I haven't implemented in-browser rendering of the formulas. Looking into how to do that with typst.ts.

Feel free to leave comments and to try this out. Diagrams should be readily useable with

#import "@preview/fletcher:0.5.3" as fletcher: diagram, node, edge

at a document's start.

@FliiFe FliiFe marked this pull request as draft January 9, 2025 17:55
@FliiFe
Copy link
Author

FliiFe commented Jan 9, 2025

For a bit of fun here's an example in which I crammed almost every variation of options (except those that are incompatible with Fletcher)

image
image

@FliiFe
Copy link
Author

FliiFe commented Jan 9, 2025

@varkor Couple questions if you have time for them.

At this point I've implemented Typst rendering, but the (AFAIK only) library is very bare bones so I've had to work around some minor issues. Notably, concurrent renders seem to break the library but I've implemented a queue for the rendering, which is quick enough to work as a permanent solution. Onto real issues:

  • Typst.ts loads a 20MB binary in memory (it's the wasm-compiled typst compiler). Would you rather have everything load from the project as is currently the case with KaTeX, at the expense of having a 20M binary added to the directory, or would you rather load the library & binary from a CDN ? Obviously there's no doubt in my mind this will end up being opt-in and lazily loaded.
  • When rendering with Typst, I simply overrite the innerHTML of the corresponding label.element. From what I could gather this is also what KaTeX does. However, some functionalities seem reliant on those .katex, .katex-error divs existing. Could you briefly explain why ? What I've noticed is that
    1. all the labels for edges have disappeared
    2. moving cells is completely broken

Do you have an idea about the possible cause of these breakages ?

Cheers

@FliiFe
Copy link
Author

FliiFe commented Jan 10, 2025

const defs = this.requisition_element(this.svg, "defs");

This call is returning undefined if I do render arrow labels with typst. The rendered version is an SVG, and in execution of requisition_element, elements.length is actually 3. This means at some point the SVG generated by Typst was taken into account when it shouldn't have. Any idea where the corresponding selector resides ?

EDIT: Fixed, see commit below.

@varkor
Copy link
Owner

varkor commented Jan 10, 2025

Thanks for the pull request!

I must admit that it hadn't occurred to me that Typst has different syntax for mathematics than LaTeX, which makes support for Typst subtler. I suppose it will be necessary to have a toggle in the UI for LaTeX/Typst mode – I'll have to think about how to do that without complicating the UI too much.

  • Regarding loading the binary, my feeling is that (at least for now) Typst users will be a small minority, so that it probably makes more sense to load it from a CDN (this can always be changed in the future if/when Typst becomes more popular).
  • Once a KaTeX equation had been rendered, we need to examine the corresponding .katex, .katex-error element so that we can measure the width/height of the equation (since KaTeX does not have APIs for measuring equations, or at least did not when quiver was initially written). This is used to work out the bounding boxes for the label elements, so that they are positioned and cropped correctly. If there is some element whose width/height corresponds to the dimensions of a Typst equation, it should be possible to simply use that element in place of the .katex, .katex-error elements – I don't believe there are any special features of those elements that are being used that wouldn't be valid more generally.

Unfortunately, the timing is a little inconvenient, as I'm about to go on a research trip for a few weeks, and don't yet know whether I'll have time to look at this in detail before I get back. However, if I don't manage to look at it during my trip, it will be a priority for me once I return. I'll try to answer any questions you have in the meantime, though.

@FliiFe
Copy link
Author

FliiFe commented Jan 10, 2025

Thanks for your time ! Regarding the second point, this was actually all addressed in the latest commit which makes me confident this is close to releasable. Don't worry about the timing though, I can just host the fork somewhere in the meantime.

What's left (for me) to do:

  • Colour labels correctly when rendering with typst
  • Add a toggle somewhere to switch between KaTeX and Typst (lazy-loading will be easy to implement). I do feel like this setting should be remembered, via localStorage most likely.
  • Implement the incompatibilities-reporting functionality in the typst export window, and maybe add some customization options there too

@FliiFe FliiFe marked this pull request as ready for review January 12, 2025 20:33
@FliiFe
Copy link
Author

FliiFe commented Jan 12, 2025

That's it, I'm calling it releasable ! Let me know what you think.

@FliiFe FliiFe changed the title [DRAFT] Export to Typst/Fletcher diagram Export to Typst/Fletcher diagram Jan 12, 2025
Copy link

@jingkaimori jingkaimori left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some detailed but incomplete suggestion for code

src/ui.mjs Outdated Show resolved Hide resolved
src/ui.mjs Outdated Show resolved Hide resolved
@jingkaimori

This comment was marked as resolved.

Co-authored-by: jingkaimori <jingkaimori@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants