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

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

Open
alan-foo opened this issue May 19, 2023 · 9 comments
Open

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

alan-foo opened this issue May 19, 2023 · 9 comments

Comments

@alan-foo
Copy link

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

tdewolff commented May 23, 2023

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
Copy link
Author

alan-foo commented May 25, 2023

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
Copy link
Owner

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)
	}

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

No branches or pull requests

2 participants