Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
1  
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. –  Szabolcs May 21 at 19:58
1  
@Szabolcs So you mean plot legend is dynamic? Why does it need to be dynamic? –  xslittlegrass May 21 at 20:00
    
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[]. –  Szabolcs May 21 at 20:01

1 Answer 1

up vote 12 down vote accepted

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:

ToBoxes@BarLegend[]

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.

share|improve this answer
    
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. –  xslittlegrass May 21 at 20:35
1  
@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". –  Szabolcs May 21 at 20:38
    
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? –  xslittlegrass May 21 at 21:00
    
@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 ... –  Szabolcs May 21 at 21:08
    
Thanks, I learn a lot :) –  xslittlegrass May 21 at 21:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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