3
$\begingroup$

Typically I have several vastly different versions of Mathematica installed on the same machine. Specifically, currently I have installed:

Mathematica 5.2

Mathematica 8.0.4

Mathematica 10.4.1

Mathematica 11.0.1

Mathematica 11.1.1

The built-in stylesheets from different versions aren't compatible with each other what prevents me from customizing the default appearance of Notebooks, Documentation etc. by changing the corresponding built-in stylesheet and putting it in my $UserBaseDirectory. I know I can modify the stylesheet in $InstallationDirectory, but I prefer do not do that because I wish to keep the ability to easily rollback to the default stylesheet simply by deleting/renaming a file in my $UserBaseDirectory. Doing this way I also can be sure that my changes won't affect other users of the computer (who log in under their own accounts).

Is there a way to have versioned built-in stylesheets in $UserBaseDirectory? For example, how is it possible to have a modified "Reference.nb" stylesheet which will be used only by version 11.1 and not by other versions of Mathematica?

$\endgroup$
1

1 Answer 1

2
$\begingroup$

We can force FrontEnd to use versioned preferences by turning on the "VersionedPreferences" option:

SetOptions[$FrontEnd, "VersionedPreferences" -> True];

After evaluating this and restarting Mathematica each version will use its own set of FrontEnd preferences.

One of such preferences is StyleSheetPath which specifies the list of directories FrontEnd searches to find stylesheets:

CurrentValue[$FrontEnd, StyleSheetPath]
{
FrontEnd`FileName[{"C:\\Program Files\\Wolfram Research\\Mathematica\\11.1\\SystemFiles\\Links\\DataResource\\FrontEnd","StyleSheets"},"PacletManager"->True,"Prepend"->True],
ParentList,
FrontEnd`FileName[{"C:\\Program Files\\Wolfram Research\\Mathematica\\11.1\\SystemFiles\\Components\\MUnit\\FrontEnd","StyleSheets"},"PacletManager"->True]
}

Let us create in $UserBaseDirectory a directory named "_MyStyleSheets_" with subfolder named according to $VersionNumber:

CreateDirectory[
 FileNameJoin[{$UserBaseDirectory, "_MyStyleSheets_", ToString[$VersionNumber]}]];

Now prepend (!) this folder to StyleSheetPath. The following permanently changes StyleSheetPath for the version where you evaluate this (if "VersionedPreferences" is already set to True):

SetOptions[$FrontEnd, 
 StyleSheetPath -> 
  Prepend[CurrentValue[$FrontEnd, StyleSheetPath], 
   With[{ver = ToString[$VersionNumber]}, 
    FrontEnd`FileName[{$UserBaseDirectory, "_MyStyleSheets_", ver}]]]];

Now if we create a subfolder "Wolfram" and put there modified "Reference.nb" stylesheet, after restarting the Documentation pages of the current version of Mathematica will use this stylesheet (but not the other installed versions):

CreateDirectory[
 FileNameJoin[{$UserBaseDirectory, "_MyStyleSheets_", ToString[$VersionNumber], 
   "Wolfram"}]];

Export[FileNameJoin[{$UserBaseDirectory, "_MyStyleSheets_", ToString[$VersionNumber], 
   "Wolfram", "Reference.nb"}], 
 Notebook[{Cell[
    StyleData[StyleDefinitions -> 
      FrontEnd`FileName[{$InstallationDirectory, "SystemFiles", "FrontEnd", "StyleSheets",
         "Wolfram", "Reference.nb"}]]], 
   Cell[StyleData["NotesSection"], 
    CellDynamicExpression :> 
     FrontEnd`Private`SetCurrentValue[
      FrontEnd`ButtonNotebook[], {TaggingRules, "Openers", "NotesSection"}, True]]}, 
  StyleDefinitions -> "PrivateStylesheetFormatting.nb"]]

This approach has the advantage that you always know where your custom stylesheets are located, and can easily backup them all at once if you reinstall the OS, transfer data to other computer, share your custom-styled Notebooks with friends etc.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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