# Julia Symbolics in Jupyter - Initializing LaTeX printing

**URL:** <https://discourse.julialang.org/t/julia-symbolics-in-jupyter-initializing-latex-printing/110377>\
**Category:** General Usage\
**Created:** [February 18, 2024, 5:01pm UTC](https://discourse.julialang.org/t/julia-symbolics-in-jupyter-initializing-latex-printing/110377 "2024-02-18T17:01:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![brombo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brombo/32/47867_2.png) [@brombo](https://discourse.julialang.org/u/brombo)\
**Post date:** [February 18, 2024, 5:01pm UTC](https://discourse.julialang.org/t/julia-symbolics-in-jupyter-initializing-latex-printing/110377/1 "2024-02-18T17:01:05Z")

</div>

I am familiar with using python and sympy on Jupyter Notebook. When printing LaTeX expressions on Notebook I have in my printing modules the statement -

```julia
init_printing(
                use_latex='mathjax',
                latex_mode='equation*',
                latex_printer=latex,
                # Affects only the plaintext printing, and makes our printing
                # tests easier to maintain
                wrap_line=False,
            )

```

which is executed by the function `Format()`.

I also print the following LaTeX macros so I can use them in print statements -

```julia
ip_cmds = raw"""
$\DeclareMathOperator{\Tr}{Tr}
\DeclareMathOperator{\Adj}{Adj}
\newcommand{\bfrac}[2]{\displaystyle\frac{#1}{#2}}
\newcommand{\lp}{\left (}
\newcommand{\rp}{\right )}
\newcommand{\paren}[1]{\lp {#1} \rp}
\newcommand{\half}{\frac{1}{2}}
\newcommand{\llt}{\left <}
\newcommand{\rgt}{\right >}
\newcommand{\abs}[1]{\left |{#1}\right | }
\newcommand{\pdiff}[2]{\bfrac{\partial {#1}}{\partial {#2}}}
\newcommand{\npdiff}[3]{\bfrac{\partial^{#3} {#1}}{\partial {#2}^{#3}}}
\newcommand{\lbrc}{\left \{}
\newcommand{\rbrc}{\right \}}
\newcommand{\W}{\wedge}
\newcommand{\prm}[1]{{#1}'}
\newcommand{\ddt}[1]{\bfrac{d{#1}}{dt}}
\newcommand{\R}{\dagger}
\newcommand{\deriv}[3]{\bfrac{d^{#3}#1}{d{#2}^{#3}}}
\newcommand{\grade}[1]{\left < {#1} \right >}
\newcommand{\f}[2]{{#1}\lp {#2} \rp}
\newcommand{\eval}[2]{\left . {#1} \right |_{#2}}$
"""

```

In Symbolics.jl is there an equivalent to the `init_printing` statement for use in Jupyter Notebook. I am doing this so I can annotate my latex strings. An example would be

```julia
print(r"\theta_{l} =",latex_string)

```

---

<div class="post-metadata">

**Author:** ![moble](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moble/32/23535_2.png) [@moble](https://discourse.julialang.org/u/moble)\
**Post date:** [February 20, 2024, 5:17pm UTC](https://discourse.julialang.org/t/julia-symbolics-in-jupyter-initializing-latex-printing/110377/2 "2024-02-20T17:17:12Z")

</div>

Hi Alan! I’m a fellow GA nerd. On that note, I just want to make sure that you’ve seen [Grassmann.jl](https://github.com/chakravala/Grassmann.jl). There are other GA julia packages, but that one’s unlike anything I’ve seen elsewhere. You and I actually interacted over galgebra a long time ago (before it went into sympy, even), and I helped move some of your unicode/latex printing stuff into sympy itself, so this feels like familiar territory!

First, I will say that your arguments to `init_printing` are (essentially) the default behavior of Symbolics. For no good reason that I know of, Symbolics wraps expressions in `$$` rather than `equation*`, but I think mathjax treats those the same. So I don’t see anything you really need to adjust. Otherwise, I think the only control you have is to [modify the defaults of Latexify.jl](https://korsbo.github.io/Latexify.jl/latest/#Setting-your-own-defaults). (See why that’s relevant below.) It looks like the possible defaults to modify are listed [here](https://korsbo.github.io/Latexify.jl/latest/arguments/).

As for your `ip_cmds`, you basically just need to tell mathjax about them. The easiest way I know of is to just run

```julia
import LaTeXStrings

display(LaTeXStrings.LaTeXString(ip_cmds))

```

That “displays” some blank output, which nonetheless contains your commands. But then mathjax knows what to do with, e.g., `$x \W y$`. You can even use these manually in your markdown cells. If you’re making a package, you would call `import LaTeXStrings` inside your module, and then you could create your own `init_printing` function that just calls this `display` command.

* * *

Under the covers, Julia uses [the `show` function](https://docs.julialang.org/en/v1/base/io-network/#Base.show-Tuple%7BIO,%20Any,%20Any%7D) to print out objects, kind of like python’s ` __str__ ` and ` __repr__ ` methods. Different formats are controlled by different `mime` arguments. Specifically, in a notebook, `show` is automatically called with `mime=MIME("text/latex")`. (And actually, it also calls it with `MIME("text/plain")`, but that output is just saved within the notebook, but not displayed.) So to hook into this behavior, `Symbolics` defines `Base.show` methods for the types that it creates. Those, in turn, generally rely on the `latexify` function from [Latexify.jl](https://github.com/korsbo/Latexify.jl).

To enable pretty latex with your own objects, you would define new methods of `Base.show` with your own types. At a first approximation, I’d guess that you’ll just want `show` to call `latexify`:

```julia
function Base.show(io::IO, ::MIME"text/latex", e::AbstractGAElement)
    print(io, latexify(e))
end

```

Then, your goal would be to teach `latexify` how to write your various elements. It looks like [the recommended way to do this is to use “recipes”.](https://korsbo.github.io/Latexify.jl/latest/tutorials/recipes/)

---

<div class="post-metadata">

**Author:** ![brombo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brombo/32/47867_2.png) [@brombo](https://discourse.julialang.org/u/brombo)\
**Post date:** [February 20, 2024, 6:22pm UTC](https://discourse.julialang.org/t/julia-symbolics-in-jupyter-initializing-latex-printing/110377/3 "2024-02-20T18:22:23Z")

</div>

I made it work in Notebook this way -

```julia

function isJupyter()
return isdefined(Main, :IJulia) && Main.IJulia.inited
end

#In Jupyter Notebook you can render a LaTeX string with display.Latex(latex_str)

if isJupyter()
display = pyimport("IPython.display")
else
display = "" #I don't know what I want it to be if not in notebook or I just won't use it at all in that case
end
```

---

<div class="post-metadata">

**Author:** ![brombo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brombo/32/47867_2.png) [@brombo](https://discourse.julialang.org/u/brombo)\
**Post date:** [February 20, 2024, 6:28pm UTC](https://discourse.julialang.org/t/julia-symbolics-in-jupyter-initializing-latex-printing/110377/4 "2024-02-20T18:28:51Z")

</div>

I know about Grassmann.jl. I am trying to make the user interface in a Julia version of galgebra look like the sympy version and clean up the internals. I also want to maintain the capability of a non-orthogonal basis. In sympy galgebra I got overly enthuses about sympy non-commutative symbols and used them internally when there was a much simpler way of doing things. By the way, for graphics I proselytize Asymptote which does have a Julia wrapper (I don’t know how good it is)

[https://asymptote.sourceforge.io/](https://asymptote.sourceforge.io/)

---

<div class="post-metadata">

**Author:** ![moble](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moble/32/23535_2.png) [@moble](https://discourse.julialang.org/u/moble)\
**Post date:** [February 20, 2024, 6:32pm UTC](https://discourse.julialang.org/t/julia-symbolics-in-jupyter-initializing-latex-printing/110377/5 "2024-02-20T18:32:25Z")

</div>

I’m not sure what you mean here. The `display` function is actually [built in to Julia](https://docs.julialang.org/en/v1/base/io-network/#Base.Multimedia.display), so it should always be available — whether in the REPL or in a notebook. Relying on python to do things sounds brittle.

---

<div class="post-metadata">

**Author:** ![brombo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brombo/32/47867_2.png) [@brombo](https://discourse.julialang.org/u/brombo)\
**Post date:** [February 20, 2024, 8:02pm UTC](https://discourse.julialang.org/t/julia-symbolics-in-jupyter-initializing-latex-printing/110377/6 "2024-02-20T20:02:44Z")

</div>

Attached is pdf of simple Julia Notebook to show what works and what doen’t on my computer. Am I not using `display()` correctly?

(Attachment Untitled3-1.pdf is missing)

---

<div class="post-metadata">

**Author:** ![brombo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brombo/32/47867_2.png) [@brombo](https://discourse.julialang.org/u/brombo)\
**Post date:** [February 20, 2024, 8:10pm UTC](https://discourse.julialang.org/t/julia-symbolics-in-jupyter-initializing-latex-printing/110377/7 "2024-02-20T20:10:54Z")

</div>

Shown is simple Julia Notebook to show what works and what doesn’t on my computer. Am I not using `display()` correctly? The Notebook is running Julia 1.10.1

 ![](https://global.discourse-cdn.com/julialang/original/3X/f/4/f4b86d303884e8bf5f1503970b55b87dc0331e46.jpeg)

---

<div class="post-metadata">

**Author:** ![moble](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moble/32/23535_2.png) [@moble](https://discourse.julialang.org/u/moble)\
**Post date:** [February 20, 2024, 8:27pm UTC](https://discourse.julialang.org/t/julia-symbolics-in-jupyter-initializing-latex-printing/110377/8 "2024-02-20T20:27:30Z")

</div>

That’s correct. The problem is that you’re just asking Julia to `display` a literal string. It doesn’t know that you want it to be interpreted as a LaTeX string. That’s why you need the `LaTeXStrings.LaTeXString` function in Julia, just as you needed `Display.Latex` in python.

You might have to install it (one time) with

```julia
import Pkg; Pkg.add("LaTeXStrings")

```

But once you’ve done that, you can always do

```julia
using LaTeXStrings

```

Then, you need to tell Julia that you want a string to be interpreted as LaTeX. The easiest way is to just stick an `L` in front of the string:

```julia
L"$\theta$"

```

Note that I don’t even have to escape the dollar signs and backslash!

So try this: install the package, open a new Jupyter notebook, and then just run these two lines:

```julia
using LaTeXStrings
display(L"$\theta$")

```

It should display what you want via mathjax. (In fact, you don’t even need to call `display` because Julia automatically displays the last thing in a cell.) No python needed!

For programmatic use, it may make more sense to pass a normal string to `LaTeXString`, which produces the same kind of object:

```julia
normal_string = raw"$\theta$"
LaTeXString(normal_string)

```
