I’m learning the NotebookWrite[] interface and have hit a wall on inserting images.
What I’ve done is a script that generates a map that I then want to insert the image that results from the code into the daily notebook, but everything I’ve tried thus far instead inserts the raw code, not the map image.
You have to convert the expression you want to display to boxes. MakeBoxes[] is HoldAllComplete, so you have to evaluate the code that produces the image (even if the “code” is simply a variable) before passing the resulting image to MakeBoxes. You can use With[] instead of a pure function, if you prefer.
When trying to figure this stuff, the menu command Cell > Show Expression can be helpful, in case you hadn’t run across it yet. You can use it to see how the system creates the cells you type and compute and mimic it.
Sorry about the []. It wasn’t in the code I pasted. Probably it got there when editing. For some reason, the mouse clicks are mapped to the wrong location when I’m using the editor. Sometimes I don’t see where I’ve typed. Anyway I took it out.
Works perfectly for me. Don’t know why it wouldn’t work for you.
I commented out the second line. It’s not necessary and may be a distraction. Works either way for me anyway.
As I understand your reply, nothing is written into nb by the last NotebookWrite[], which seems strange. And no error (I suppose).
Okay, the following worked, although the graphic size was rather small (I’ll need to figure out the sizing). I guess I need to wrap larger code in Module or something…
nb = CreateDocument[];
GeoGraphics[GeoMarker[Here]] //
NotebookWrite[nb,
Cell[BoxData[MakeBoxes[#, StandardForm]], "Output"]] &
Great. For me, the created notebook has an image a few pixels larger than the standard output of GeoGraphics[GeoMarker[Here]]. In any case, there’s the ImageSize option you can use to control the size of the output image.