Created
June 23, 2019 14:49
-
-
Save jleugeri/c8b7432d3c0b6053b7079ddeb7a95905 to your computer and use it in GitHub Desktop.
Render a latex equation to a raw svg string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using LaTeXStrings | |
function to_svg(formula::LaTeXString ; preamble="\\usepackage{amsmath}") | |
text=""" | |
\\documentclass[border=2pt]{standalone} | |
$(preamble) | |
\\usepackage{varwidth} | |
\\begin{document} | |
\\begin{varwidth}{\\linewidth} | |
$(String(formula)) | |
\\end{varwidth} | |
\\end{document} | |
""" | |
text = try | |
f = tempname() | |
dir,_ = splitdir(f) | |
write(f, text) | |
run(`pdflatex -output-directory=$(dir) $(f)`) | |
run(`pdf2svg $(f).pdf $(f).svg`) | |
open("$(f).svg") do svg | |
return read(svg, String) | |
end | |
catch e | |
println("Failed to execute conversion with error: $(e)") | |
"" | |
end | |
return text | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment