The purpose of this is to append a DockedCell style definition to the list of cells inside Edit Stylesheet without deleting any existing style cells. My one attempt tends to erase existing private or custom style cells. More information can be found here: Cannot find DockedCells with Cells or NotebookFind


1 Answer 1


There is ResourceFunction["AddStyleToNotebook"] which allows to add a new style to the private stylesheet without erasing the existing styles. But unfortunately currently it has a bug specifically with the Initialization option. Hence until the bug isn't fixed we can define our own AddStyleToNotebook:

AddStyleToNotebook[nb_NotebookObject, cell_Cell] := 
 Module[{oldDefs = CurrentValue[nb, StyleDefinitions], newDefs},
  newDefs = If[Head[oldDefs] === Notebook,
    If[FreeQ[First@oldDefs, cell, {1}], Insert[oldDefs, cell, {1, -1}], oldDefs],
    Notebook[{Cell[StyleData[StyleDefinitions -> oldDefs]], cell}, 
     StyleDefinitions -> "PrivateStylesheetFormatting.nb"]];
  SetOptions[nb, StyleDefinitions -> newDefs]]

Use it as follows:

  Initialization :> 
   If[Head[System`$DockedCells] === List, 
    System`$DockedCells = 
     Append[Select[System`$DockedCells, CurrentValue[#, CellID] =!= $Failed &], 
      EvaluationCell[]], System`$DockedCells = {EvaluationCell[]}]]]

(Here I put $DockedCells in the System` context in order to avoid namespace conflicts.)

My report about the bug in the current ResourceFunction["AddStyleToNotebook"]

Here, I'm including the report I just submitted using the "Submit a message about this function" button on the function's page:

Currently the resource function AddStyleToNotebook has a bug: when we try to add several times a new style containing the Initialization option, the latter will be damaged. As an example, consider the following use case:

  "DockedCell", {Initialization :> 
    If[Head[System`$DockedCells] === List, 
     System`$DockedCells = 
      Append[Select[System`$DockedCells, CurrentValue[#, CellID] =!= $Failed &], 
       EvaluationCell[]], System`$DockedCells = {EvaluationCell[]}]}];

If we evaluate the above several times, we get a list of the above values as the value for the Initialization option, what is wrong and leads to the wrong behavior. The above definition is taken from the following answer where its purpose is explained:


Alexey Popkov

  • $\begingroup$ thank you for this. you've been very helpful. $\endgroup$ Aug 17, 2022 at 6:21
  • $\begingroup$ I have just one more question. How do I make certain it only adds the style once in a given notebook? I know I can easily add a default Option setstyle -> False which must be set to setstyle -> True the first time it is run but can this be automated? $\endgroup$ Aug 17, 2022 at 6:27
  • 1
    $\begingroup$ @JulesManson Please look closer at the defintion for AddStyleToNotebook. It already includes a checkup for the presence of the cell via FreeQ[First@oldDefs, cell, {1}]. $\endgroup$ Aug 17, 2022 at 6:35
  • $\begingroup$ you might be pleased to know that i sent a feature request to Wolfram for a direct way for finding DockedCells through the Technical Support (for Service Subscribers). $\endgroup$ Aug 17, 2022 at 6:46
  • 1
    $\begingroup$ @JulesManson Thank you. $\endgroup$ Aug 17, 2022 at 6:50

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.