13

I'd like to make some features of my notebooks responsive to some properties of styles in the active stylesheet. Is there a way to do this? I can't seem to get anything out of StyleData. What I'm looking for is something of the form

StyleData["SomeStyle", "FontName"]
StyleData["SomeStyle", "FontSize"]

etc.

Are there functions or settings I can use to programmatically get information about properties of a style?

orome
  • 12,819
  • 3
  • 52
  • 100
  • 1
    Open the stylesheet (possibly Default.nb). If necessary, choose a style from the "Choose a style" drop-down menu. Select the now-displayed cell named after that style. Now use the Option Inspector, either browsing for what you want in the outline hierarchy or else by using the Search box in the Option Inspector. – murray Nov 13 '12 at 21:59
  • 1
    @murray: How can I do that in code? (See edit.) – orome Nov 13 '12 at 22:08

4 Answers4

14

You need

 CurrentValue[{StyleDefinitions, stylename}]

Examples:

 CurrentValue[{StyleDefinitions, "Section"}]

enter image description here

 CurrentValue[{StyleDefinitions, "Section", "CellFrame"}]
 (* {{0, 0}, {0, 1}} *)
 CurrentValue[{StyleDefinitions, "Subsection", "FontFamily"}]
 (* "Helvetica" *)
 CurrentValue[{StyleDefinitions, "Graphics", "CapForm"}]
 (* "Square" *)

etc.

kglr
  • 394,356
  • 18
  • 477
  • 896
  • Hey, do you know how to get the info for a particular screen environment such as printout? – Rojo Feb 16 '13 at 22:24
  • It seems a particular option you can get with CurrentValue[notebook, {StyleDefinitions, "style/Printout", theOption}], but I still haven't found how to get the whole list – Rojo Feb 16 '13 at 22:31
  • Hi @Rojo, i don't know how to get the definitions for combo styles. I would expect/wish that using the stylenames (like "Section/SlideShow" or StyleData["Section", "SlideShow"]) would work; but it doesn't (ver 9 on windows vista). And, the option values returned by CurrentValue[notebook, {StyleDefinitions, "style/Printout", theOption}] do not match the defined/inherited values of these options based on the definitions in Default.nb. For example, CurrentValue[{StyleDefinitions, "Section/xx", "FontSize"}] gives 14 for xx in {"PrintOut","Condensed","Working","SlideShow"}. – kglr Feb 17 '13 at 01:07
  • That's sad. Thanks! – Rojo Feb 17 '13 at 06:51
  • It is possible to use CurrentValue to get some property of a style of the same stylesheet I'm defining? I.e. if I want to use the same FontColor many times and I don't want to write it explicitely all times? I tried but I get a wrong color... Thanks – unlikely Mar 04 '16 at 16:59
4

This is just an addendum concerning screen environments. To obtain all the styles associated with an environment use a list as in:

CurrentValue[EvaluationNotebook[], {StyleDefinitions, {"Text", "Printout"}}]

{CellMargins -> {{49, Inherited}, {Inherited, Inherited}}, Hyphenation -> True, LineSpacing -> {1, 3}, TabSpacings -> {2.5}, CounterIncrements -> "Text", MenuSortingValue -> 1400, MenuCommandKey -> "7", FontFamily -> "Source Sans Pro", FontSize -> 15}

or to obtain a specific setting:

CurrentValue[EvaluationNotebook[], {StyleDefinitions, {"Section", "SlideShow"}, FontSize}]

40

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
1

(Very incomplete answer!)

Open the style sheet, e.g.:

nb = NotebookOpen[
    "/Applications/Mathematica.app/SystemFiles/FrontEnd/StyleSheets/\
     Creative/PastelColor.nb"]

Find what you want, e.g., style for Section:

NotebookFind[nb, "Styles for Title and Section Cells"]
NotebookFind[nb, "Section"]
SelectionMove[nb, Next, Cell]

Read it:

properties = NotebookRead[nb]

Of course to use my method, you already need to know in detail the structure of the style sheet.

murray
  • 11,888
  • 2
  • 26
  • 50
0

Carl Woll's answer using CurrentValue is definitely the way to go once you know exactly what properties you want, and you want to do so programmatically.

To expand upon murray's answer using NotebookRead, and to perhaps provide an answer directed towards the title of your post, here is a method that uses NotebookRead to make a "handy dictionary" of all properties in a stylesheet, that you can then search-through using normal Mathematica search features:

FormatStyleSheet = With[
    {
     defaultNBData = 
      First@Through@{NotebookRead@*Cells, NotebookClose}@
         NotebookOpen[
          FileNameJoin[
           {
            $InstallationDirectory,
            "SystemFiles",
            "FrontEnd",
            "StyleSheets",
            #
            }
           ],
          Visible -> False],
     Col = Column[#,
        Background -> {{LightBlue, LightYellow}},
        BaseStyle -> "Input",
        Alignment -> {Left, Center},
        ItemSize -> Scaled@1] &,
     CellsToNB = CreateDocument@*Cell@*CellGroupData,
     CloseGroups = Function[nb,
       SelectionMove[nb, All, Notebook];
       FrontEndExecute@FrontEndToken@"SelectionCloseAllGroups";
       SelectionMove[nb, Before, Cell]]
     },
CloseGroups@CellsToNB@ReplaceAll[
    c : 
      Cell[StyleData[___, OptionsPattern@StyleData], 
       definitions___] :>
     Sequence[c, Cell@BoxData@ToBoxes@Col@{
           StyleData -> OptionValue[StyleData, StyleDefinitions],
           definitions}]
    ]@defaultNBData
] &;

Usage:

FormatStyleSheet@"Default.nb"
FormatStyleSheet@"Core.nb"

Example output:

Example output

You can use the methodology in the above code (namely, NotebookRead) to get the values you want programmatically, of course, too. But I think Carl Woll's method is more direct for that.

Sean
  • 645
  • 4
  • 10