Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Save[] definitions associated to a symbol in Mathematica without saving subsidiary definitions?

The built-in Mathematica command Save[file, symbol] uses FullDefinition[] to look up the definition symbol and all of the subsidiary definitions.

For example, the commands

a:=b
c:=2a+b
Save[ToFileName[NotebookDirectory[],"test.dat"],c]

produces the file test.dat containing

c := 2*a + b
a := b

I have a program with a lot of prettifying MakeBoxes type definitions that I do not want to be saved when I Save[] the many separate results.

In terms of the simple example above, I do not want the a := b definition saved to the file. Does anyone know a neat way to make this happen?

like image 807
Simon Avatar asked Dec 16 '22 20:12

Simon


1 Answers

According to the documentation, Save uses FullDefinition while what you want is for it to use Definition. Using a Block we can override the global definition of any symbol, and in particular replace FullDefinition with Definition while running Save:

Block[{FullDefinition},
  FullDefinition = Definition;
  Save[filename, c]
  ];
FilePrint[filename]
DeleteFile[filename]

The magic works:

c := 2*a + b

EDIT. Wrapping things up with the right attributes:

SetAttributes[truncatedSave, HoldRest]
truncatedSave[filename_, args__] := Block[{FullDefinition},
   FullDefinition = Definition;
   Save[filename, args]];
like image 130
Janus Avatar answered Jan 18 '23 23:01

Janus