7
$\begingroup$

I find the old stylesheet UI clunky and hard to use:

FrontEndExecute @ FrontEndToken @ "EditStyleDefinitions"

enter image description here

There's also the clunky style previewer as well:

enter image description here

Without using the above, I'd like to programmatically quickly tweak and preview styled expressions and definitions for named styles. For example, let's try copying "Entity" and renaming it "MyRoundedFrameStyle" giving it different colors and frame properties:

CellPrint @ Cell[BoxData[
    TemplateBox[{"\"United States\"", , , "country"}, "Entity"]], 
  "Input"]

enter image description here

What I've tried:

Here's some code to get started:

DynamicModule[{x = ""}, TextGrid[{{"Input", "Output"}, 
    {InputField[Dynamic[x], String, ContinuousAction -> True],           
    Dynamic[DisplayForm[Check[DisplayForm@ToExpression[x], "-"]]]}}, 
   Alignment -> Center, Dividers -> All]]

Obviously, it doesn't work well:

enter image description here

Notes:

  • Being able to do this inline and programmatically within a notebook would "be best" ;)
  • I haven't seen any ResourceFunction that could help here...
  • Perhaps there are some undocumented symbols that let one ergonomically copy-and-tweak (predefined cell or box) styles for quick reuse?
  • If nothing like this exists, StylePlayground would be a good name!
$\endgroup$
6
  • $\begingroup$ to view the style definition for the style "stylename" you can use CurrentValue[{StyleDefinitions,"stylename"}] $\endgroup$ – kglr Jan 7 '20 at 19:17
  • 1
    $\begingroup$ I’ve got code that does this which I use to develop my stylesheet. Basically you can find the stylesheet notebook for the notebook of interest in NotebookInformation and then you can use standard notebook programming to modify style definitions. After you’ve modified you then need to use the "ToggleShowExpression" token on the cells of interest to get the changes to load. $\endgroup$ – b3m2a1 Jan 8 '20 at 17:08
  • $\begingroup$ That could work, would be nice to see changes live somehow tho $\endgroup$ – M.R. Jan 8 '20 at 19:40
  • 1
    $\begingroup$ It is relatively straight forward to build your own dynamic style creator as per: mathematica.stackexchange.com/questions/7738/… $\endgroup$ – Mike Honeychurch Jan 8 '20 at 22:25
  • $\begingroup$ @M.R. I just create a test-notebook that links to my stylesheet and play with it there. I can give an example. $\endgroup$ – b3m2a1 Jan 13 '20 at 4:14
3
$\begingroup$

Here's my preferred way to do this. First we'll make some template:

Options[createPreviewNotebookTemplate] =
    {
       "Styles" -> {
             "Title", "Subtitle", "Chapter",
             "Section", "Subsection", "Subsubsection",
             "Text", "Code", "Input", "Output",
             "Item", "ItemNumbered", "ItemParagraph",
             "Subitem", "SubitemNumbered", "SubitemParagraph",
             "InlineFormula", "DisplayFormula", "Program"
           },
       "StyleContentMap" -> {
           "Code" -> BoxData, 
           "Input" -> BoxData, 
           "Output" -> BoxData,
           _ -> Identity
           }
       };
createPreviewNotebookTemplate[ops : OptionsPattern[]] :=

 With[{map = OptionValue["StyleContentMap"]},
      Notebook[
         Map[Cell[(# /. map)[#], #] &, OptionValue["Styles"]]
         ]
      ]

nb = CreateDocument@createPreviewNotebookTemplate[]

enter image description here

Then you can load a package I've had on my GitHub for a while and play with the styling:

Get["https://raw.githubusercontent.com/b3m2a1/mathematica-tools/master/StylesheetEdit.wl"]

(*StyleSheetNew[nb, "Title"]*) (* you only want this one time, before you start adding edits *)
StyleSheetEdit[nb, "Title",
 {
   FontColor -> Pink
  }
 ]

(*StyleSheetNew[nb, "Notebook"]*)
StyleSheetEdit[nb, "Notebook",
 {
   Background -> GrayLevel[.9]
  }
 ]

You can also revert edits:

StyleSheetDelete[nb, "Notebook"]

This is affecting the actual stylesheet notebook that nb uses, not continuously writing to StyleDefinitions. If nb were a stylesheet notebook in and of itself the changes will be applied to all notebooks that depend on that sheet.

E.g. we can get the sheet for a given notebook via:

defaultSheet =
 Lookup[NotebookInformation[EvaluationNotebook[]], "StyleDefinitions"][[1]]

Then when I do:

StyleSheetEdit[defaultSheet, "Notebook",
 {
   Background -> GrayLevel[.9]
  }
 ]

every notebook that uses "Default.nb" will get a gray background.

We can revert that without deleting the cell by

StyleSheetEdit[defaultSheet, "Notebook",
 {
   Background -> Inherited
  }
 ]
$\endgroup$

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.