julialatexsymbolic-math

Displaying Symbolic Variable Name as LaTeX in Julia


I am working with Julia and the Symbolics package to display variable names as LaTeX. I have a variable defined with a LaTeX-like name using the @variables macro:

Pkg.add("Symbolics")
using Symbolics

@variables var"\frac{\partial y}{\partial x}"

I would like to display this variable name as LaTeX, but when I use display(var"\frac{\partial y}{\partial x}"), it shows the value of the variable rather than the LaTeX expression. I want to display the variable name like using the L"" string :

L"\frac{\partial y}{\partial x}" |> display

Is there a way to display this symbolic variable name as LaTeX, so it shows the variable name itself.

I have tried the following approaches to display the symbolic variable var"\frac{\partial y}{\partial x}" as LaTeX:

I attempted to display the variable:

front = "L\""
back = '"'
eval(print(front, :(var"\frac{\partial y}{\partial x}") ,back)) |> display

but it did not work.

I expect that I can use metaprogramming to display the symbolic variable var"\frac{\partial y}{\partial x}" as LaTeX i.e. as output of L"\frac{\partial y}{\partial x}" |> display, showing the variable name itself, var"\frac{\partial y}{\partial x}".

My expectation is to find a solution or an alternative approach that allows me to display the symbolic variable name as LaTeX, preserving the variable's LaTeX-like formatting.


Solution

  • You have two options.

    First, by specifying the MIME of the output, you can get the variable to be displayed as a LaTeX equation:

    julia> x = (@variables var"\frac{\partial y}{\partial x}")[1]
    var"\\frac{\\partial y}{\\partial x}"
    
    julia> display("text/latex", x)
    $$ \begin{equation}
    \frac{\partial y}{\partial x}
    \end{equation}
     $$
    

    You can also turn the variable into a Symbol (by calling Symbol) and then a LaTeXString (by calling LaTeXStrings.jl’s latexstring), which will get you the LaTeX in inline form.

    julia> latexstring(Symbol(x))
    L"$\frac{\partial y}{\partial x}$"