TransWikia.com

Rendering graphics: UsingFrontEnd vs using a front end

Mathematica Asked by srossd on April 29, 2021

I’m trying to understand how Graphics expressions are rendered. I create an invisible notebook, write a cell and evaluate it, then rasterize the notebook.

UsingFrontEnd[
nb = CreateNotebook[Visible->False];
NotebookWrite[nb,Cell["Graphics[Disk[{0,0}]]","Input"]];
NotebookEvaluate[nb,InsertResults->True];
Export["test_out.jpg",Rasterize[nb]];
]

If I open Mathematica and run this line, the output is as expected:

Graphics renders

But if I run this from the terminal (using math -script myscript.m), the Graphics object doesn’t render in the output cell.

Graphics doesn't render

Is there any way to change this behavior, so that I get the same result running from within Mathematica or from the terminal? I’m running 12.1, if that’s important. I’m going to try to work around this using Export, but for some parts of my application I really do need Rasterize, so it would be convenient if I could use it in all instances.

One Answer

Where's the difference?

In a command line mode, evaluators default to use InputForm(raw wolfram language expressions), rather than StandardForm (which MakeBoxes).

Effective (but not canonical) solution

I can't find any way to specify the default form for evalaution yet. But we can make the boxes on our own.

Replace the Cell expression in your code to:

Cell["Graphics[Disk[{0,0}]]","Input",CellEvaluationFunction->(MakeBoxes@@MakeExpression[#,StandardForm]&)]

Then the code will work for the command line environment.

Understand it better

To reproduce the problem in the notebook environment, use such a CellEvaluationFunction: (#1&).

Additional advice

The kernel can do MakeBoxes and Export of Graphics, so UsingFrontEnd is not essential for most rendering requirements. $DisplayFunction may help.

Correct answer by bcegkmqs23 on April 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP