LaTeX rendering in the Julia REPL

I watched the Latexify demonstration at JuliaCon here.

It seems from the video that simply entering something like latexify("x/y") into the terminal will result in output that looks like proper LaTeX.

However, when I tried on my terminal I ended up with this:
Screenshot from 2020-07-22 01-00-51

My version of the Julia REPL appears to simply display the raw version of the LaTeX, and also does so even when I try using show. Has something changed between then and now, or am I not displaying it properly?

AFAIK there’s no way to display proper LaTeX inside terminal emulator without rendering it out as some sort of image and then display with some image protocol.

It works in IJulia (Jupyter Notebook) though.

5 Likes

Thanks for your reply.
I was worried that was the only option though, since I was afraid making Jupyter Notebook a prerequisite to my package would decrease user-friendliness and ease of use.
I’ll think about it though.

I recently added a render function to latexify.jl. It’s still very much a beta-feature, but the idea is to generate an image with the relevant equation and open it in a new window.

I don’t have any completely satisfactory solutions for the REPL but this is better than nothing.

10 Likes

Are there any requirements for render? Would using it just look something like render(latexify("x/y"))?

render(latexify("x/y")), or my own favourite, latexify("x/y") |> render is correct usage. For it to work, however, you will need to have LuaLaTeX installed (+ some fairly basic packages). One day, I might make it more liberal such that it can use latex or pdflatex instead but currently it is pretty crude.

The code can (at the time of writing) be found here.

2 Likes

@korsbo, is this working in Julia’s 1.6.0 REPL?

well, there is no support for displaying rich media in any arbitrary REPL. render in Latexify.jl will, however, try to create an image from the maths and open that in an external viewer. It also works nicely with the VSCode plotpane if you’re in VSCode.

1 Like

there is also GitHub - MichaelHatherly/MathJaxRenderer.jl: Render LaTeX equations with MathJax offline.

its still very new but it’s something that I’ve been eyeballing as a potential replacement for the render function inside Latexify.jl.

3 Likes

is there some reason why you specify 1.6? Any new features in that REPL that I’ve missed?

No special reason, thank you.
Trying to visualize in VS Code this amazing post showing a continued fraction.
Quick attempts using Latexify.jl have not succeeded, yet.

6 Likes

@korsbo, excellent.
Thank you for your time showing the way.

1 Like

@korsbo, fyi, in order to plot to an external Plots.jl window, found this useful:

using Latexify, Plots; gr()
plot(framestyle = :none) 
title!(latexify(ex), titlefontsize=10)  # adjust font size

Plots_gr_GKS_QtTerm_Latex_continued_fraction

There should be a better way though, that automatically adjusts the font size.

2 Likes

There is also https://github.com/simonschoelly/KittyTerminalImages.jl, with LaTeX rendering on their TODOs.

1 Like

Why not over-engineer it? :stuck_out_tongue:

using RecipesBase, LaTeXStrings
@recipe function f(::Type{LaTeXString}, s::LaTeXString)
       framestyle := :none
       titlefontsize --> 10
       title := s
       legend := false
       ([0],[0])
end

using Plots
plot(L"x^7-3x+4")

probably also better to make it an annotation than a title, then it won’t look as strange in plotly, for instance.

3 Likes

Got an error in 1.7.2

julia> latexify("x/y") |> render
ERROR: MethodError: no method matching render(::LaTeXStrings.LaTeXString, ::MIME{Symbol("image/pdf")})
Closest candidates are:
  render(::LaTeXStrings.LaTeXString; kwargs...) at ~/.julia/packages/Latexify/v4lfi/src/utils.jl:59
  render(::LaTeXStrings.LaTeXString, ::MIME{Symbol("juliavscode/html")}; kwargs...) at ~/.julia/packages/Latexify/v4lfi/src/utils.jl:101
  render(::LaTeXStrings.LaTeXString, ::MIME{Symbol("application/pdf")}; debug, name, command, open) at ~/.julia/packages/Latexify/v4lfi/src/utils.jl:106
  ...
Stacktrace:
 [1] render(s::LaTeXStrings.LaTeXString; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ Latexify ~/.julia/packages/Latexify/v4lfi/src/utils.jl:60
 [2] render
   @ ~/.julia/packages/Latexify/v4lfi/src/utils.jl:60 [inlined]
 [3] |>(x::LaTeXStrings.LaTeXString, f::typeof(render))
   @ Base ./operators.jl:966
 [4] top-level scope
   @ REPL[3]:1

This one work

render(latexify("x/y"), MIME(Symbol("application/pdf")))
1 Like

Yes, that’s the default from Latexify v0.15.13.

might it ever be possible to render latex within the actual code ?

To write something like

@render x = a/b

And after the line is executed, it displays x = a/b as a Latex equation over two lines.

Then if the line causes an error or if you click on it, it goes back to the underlying code, for editing.