# Base.show() for displaying MathJax formulas in Jupyter / IJulia?

**URL:** <https://discourse.julialang.org/t/base-show-for-displaying-mathjax-formulas-in-jupyter-ijulia/57244>\
**Category:** Jupyter-Notebook\
**Created:** [March 15, 2021, 10:31pm UTC](https://discourse.julialang.org/t/base-show-for-displaying-mathjax-formulas-in-jupyter-ijulia/57244 "2021-03-15T22:31:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [March 15, 2021, 10:31pm UTC](https://discourse.julialang.org/t/base-show-for-displaying-mathjax-formulas-in-jupyter-ijulia/57244/1 "2021-03-15T22:31:23Z")

</div>

I’d like to override Base.show() for a custom type I defined, to displayed an object of the type as a math formula (MathJax / LaTeX) when using Jupyter notebook. How can this be done? I already know how to override Base.show() for the command line interface, but want to know how to exploit Jupyter’s math rendering capabilities.

---

<div class="post-metadata">

**Author:** ![drjrkuhn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drjrkuhn/32/920_2.png) [@drjrkuhn](https://discourse.julialang.org/u/drjrkuhn)\
**Post date:** [March 17, 2021, 11:41am UTC](https://discourse.julialang.org/t/base-show-for-displaying-mathjax-formulas-in-jupyter-ijulia/57244/2 "2021-03-17T11:41:02Z")

</div>

Along those lines, defining Julia code with inline Latex math strings like \frac and rendering them as such in Jupyter would be nice too.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 17, 2021, 12:00pm UTC](https://discourse.julialang.org/t/base-show-for-displaying-mathjax-formulas-in-jupyter-ijulia/57244/3 "2021-03-17T12:00:22Z")

</div>

> [@greatpet](#):
>
> I’d like to override Base.show() for a custom type I defined, to displayed an object of the type as a math formula (MathJax / LaTeX) when using Jupyter notebook. How can this be done?

You can use `text/latex`:

```julia
Base.show(io::IO, ::MIME"text/latex", x::MyObject) =
    print(io, "\\frac{1}{1+2+3+4}")

```

or you can use markdown (either `text/markdown` or via the `Markdown` stdlib) with embedded equations if you need more flexible formatting.

> [@drjrkuhn](#):
>
> Along those lines, defining Julia code with inline Latex math strings like \frac and rendering them as such in Jupyter would be nice too.

You can put LaTeX in the docstrings of your Julia code and then it will render as equations in the Jupyter help.

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [March 17, 2021, 1:38pm UTC](https://discourse.julialang.org/t/base-show-for-displaying-mathjax-formulas-in-jupyter-ijulia/57244/4 "2021-03-17T13:38:35Z")

</div>

> [@stevengj](#):
>
> ```julia
> Base.show(io::IO, ::MIME"text/latex", x::MyObject) =
> print(io, "\\frac{1}{1+2+3+4}")
> 
> ```

Unfortunately this doesn’t seems to work for the latest versions of Julia (1.5.4) and Jupyter. Here’s my screenshot (click to enlarge).

 ![JupyterPrint](https://global.discourse-cdn.com/julialang/original/3X/a/2/a2f75090fa6d11b574dd192cdaadf6b441f4c72d.png)

I suspect there is some API change.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [March 17, 2021, 2:29pm UTC](https://discourse.julialang.org/t/base-show-for-displaying-mathjax-formulas-in-jupyter-ijulia/57244/5 "2021-03-17T14:29:10Z")

</div>

I think you need `$`s.

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [March 17, 2021, 2:33pm UTC](https://discourse.julialang.org/t/base-show-for-displaying-mathjax-formulas-in-jupyter-ijulia/57244/6 "2021-03-17T14:33:47Z")

</div>

You’re right, this worked (using $$ display environment):

```
Base.show(io::IO, ::MIME"text/latex", x::MyObject) =
    print(io, "\$\$\\frac{1}{1+2+3+4}\$\$")

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [March 17, 2021, 2:36pm UTC](https://discourse.julialang.org/t/base-show-for-displaying-mathjax-formulas-in-jupyter-ijulia/57244/7 "2021-03-17T14:36:45Z")

</div>

If it’s a more complicated pattern, and you want it to be a bit more explicit, you can use `Latexify` and define a latexrecipe for your type. This will also be recursive:

```julia
@latexrecipe function f(m::MyType)
    return :(1/(3+4a+5b))
end

julia> latexify(:(x=$(MyType()))
L"$x = \frac{1}{3 + 4a + 5b}$" 

```

(I’m on my phone, take everything with a dash of cayenne)
