3

Being a long time unix shell person, I prefer Mathematica's command line interface, over the notebook. (command history, etc...)

It just learned that on OS X, the cli is available as MathKernel.

What I'm wondering now is, is it possible to open graphics (and/or animations, sounds, etc) result, from within the command line?

In[16]:= Plot[Sin[x], {x, -Pi, Pi}]

Out[16]= -Graphics-

I would like to be able to view output #16.

billc
  • 684
  • 5
  • 16
  • You need to load <<JavaGraphics`. See also my answer here (that question and title should probably rewritten) – rm -rf May 21 '14 at 04:20
  • @rm-rf Strangely, when using <<Terminal`, I get garbage output on OS X. – Szabolcs May 21 '14 at 14:03
  • @Szabolcs Terminal is for ASCII graphics and it has worked for me – rm -rf May 21 '14 at 14:06
  • @rm-rf I thought it has worked for me too, but I'm getting this now. Have you tried on Mavericks? – Szabolcs May 21 '14 at 14:14
  • @Szabolcs You're right, that's what I get too on Mavericks. Nothing comes to mind immediately as to what could be wrong... – rm -rf May 21 '14 at 15:37
  • @rm-rf, sorry, didn't see your comment before I answered. My bad. – Rico Picone May 21 '14 at 18:12
  • @Szabolcs same output here on Mavericks 10.9.3. – shrx May 21 '14 at 19:05
  • You are going to lose a lot by sticking to the command line interface though. You'll probably end up getting frustrated and concluding that Mathematica is not very good. I'd advise you to learn to use the notebook interface properly instead. Not only is it the proper way to use Mathematica (it's desgined to be used this way), I personally find it far superior to any command line interface when doing interactive scientific computing. What you're saying sounds like this to me: "I'm used to working with screwdrivers, so I want a screwdriver with a tough handle that can hammer in this nail." – Szabolcs May 21 '14 at 19:24

2 Answers2

5

If you are using a windowing system, the following before evaluating a graphic should work.

<<JavaGraphics`
Rico Picone
  • 885
  • 1
  • 5
  • 12
2

You must use a FrontEnd to generate actual images, but you don't have to use the notebook interface. You can actually do all of this from a standalone kernel. (You must be logged in to a system that allows you to launch GUI applications, though.)

For example:

In[5]:= g=Plot[Sin[x],{x,-Pi,Pi}];                                              

In[6]:= Export["testplot.png",g]                                                

Out[6]= testplot.png

In[7]:= UsingFrontEnd[SystemOpen["testplot.png"]]

You first create the object you want to see (in this case expression g). Then you pass it to Export. Export will launch the FrontEnd as a service to render the expression, and then the Kernel will convert it to the format specified in Export and save it to disk. You can then view it using SystemOpen. This technique will also work with animations (you will need to use an export format like QuickTime).

  • Thanks to everyone who answered my question. This, and the info about <<JavaGraphics\`` and<<Terminal`` are exactly what I am looking for. (fwiw, I also get noise with terminal graphics under Mavericks, but I feel like that can probably be sorted out.) – billc May 21 '14 at 17:00
  • @ChristopherCole Just curious: why does the kernel have to launch the FrontEnd to export? (I assume that's just images?) It seems odd that if we are writing data to a file that the kernel couldn't do that by itself. – mfvonh May 21 '14 at 17:32
  • Ultimately, you need something to render Graphics[...directives, primitives, and options...]. The FrontEnd isn't the only program that knows how to do this; `JavaGraphics`` in the Kernel can render those primitives and directives it understands. However, since V6, Graphics, like formatted text and equations, are really box expressions and therefore subject to styles and style inheritance. The FrontEnd knows how to resolve these styles, so asking the FrontEnd to perform the render means that the exported graphic will have nearly the same appearance as in a Notebook. – Christopher Cole May 21 '14 at 18:42