Minimum Working Example
I've defined my own plot function, which does a lot of stuff to the inputs before passing it into the Graphics function. Because of that, I'm using SetDelayed and Module. Here's a MWE. As expected, this produces no output.
myPlot[x_] :=
Module[
{y = x},
Graphics[y]
]
Error
Until now, I've stored this in a notebook which I need to execute every time I open Mathematica. I've recently learned about $Initialization and want to use that so I no longer have to manually execute this definition before using it. $Initialization requires Hold, but as soon as I put Hold around this definition, Graphics tries to execute. I get output with a large red square where Graphics is in the expression and the following error.
Hold[
myPlot[x_] :=
Module[
{y = x},
Graphics[y]
]
]
y is not a Graphics primitive or directive
I've tried numerous variations of Hold and Defer (and others) all throughout the expression, both inside and outside Graphics, and everything gives the same error.
Question
How can I format this function definition with Hold around it so that it won't throw an error and will execute when Mathematica runs ReleaseHold on it?
Holdexpression just formats with a pink box in your notebook. It should work just the same as you expect – Jason B. Mar 20 '23 at 18:17PersistentObjects["*$Initialization"]and found the .wl file created byInitializationValue. It looks like it stored in the file correctly. Looking at the dozen other expressions which I've put in there and are working, it looks how I'd build it by hand if I tried to edit this .wl file directly. However, when I go to use my plot function, I get the red box with error message. I think that whenReleaseHold[$Initialization]is run, the error occurs again and that expression-with-error is what gets stored as my function. – Travis Bemrose Mar 20 '23 at 23:08$Initialization, I've gotten all of them to work but this one. – Travis Bemrose Mar 20 '23 at 23:09https://mathematica.stackexchange.com/questions/219081/auto-loading-a-m-file– Daniel Huber Mar 21 '23 at 09:48Unprotectpattern in my initialization list, both before and after my function definition. Now, when M opens, the pattern stops graphics functions from rendering inside hold functions, but the plot definition is still messed up. I think parallel processing is to blame, where the plot def finishes before the hold pattern def does. – Travis Bemrose Mar 21 '23 at 20:13Hold,Graphics,SetDelayedinteraction, hence the question. (Thanks to Kuba for the other links. This is a duplicate question (of 32296) even though I searched for those before posting.) – Travis Bemrose Mar 21 '23 at 20:15Unprotectpattern in 32296 did the trick. I still had an errantDeferin my definition from my earlier attempts. I've removed that and now it all works. (I don't know how to mark this question as a duplicate.) – Travis Bemrose Mar 21 '23 at 20:24