Plaintext output in IJulia instead of html display

Hopefully a picture conveys my issue:

I’d like to understand why a Polynomial object - which can be rendered beautifully in an IJulia notebook - isn’t rendered nicely by default, and is there a simple recipe to tell IJulia/julia how I’d like my polynomials rendered?

IJulia captures the output in multiple formats and then the frontend decides which format is the “best one”. You can see this if you look at the .ipynb file:

   "outputs": [
    {
     "data": {
      "text/html": [
       "1 + 2∙x + 3∙x^2 + x^4"
      ],
      "text/latex": [
       "$1 + 2\\cdot x + 3\\cdot x^{2} + x^{4}$"
      ],
      "text/plain": [
       "Poly(1 + 2*x + 3*x^2 + x^4)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],

and, apparently text/html is ranked higher than text/latex in this case. It is possible to show superscripts in HTML so it seems like this is an oversight in Polynomials. I submitted a PR for that: https://github.com/JuliaMath/Polynomials.jl/pull/167 which results in:
poly

2 Likes

Untested, but I guess you can do

append!(empty!(IJulia.ijulia_mime_types), [
    MIME("text/plain"),
    MIME("image/svg+xml"),
    [MIME("image/png"),MIME("image/jpeg")],
    [
        MIME("text/latex"),
        MIME("text/markdown"),
        MIME("text/html"),
    ],
])

The original setting is

But I’m not sure if this is for user-level configuration.

Thanks for opening a PR to fix this straight away @fredrikekre. I didn’t realize that IJulia captures the output in multiple formats - that is quite cool.

@tkf your code was correct to change the default mime type, but as it is changing the default output for all outputs it doesn’t look like something a user should touch!

What the behavior looks like after the fix:

If you were trying to override the IJulia output just for Polynomials would this be a reasonable approach?

using Polynomials

Polynomials.printpoly(io::IO, p::Poly{T}, ::MIME"text/html"; descending_powers=false, offset::Int=0) where {T} =
    Polynomials.printpoly(io, p, MIME"text/latex"(), descending_powers, offset)

It assumes that HTML renderer has MathJax and configured to recognize $...$. I guess it’s fine in IJulia, but if you want to add it to Polynomials.jl maybe you’d want to check isdefined(Main, :IJulia) && Main.IJulia.inited.

Or I guess a better way to do it is to set IOContext like :mathjax => true in IJulia, Documenter, etc. so that packages like Polynomials.jl can change its behavior in text/html show.