1

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?

Travis Bemrose
  • 450
  • 2
  • 12
  • I don't think this is a problem - it isn't really throwing an error, the Hold expression 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:17
  • yeah, I think this is just a display thing. The front end is trying to display the Graphics expression. Hold is keeping it from being evaluated, but the front end sees that Graphics expression and wants to display it. At least, that's my guess. I don't see any actual problem going on with the Hold expression itself. – lericr Mar 20 '23 at 18:36
  • A better way to define an always available user function is to put the definition into the "init.m" file – Daniel Huber Mar 20 '23 at 20:29
  • It doesn't work. I've done PersistentObjects["*$Initialization"] and found the .wl file created by InitializationValue. 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 when ReleaseHold[$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
  • @DanielHuber I tried using "init.m" first, but couldn't get it to work for any of the initializations I want. Using $Initialization, I've gotten all of them to work but this one. – Travis Bemrose Mar 20 '23 at 23:09
  • I was confused once as well: 114305, does this topic answer your question? – Kuba Mar 21 '23 at 07:26
  • 1
    Search the web for how to implement autoloading a file with your own definitions. E.g.: https://mathematica.stackexchange.com/questions/219081/auto-loading-a-m-file – Daniel Huber Mar 21 '23 at 09:48
  • @Kuba That helps. It points to 32296 which is a good lead. I now understand what's going on and why, but that solution didn't work. I put the Unprotect pattern 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:13
  • @DanielHuber I do need to learn that, but I also needed to understand the Hold, Graphics, SetDelayed interaction, 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:15
  • @Kuba Actually, the Unprotect pattern in 32296 did the trick. I still had an errant Defer in 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

0 Answers0