Functions can be used in stylesheets but not defined as per:
myColor = RGBColor[.5,.5,.5];
The reason you cannot, for example, enter Red
in a stylesheet is because it is typically written in the underlying box code. By analogy you cannot choose show expression and replace RGBColor[1.,0.,0.]
in the underlying expression with Red
.
But as @Mr.Wizard said this can be done programatically. For example you can manipulate your current notebooks stylesheet with this sort of code:
SetOptions[EvaluationNotebook[],
StyleDefinitions -> Notebook[
{Cell[StyleData[StyleDefinitions -> "Default.nb"]],
Cell[StyleData["Input"], FontColor -> myColor]},
StyleDefinitions -> "PrivateStylesheetFormatting.nb"
]
]
...in which you can enter a named color. You can change the stylesheet dynamically:
Manipulate[
(SetOptions[EvaluationNotebook[],
StyleDefinitions -> Notebook[
{Cell[StyleData[StyleDefinitions -> "Default.nb"]],
Cell[StyleData["Input"], FontColor -> myColor]},
StyleDefinitions -> "PrivateStylesheetFormatting.nb"
]
];
Style["Input Colour", 18, myColor]),
{myColor, Red}
]
By combining code for "Input", comments, strings and so on you can dynamically compare combinations of font and background colours to create a stylesheet that you personally find aesthetically pleasing.
(* a comment *)
Manipulate[
(SetOptions[EvaluationNotebook[],
StyleDefinitions -> Notebook[
{Cell[StyleData[StyleDefinitions -> "Default.nb"]],
Cell[StyleData["Input"],
Background -> background,
FontSize -> size,
FontColor -> myColor,
AutoStyleOptions -> {
"CommentStyle" -> {FontColor -> commentcolor, FontSize -> 11,
FontFamily -> "Comic Sans MS", ShowAutoStyles -> False,
ShowSyntaxStyles -> False, AutoNumberFormatting -> False},
"StringStyle" -> {FontColor -> stringcolor, FontSize -> 11,
FontFamily -> "Century Gothic", ShowAutoStyles -> False,
ShowSyntaxStyles -> False, AutoNumberFormatting -> False}}]},
StyleDefinitions -> "PrivateStylesheetFormatting.nb"
]
];
Style["Input Colour", 18, myColor]),
{myColor, Black},
{commentcolor, Purple},
{stringcolor, Brown},
{background, White},
{{size, 12, "size"}, 8, 20, 1}
]
Edit
re: questions from @R.M and @jmlopez
"Do you also know if it is possible to copy all the definitions from a stylesheet to the private stylesheet?"
I'm not actually sure what this question means. Stylesheet settings are inherited. The first cell in a private stylesheet
Cell[StyleData[StyleDefinitions -> "Default.nb"]
means the stylsheet is taking its styles from the default stylesheet. So you don't have to copy over any styles. If your private stylesheet is based on one of the nested stylesheets you would insert e.g.
StyleData[
StyleDefinitions ->
FrontEnd`FileName[{"Creative"}, "NaturalColor.nb"]]
...and so on. If you want to define multiple styles then
SetOptions[EvaluationNotebook[],
StyleDefinitions -> Notebook[
{Cell[StyleData[StyleDefinitions -> "Default.nb"]],
Cell[StyleData["Input"],
...
]
Cell[StyleData["Output"],
...
]
Cell[StyleData["Text"],
...
] etc.
If you want to create a stand alone stylesheet programmatically then
nb = CreateDocument[
Notebook[{Cell[StyleData[StyleDefinitions -> "Default.nb"]],
Cell[StyleData["Input"], FontColor -> myColor]},
StyleDefinitions -> "PrivateStylesheetFormatting.nb"],
WindowSize -> All];
NotebookSave[nb, "mystylesheet.nb"]
You can now distribute "mystylesheet.nb"
Blue
) in stylesheets $\endgroup$Red
and other defined colors or functions in the stylesheets. Too bad we can't define our own. Or can ... ALL GLORY TO THE HYPNOTOAD!!! $\endgroup$Red
or any other named colour (AFAIK). See the difference by addingCell[StyleData["Input"], Background -> RGBColor[1,0,0]]
andCell[StyleData["Input"], Background -> Red]
(one at a time) to the stylesheet $\endgroup$RGBColor[1,0,0]
but notRed
. What are the limits of built-in expressions we can use in the making of stylesheets? $\endgroup$RGBColor[r,g,b]
can be interpreted simply by parsing the expression, whereasBlue
requires it to be evaluated or itsOwnValues
read to get the rgb values. $\endgroup$