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?
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]];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With