Having generated a list of plots, such as

plot01 = Plot[Sin[x], {x, 0, 6}];
plot02 = Plot[Cos[x], {x, 0, 6}];
plotList =  {plot01, plot02}

Then create strings with the name of each plot. The following works for the individual plot names

plot01Name = ToString[HoldForm[plot01]] 
plot01Name // FullForm 
(* "plot01" *)

but mapping this to the list doesn't work

plotListNames = Map[ToString[HoldForm[#]] &, plotList ];
plotListNames // FullForm
(* List["-Graphics-", "-Graphics-"] *)

Looking for an efficient way to create the list of plot names, so that the names can be used to Export the plots. The name of the exported plot should be similar to the name of the symbol chosen for the plot.


From the comments by @b3m2a1, the following produces the desired result.

plot01 = Plot[Sin[x], {x, 0, 6}];
plot02 = Plot[Cos[x], {x, 0, 6}];
plotListHold =  Thread@HoldForm[{plot01, plot02}];
Export[ ToString[#] ~~ ".jpg", ReleaseHold[#] ] & /@ plotListHold
  • $\begingroup$ You need plotList = Thread@HoldForm[{plot01, plot02}] then Map[ToString, plotList] will work $\endgroup$
    – b3m2a1
    Apr 13, 2020 at 3:36
  • $\begingroup$ Thanks! that gives a list of Strings, which can be used in Export. And then to use the plotList to display the plots (not for Export), something like this? GraphicsRow[ReleaseHold[plotList]] $\endgroup$
    – user6546
    Apr 13, 2020 at 3:54

1 Answer 1


Cross posted from https://community.wolfram.com/groups/-/m/t/1984979

I would take a different approach. HoldForm is difficult to control and it can have strange side effects. I would use notebook programming to grab the input cell and manipulate it. I think it will be more reliable:

First, define a function that will take an input string and parse it, and save the file using the filename. For example, "p1 = Plot[Cos[x], {x, 0, 6}]" gets saved by making a jpg from p1 and calling the file "Plot[Cos[x], {x, 0, 6}].jpg"

saveFile[x_] := 
 Module[{lst}, lst = StringSplit[x, "="]; 
  Export[lst[[2]] <> ".jpg", ToExpression[lst[[1]]], "jpg"]]

Next, make a function to grab the previous input line and send it to saveFile[]: (Note that most of the code below is so that the result is inserted after the existing cell. It makes for a cleaner notebook. If you do not care, many of the lines in this function go away and the save is inserted before the plot)

saveIt2[] := 
 Module[{nb = InputNotebook[], currentCell}, 
  SelectionMove[nb, Previous, CellContents, 3]; (*  go back to the previous input cell *); 
  currentCell = NotebookRead[nb];  (* save the cell *);
  SelectionMove[nb, Next, Cell, 2];   (*  move back down selecting my cell *); 
  SelectionMove[nb, After, Cell];   (* move after my cell *); 
  NotebookWrite[nb, currentCell];  (*   insert the contents of the saved cell *); 
  SelectionMove[nb, All, Cell];   (* select the inserted cell *); 
   "saveFile[ToString[\"\[SelectionPlaceholder]\"]]"];   (*  insert the save function *);
  SelectionEvaluateCreateCell[nb] (*  evaluate the new cell and save the file *) ]

To use the code:

p1 = Plot[Cos[x], {x, 0, 6}]

This will go and create a new cell with the save function and export the plot. I suppose an upgrade would be to create a function or button that deletes all the saveFile cells when complete so you can reevaluate the notebook without side effects.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.