1
$\begingroup$

My question is virtually the same as Saving a notebook as PDF, preserving syntax highlighting except that I'd like to save my file (a tutorial on Mathematica) as a .CDF file and thus keep the syntax highlighting for obvious reasons.

As Kuba mentioned in a comment, syntax highlighting works fine when opening a .CDF file in Mathematica; the same can not be said for the free CDF player (Version 10.4)

$\endgroup$
1
  • $\begingroup$ @Kuba I just noticed that there is a difference depending on wether you open a .cdf in Mathematica or the free CDF player. I will add that to my question $\endgroup$
    – Sascha
    Commented Aug 16, 2016 at 11:42

1 Answer 1

3
$\begingroup$

TL;DR; with a minimal example

Don't know how to make coloring in e.g. Plot work (Module and friends work ok).

Don't know how to make coloring of System` and other contexts different.

Otherwise:

With[{autoSO = Options[$FrontEnd, AutoStyleOptions]},
 DynamicModule[{},
  ExpressionCell[
   Defer@Module[{n = 1}, 
     Plot[Sin[x/n], {x, 0, 1}, PlotLabel -> "Label"]], "Notebook", 
   "Input"]
  ,
  Initialization :> (
    CurrentValue[$FrontEndSession, ShowSyntaxStyles] = True;
    SetOptions[$FrontEndSession, autoSO];
    CurrentValue[$FrontEndSession, {AutoStyleOptions, 
       "UndefinedSymbolStyle"}] = {FontColor -> Black};

    )
  ]
 ]

enter image description here

You can put in in NotebookDynamicExpression, Initialization, or in a Button to trigger it only when needed.


Details

   (*notebook we will deploy as cdf*)

Checkbox[Dynamic@CurrentValue[$FrontEndSession, ShowSyntaxStyles]]

With[{autoSO = Options[$FrontEnd, AutoStyleOptions]},
 Button["Get MMA's colors", SetOptions[$FrontEndSession, autoSO]]
 ]

Button["Fix System` colors",
 CurrentValue[$FrontEndSession, {AutoStyleOptions, 
    "UndefinedSymbolStyle"}] = {FontColor -> Black}
 ]

GlobalVariable = "String"

Plot[Sin[x], {x, 0, 1}, PlotLabel -> "Label"]

enter image description here

I though that we can't do much because after copying Default and Core stylesheets to appropriate Player's directories nothing changed.

But there are couple of steps which will lead us to something useful.

  • ShowSyntaxStyles options is special because its value for a $FrontEnd* has higher precedence than Stylesheet's or Notebook's. So we can set it manually for a $FrontEnd* then.

    enter image description here

    Better! Not perfect though, everything is blue and has default styles one may have tweaked in Mathematica.

  • We can inherit custom styles (like Strings' here) by injecting MMA $FrontEnd settings and applying them in a CDF, e.g.:

     With[{autoSO = Options[$FrontEnd, AutoStyleOptions]},
          Button["Get MMA's colors", SetOptions[$FrontEndSession, autoSO]]
     ]
    

    after clicking:

    enter image description here

    Better, but still the problem with blue symbols remain

  • I thought it can be fixed with

     AutoStyleOptions -> "SymbolContextStyles" -> {
         "System`" -> {}, "Global`" -> {}, Automatic -> {}
     }
    

    which we can use to e.g.

     Button["Fix System` colors",
       CurrentValue[$FrontEndSession, {AutoStyleOptions, 
        "SymbolContextStyles", Automatic}] = {FontColor -> Black}
     ]
    

    but it is not the case, $FrontEnd in the Player doesn't know that Plot is a System`'s symbol. Even if we use:

     FrontEndExecute @ FrontEnd`Private`GetUpdatedSymbolContexts[];
    

    Update/refresh notebook variable color on clear

    So at the end I think it is ok to change color of undefined symbols to black, the lesser evil:

      CurrentValue[$FrontEndSession, {AutoStyleOptions, 
        "UndefinedSymbolStyle"}] = {FontColor -> Black}
    

    enter image description here

$\endgroup$

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.