Skip to content

Instantly share code, notes, and snippets.

@asinghvi17
Last active March 15, 2023 01:22
Show Gist options
  • Save asinghvi17/f886095d711bc1291fa026b43671c01c to your computer and use it in GitHub Desktop.
Save asinghvi17/f886095d711bc1291fa026b43671c01c to your computer and use it in GitHub Desktop.
TeX rendering handled using Julia - mostly in memory!
# Generate the tex file
rawtex = raw"""
\documentclass{standalone}
\begin{document}
\[\left[{\frac{-\hbar^{2}}{2m}}\nabla^{2}+V(\mathbf{r})\right]\Psi(\mathbf{r}) = E\Psi(\mathbf{r})\] % Schrodinger equation
\end{document}
"""
lua_stdin = IOBuffer(writable = true)
# compile to a temporary file
luatex = run(pipeline(`dvilualatex --jobname=temp`, stdin = lua_stdin))
write(lua_stdin, rawtex)
close(lua_stdin)
success(luatex) # wait for the compilation to finish
dvi = read("temp.dvi", String)
# Dvisvgm - convert the DVI to an SVG
dvisvgm_in = IOBuffer(; writable = true)
dvisvgm_out = IOBuffer(; writable = true)
dvisvgm = run(pipeline(`dvisvgm --stdin --stdout`, stdin = dvisvgm_in, stdout = dvisvgm_out))
write(dvisvgm_in, dvi)
close(dvisvgm_in)
success(dvisvgm)
svg = read(dvisvgm_out)
# We now have an in-memory SVG, waiting to be rendered by Rsvg.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment