Why are all the lines commented when I create a Mathematica script?



When I created a Mathematica script, I found that all my lines were commented out (when opened with vi or gedit or any other text editor). So I either need to get rid of the (**) manually or through a bash script before I run my Mathematica script from a unix terminal.

To create my script, I include the shebang #!/usr/local/bin/MathematicaScript -script on the first line and then save my file as a .m package. The resulting file looks something like this:

(* ::Package:: *)

(* ::Input:: *)

(* ::Input:: *)
(*f[x_] := x*)

Why are my lines commented? Is there anything I need to do?


Posted 2012-08-27T17:29:52.400

Reputation: 5 283



The reason why all the lines are commented is because you are using the frontend to create your script in input cells and saving it as a .m file (equivalent to choosing "Mathematica Package" in the Save As dialog).

Now to create a Mathematica package file (or .m file), the code needs to be in initialization cells or code cells. Content in any other cell is commented out. So the change you need to make is to select your input cells and choose Cell > Cell Properties > Initialization Cell or enter your code in a code cell (Cmd8 on a mac). If you save this file, you'll find the code present uncommented in the saved file.

rm -rf

Posted 2012-08-27T17:29:52.400

Reputation: 85 395

I normally use a notebook to create a package using a template from Wellin. As stated previously one sets the cells desired in the xxx.m file to have the Initialization property. I only save the xxx.nb notebook. The associated xxx.m file can automatically be saved if you go to the Options Inspector and select the current notebook. Then set Notebook Options -> File Options -> AutoGeneratePackage to Automatic. This helps greatly to keep the notebook and package in sync. – Jack LaVigne – 2015-08-23T14:44:28.560

@JackLaVigne Your last comment is great!!! Now I can safely working with notebook but always have my .m file up-to-date. I have a confusion, the code/initialization cell can be excuted in the notebook interface(I didn't know this until I see this post). What's the benefits of using input cell? It seems that everything done by input cell can be done with code cell, and code cell can save .m file properly. – an offer can't refuse – 2015-12-30T04:12:40.103

@buzhidao Because input cells are not exported, you can use them in your notebook to do scratch work, create examples which are not part of the package, etc. – rm -rf – 2015-12-30T04:24:34.857

great to know this. – an offer can't refuse – 2015-12-30T04:51:30.677

Oh my! Thats a nuance I was not aware of. Will try it at once. – dearN – 2012-08-27T17:57:42.273

What do you know! Your suggestion worked! Thanks. – dearN – 2012-08-27T18:02:33.000

@DNA: It's a blessing in that you can leave all sorts of other stuff -- especially text cells, Section, etc., cells -- for purposes of documentation, test examples, etc., in the source .nb notebook, then just save the notebook as a .m package. – murray – 2012-08-27T22:15:04.657

@DNA or you could switch over to working in the .m package directly.

– Yves Klett – 2012-08-28T05:03:37.613

@YvesKlett Yes, but having the ability to run .m files from the command line is fantastic and helps with running code while one is asleep! :P – dearN – 2012-08-28T13:40:36.497

2@DNA Yves meant that you could also use the front end's package editor to create the .m files. You can get to it by going to File > New > Package or if you open a .m file in Mathematica (notice it looks different from your usual front end notebook). This has nothing to do with how the file is run – rm -rf – 2012-08-28T14:18:16.187

@R.M thanks for the precognition ;-) – Yves Klett – 2012-08-28T16:24:38.957

@YvesKlett Oh! Thanks! – dearN – 2012-08-28T19:37:21.390

@R.M Oh! Thanks! – dearN – 2012-08-28T19:37:47.833