tdewolff / canvas

Cairo in Go: vector to raster, SVG, PDF, EPS, WASM, OpenGL, Gio, etc.
MIT License
1.5k stars 102 forks source link

\left and \right delimiters have issues rendering? #231

Open alan-foo opened 1 year ago

alan-foo commented 1 year ago

Hello! I have some issues with latex rendering.

text := `\left(a+b\right)\left[1-\frac{b}{a+b}\right]=a\`

latexPath, err := canvas.ParseLaTeX(text)
if err != nil {
        return
    }
c := canvas.New(50, 50)
ctx := canvas.NewContext(c)
ctx.DrawPath(0, 0, latexPath)
c.Fit(2.0)
renderers.Write("latex.png", c, canvas.DPMM(10))

When I run the example above, the png file that I get looks like this: latex

Did I do anything wrongly? Appreciate your reply, thank you!

tdewolff commented 1 year ago

Hey alan-foo, yeah the LaTeX renderer isn't great. Can you try turning on the LaTeX renderer of your system using go run -tags latex file.go? There is no great Go-only LaTeX library available, which is why using your system's implementation is usually better. The default Go version should be improved though, as it is very buggy with large symbols (such as brackets and sums).

alan-foo commented 1 year ago

Hello! I do not see any output though. Was I suppose to install any latex packages such as MacTex? (I am using a macbook)

tdewolff commented 1 year ago

You have to have some sort of LaTeX package installed. Specifically, it uses the latex and dvisvgm executables which have to be in your path.

alan-foo commented 1 year ago

I've tested it out, the output is actually still buggy. Not sure if I am doing anything wrongly. I am actually building something that requires me to process the latex equations into an image at the backend in golang, so that I can insert it into documents. Your library is the best there is. Are there any other workarounds that you know of? Sorry for asking something not part of this package. But appreciate your reply!

tdewolff commented 1 year ago

So you installed LaTeX on your system and used the -tags latex when building/running your Go project? Is the output the same as before, or is it buggy in another sense? What kind of bugs do you see, can you post a picture?

When using the system's LaTeX there isn't much that can go wrong. It literally creates a TeX file, compiles it, takes the DVI output and converts it to SVG using the same LaTeX build system, and the SVG is then converted to canvas' internal path structure.

The problem with using the ported Go version of LaTeX is that the DVI is created as before, which is perfect, but there is no reliable way to convert the DVI to SVG/canvas' paths since we don't have the original fonts the DVI uses. LaTeX uses some old and odd font formats, especially the mapping and some placement instructions for symbols is way off. The only way to fix this is not to use TeX, but some sort of pdfTeX or XeTeX which use the modern unicode fonts. Then we could use the official fonts since we can load those unicode based fonts (OTF) in canvas. Support for a ported XeTeX in Go is difficult which uses https://git.sr.ht/~sbinet/star-tex and depends on web2go (see https://gitlab.com/cznic/web2go/-/issues/2).

alan-foo commented 1 year ago

I ran the code like that: go run -tags /Library/TeX/texbin/xelatex file.go go run -tags /Library/TeX/texbin/pdflatex file.go go run -tags /Library/TeX/texbin/latex file.go

Tried with XeLaTeX and pdfLaTeX, but i still get the same type of output, where the \left and \right delimiters are placed way off. latex

So just to confirm, the same code snippet can still be used, just that underlying it would use pdfLaTeX/XeLaTeX/latex of my system when its ran?

tdewolff commented 1 year ago

Please run it as go run -tags latex file.go, the latex is a tag not a path. The latex and dvisvgm binaries should be available in your $PATH environment variable. You cannot choose to use pdflatex or xelatex, it always used LaTeX.

alan-foo commented 1 year ago

Hello! My apologies, I am very inexperienced in this, but yes I have managed to get them onto my path. I ran my code once, and it produced the image according to what it should look like. But when I run the same code with different latex input, the code runs, but no new output. Not too sure what's the issue now.

tdewolff commented 1 year ago

Did you check the error that comes out of canvas.ParseLaTeX? Remember to wrap your equation in $ for input:

    p, err := canvas.ParseLaTeX(`$y_i = y$`)
    if err != nil {
        panic(err)
    }