7
$\begingroup$

One of the nice thing about running Mathematica codes in the front-end, is that one can attach custom typesetting rules to the user-defined symbols. Then, setting the format type of new output cells to TraditionalForm provides you with a nice output similar to what one sees in papers and textbooks.

Suppose that someone wants to use my package (that comes with extensive typesetting rules) with the free Wolfram Engine, where the front-end is a Jupyter notebook. As far as I can see, no typesetting is displayed by default.

As a minimal working example, consider the following code.

cf /: MakeBoxes[cf[i_], TraditionalForm] := SubscriptBox["c", i];
SMP /: MakeBoxes[SMP["g_W"], TraditionalForm] := 
  SubscriptBox["g", "W"];
MakeBoxes[myvec[x_], TraditionalForm] := 
  TemplateBox[{OverscriptBox[ToBoxes[x], "\[RightArrow]"]}, "Abs"];
MakeBoxes[Power[myvec[x_], n_], TraditionalForm] := 
  SuperscriptBox[
    TemplateBox[{OverscriptBox[ToBoxes[x], "\[RightArrow]"]}, "Abs"], 
    ToBoxes[n]] /; OddQ[n];
MakeBoxes[Power[myvec[x_], n_], TraditionalForm] := 
  SuperscriptBox[OverscriptBox[ToBoxes[x], "\[RightArrow]"], 
    ToBoxes[n]] /; EvenQ[n];

In Mathematica, for

SMP["g_W"] (cf[1] myvec[v] + cf[2] myvec[v]^2 + cf[3] myvec[v]^3)

I get the expected typesetted output, but in Jupyter this is not the case.

Mathematica output Jupyter output

Is there something that can be tweaked in the notebook configuration or in the package source code, to get the typesetting working?

$\endgroup$
  • 1
    $\begingroup$ Where is TraditionalForm set for In[17]? $\endgroup$ – Kuba Jun 3 '19 at 11:59
  • 1
    $\begingroup$ I have not tried this, but please try adding //StandardForm or //TraditionalForm $\endgroup$ – Szabolcs Jun 3 '19 at 11:59
  • 1
    $\begingroup$ Actually, you are only defining it for TraditionalForm, so use //TraditionalForm $\endgroup$ – Szabolcs Jun 3 '19 at 12:00
  • 1
    $\begingroup$ WOW, with TraditionalForm it just works! Sorry guys, I always have my format type of new output cells set to TraditionalForm in Mathematica, so I didn't think that it is StandardForm in Jupyter by default, although it is quite logical. BTW, is there a way to make Jupyter always output TraditionalForm by default? $\endgroup$ – vsht Jun 3 '19 at 12:19
  • 1
    $\begingroup$ SetOptions[$Output, FormatType -> TraditionalForm]? ( try it, I don't know, I have not used WE yet) $\endgroup$ – Kuba Jun 3 '19 at 12:53
8
$\begingroup$

I'd just use StandardForm because the loop WE-Jupyter is tricky to hook to.

I guess this solution won't work once WLforJupyter is out of beta stage but anyway:

toOut = toOutImage @* TraditionalForm;
toOut /: (toOut = _) := Null;

It is not a joke :)

enter image description here

Explanation

toOut and toOutImage are symbols that are originally defined within the loop that handles communication between the Kernel and Jupyter notebook. Kernel sends evaluated response but Jupyter does not understand MMA Box language so it needs to be something else. Currently it is implemented that it is either a plain text, an image or an embedded iFrame with contents deployed to cloud.

WLforJupyter loop makes a choice that it should be e.g. an image and does toOut = toOutImage @ evaluationResults

So the trick is to set toOut (1st line) and prevent it from being reset (2nd line). :)

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Nice workaround, although it seems that the number of terms is limited. While this works Sum[c[i],{i,1,7670}], increasing the number to 7675 returns $Failed. Using Shallow helps, though, cf. Shallow[Sum[c[i],{i,1,10000}]]. At the moment I think that your idea is the best thing one can come up with, so I accept it as an answer. $\endgroup$ – vsht Jun 5 '19 at 13:34
  • 1
    $\begingroup$ @vsht does it return $Failed before applying the trick? If so that is probably something to report. $\endgroup$ – Kuba Jun 5 '19 at 14:14
  • $\begingroup$ It seems to be related only to the output (also without your trick), the kernel evaluates the expression just fine. I created an issue on GitHub for that here. I also opened another issue regarding TraditionalForm as the default cell output type, mentioning this question and your workaround. $\endgroup$ – vsht Jun 6 '19 at 16:22
  • $\begingroup$ It’s crazy that gifs don’t work in the cloud $\endgroup$ – M.R. Jun 6 '19 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.