Skip to content

Instantly share code, notes, and snippets.

@jleugeri
Created June 23, 2019 14:49
Show Gist options
  • Save jleugeri/c8b7432d3c0b6053b7079ddeb7a95905 to your computer and use it in GitHub Desktop.
Save jleugeri/c8b7432d3c0b6053b7079ddeb7a95905 to your computer and use it in GitHub Desktop.
Render a latex equation to a raw svg string.
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