It has been fixed in version 10.

Every time I open a notebook contains a legend, I get the message warning that it's not safe.

The file contains potentially unsafe dynamic content.

Why does this happen and how to fix it?

enter image description here

  • 1
    $\begingroup$ I think all dynamic content is considered unsafe because it can trigger automatic evaluations when the notebook is opened (which can be abused). It's not that the legend is inherently unsafe, it's that it uses Dynamic. $\endgroup$ – Szabolcs May 21 '14 at 19:58
  • 1
    $\begingroup$ @Szabolcs So you mean plot legend is dynamic? Why does it need to be dynamic? $\endgroup$ – xslittlegrass May 21 '14 at 20:00
  • $\begingroup$ Yes, my point was that the real question is: why does BarLegend need to use Dynamic? I don't know. I'm trying to find out by reading ToBoxes@BarLegend[]. $\endgroup$ – Szabolcs May 21 '14 at 20:01

The reason why this happens is that BarLegend[] has a formatting (MakeBoxes) which relies on dynamic evaluation. Why this is necessary, I do not know, but we can see it this way:


This produces a very long output which contains the following:

DynamicBox[Directive[CurrentValue[{GraphicsBoxOptions, LabelStyle}]]] 

This is the bit that causes the trouble.

My guess is that the inclusion of this dynamic construct is an oversight and might go away from BarLegends in the future ...

All dynamic constructs are considered unsafe by Mathematica. Dynamic elements in notebooks may trigger evaluations immediately as the notebook is opened. They make it possible to construct a malicious notebook that does something nasty as soon as it is opened. Mathematica prevents this by disabling dynamic evaluations in untrusted notebooks.

Read more about this here. The actual documentation notebook has buttons for editing trusted and untrusted paths.

  • $\begingroup$ I'm curious how did you isolated the problem? I tried to look at the FullForm of the plot, and there is no Dynamic in it. $\endgroup$ – xslittlegrass May 21 '14 at 20:35
  • 1
    $\begingroup$ @xslittlegrass From your screenshot it looks like the problem is caused by the legend, not the plot. So first step: look at BarLegend alone, and confirm that it also has the problem when not attached to a plot. Second step: check the FullForm/InputForm of BarLegend[] and discover that it is simply BarLegend[]. Third step: look at how BarLegend is formatted in StandardForm. The simplest way is to use ToBoxes to convert it to boxes. You might also reveal the structure of the output cell using Cmd-Shift-E, but that's messier. Step four: Cmd-F, search for "dynamic". $\endgroup$ – Szabolcs May 21 '14 at 20:38
  • $\begingroup$ Thanks, that's very helpful! But I tried to look at the full form of the whole thing. p = DensityPlot[Sin[x] Sin[y], {x, -4, 4}, {y, -3, 3}, ColorFunction -> "SunsetColors", PlotLegends -> Automatic]; and then FullForm[p], there is no Dynamics in it. So what's the name for the form that Cmd-Shift-E showed me, and what are the differences compared to InputForm? $\endgroup$ – xslittlegrass May 21 '14 at 21:00
  • $\begingroup$ @xslittlegrass The full form of that looks like Legended[(your plot), BarLegend[...]]. The key is BarLegend. FullForm will show you the internal representation of an expression. Expressions get formatted when displayed in a notebook: they get translated into boxes. These boxes may be considerably more complicated than the full form of the expression. In some cases (especially dynamic-related things like Manipulate) the main functionality is contained in the boxes generated as the standard form of the expression. This is because dynamic things are handled by the front end ... $\endgroup$ – Szabolcs May 21 '14 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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