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

SVG output #375

Open
jbaayen opened this issue Oct 30, 2015 · 21 comments
Open

SVG output #375

jbaayen opened this issue Oct 30, 2015 · 21 comments
Milestone

Comments

@jbaayen
Copy link

jbaayen commented Oct 30, 2015

SVG output, as offered by MathJax, would be great to have. Applications include formulas in diagrams, for example.

@kevinbarabash
Copy link
Member

@jbaayen there's been some work to render to canvas, see #251. Rendering to other targets is not a huge priority. We have discussed providing some sort of intermediate representation to make this easier.

@mbourne
Copy link

mbourne commented Oct 31, 2015

@jbaayen: I'm developing SVG graphs, and the approach I'm using when including formulas is to overlay the SVG with HTML elements. There are some examples on this page:

http://www.intmath.com/exponential-logarithmic-functions/calculating-e.php

It suits my purposes.

@jbaayen
Copy link
Author

jbaayen commented Oct 31, 2015

@kevinbarabash @mbourne, these sound like steps in the right direction!

We're looking at an out-of-browser application though, where SVG is the graphics format. Given an intermediate representation, we may be able to develop the SVG output ourselves.

@miestasmia
Copy link

miestasmia commented Nov 1, 2015

I'd just like to add that foreignObject is horrendously broken in WebKit and blink. Positioning is completely off.

E.g. see https://stackoverflow.com/a/10353045/1248084

@pnsaevik
Copy link

If anyone decides to implement SVG output, it would be nice to do it with <text> tags instead of outlined glyphs. This way it would be much faster to load. MathJax uses outlined glyphs.

@gagern
Copy link
Collaborator

gagern commented Dec 11, 2015

@pnsaevik: If one were to use <text>, one would have to make sure that the fonts used are the ones shipped with KaTeX. In a browser, it might suffice to use the same CSS file as for KaTeX. For stand-alone SVG, one might have to link to external resources or embed the fonts as data: URIs. And one would have to choose between WebFonts and SVG fonts. The latter don't support various features like hinting, but on the other hand SVG fonts are the only font the SVG 1.1 spec requires viewers to support. Not that it were actually supported in the wild, though.

@pnsaevik
Copy link

@gagern Thanks for your comments. It would definitely have to rely on webfonts, just as regular KaTeX. And it would be for the use within browsers, coupled with the KaTeX CSS file. But then, one may argue that CSS is sufficient, and that SVG provides no extra advantage.

@bryevdv
Copy link

bryevdv commented Jun 11, 2017

But then, one may argue that CSS is sufficient, and that SVG provides no extra advantage.

I realize this is probably not currently high on any priority list, but just by way of documenting use-cases: Bokeh just added an SVG output option for plots (a long requested feature). Users can also render KaTeX labels on plots with an extension, but since these are separate DOM elements, they are missing from the SVG export. We use canvas2svg to convert the plot canvas to SVG, and presumably if KaTeX could generate SVG as well, we could find a way to merge things together. This would make users wanting to export Bokeh plots for inclusion in scientific publications very extremely happy.

@jpatel3
Copy link

jpatel3 commented Aug 9, 2017

@mbourne Are you using foreignObject? how are you referring the position and element in SVG?

@mbourne
Copy link

mbourne commented Aug 10, 2017

@jpatel3 No, I'm not using foreignObject. For the particular example I linked to above, I'm using a PHP -> SVG library.

My text creating function has the following parameters:

function text($pt,$str,$pos,$class,$attrs,$brdParams) {...}

where $pt is something like [1, 3] and $str is either plain text (which creates an ordinary SVG element), or if it contains HTML tags or KaTeX, it creates an absolute-positioned div that sits on top of the SVG graph at position [1,3], determined by the pixel count from the left and top of the div containing the SVG element, allowing for the position of origin and any padding, using this.

$htmlTxt = "<div class=\"$class svgHtml\" style=\"top:".round($height - $pt[1]*$yunitlength-$oy-$padding)."px;left:".round($pt[0]*$xunitlength+$ox)."px\">$str</div>";

As the whole thing is being created server-side, any KaTeX sitting on top of the SVG is processed along with the rest of the page on page load.

@JacobBeaudry
Copy link

Another use case: being able to combine mathematical symbols with text within a text SVG element. It is possible to do with foreignObject; however, the text element has various attributes that make it substantially easier to manipulate (e.g. text-anchor & not having to know the dimensions of the containing box in advance, which greatly facilitates transformations).

@fabiospampinato
Copy link
Contributor

fabiospampinato commented Oct 8, 2021

Additional benefits of outputting to SVG: an SVG can base64-encoded and directly piped into an <img> tag, which would lead to fewer DOM nodes on the page (which generally makes things faster) and it also would require essentially 0 sanitization whatsoever, speeding things up that way too.

@joshsunshine
Copy link

In the Penrose project, we currently use MathJax to render labels for our diagrams. We would love to switch to KaTeX, but we need SVG support to do so.

@enjoysmath
Copy link

Hello, I would like to tird this proposal. I also need SVG output. Right now I'm limited to PNG screen captures of a QWebEnginePage displaying the KaTeX-rendered page. This is not preferable. What's preferable is to preserve the zoomability by keeping things SVG, but I don't know how to grab it!

🥇

@StarHosea
Copy link

really need this feature for ouput pdf

@mbourne
Copy link

mbourne commented Mar 22, 2023

@StarHosea But you can do PDF output of pages containing KaTeX math without SVG. What will SVG give you that HTML doesn't?

@StarHosea
Copy link

StarHosea commented Mar 24, 2023

@mbourne We generate PDF in node server by tools like pdfmake to, which doesn't support html .
I am trying to use MathJax to render Tex to svg , and then render to pdf

@HKGx
Copy link

HKGx commented Mar 24, 2023

I am trying to use MathJax to render Tex to svg , and then render to pdf

@StarHosea, I hacked my way using MathJax to render SVG files in https://github.com/HKGx/rendertex/tree/main/src/services

@StarHosea
Copy link

@HKGx Thanks a lot , I used mathjax-node like this

import  mjApi  from 'mathjax-node';
mjApi.start();
const formula = 'k = \frac{2\pi}{\lambda}';
const result = await mjApi.typeset({
    math: formula,
    format: 'TeX',
    svg: true
  });

@enjoysmath
Copy link

enjoysmath commented Mar 25, 2023 via email

@MohammadRaziei
Copy link

Is there any solutions? I used katex with d3js to create an SVG. but when I export it... none of maths shows in exported svg. so I need to render the input as SVG in foreignObject

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests