# Publish to Cloud with accompanying stylesheet

Is it possible to Publish to Cloud with an accompanying user-defined stylesheet? So that anyone who clicks on the link will see it with the correct stylesheet.

I'm not talking about using stylesheets on your own, editable, version of the document on the cloud, as in this question.

The use case is that I have made my own stylesheet "mystylesheet.nb" which extends "default.nb", and I have a notebook, which uses this stylesheet, and I want to publish this notebook to the cloud so that anyone can view it. But it should display on their screens with the correct stylesheet "mystylesheet.nb" being used.

Update: When I try Rolf Mertig's helpful Button below, it doesn't seem to work. Here is how the notebook appears on my Mathematica desktop application (it has been styled by my stylesheet):

Then I click the magic "Embed Stylesheet explicitly" Button, and then click "Publish to Cloud" from the File menu. Here is how it looks like on the cloud, in my Chrome browser:

The reason for the problem, in this case, can be found as follows. If, on the cloud version, you type in Options[SelectedNotebook[]] one finds that my stylesheet options were not loaded line-by-line in explicit form, but were rather left as a file eg. StyleDefinitions -> UserStyle.nb.

• I don't think the feature you asked has been implemented yet. It looks like on server side there could be converters translating a notebook into html. So if that converter doesn't have ability of understanding generic stylesheet, there is nothing users can do. (But do refer to this Q/A!) – Silvia Apr 4 '20 at 15:25
• It is really quite silly that Mathematica can't do this. If you're going to "publish in the cloud", you obviously want to make your document look as polished as possible. That means being able to upload your stylesheet. Not being able to do that defeats defeats the purpose of the whole thing. – Bruce Bartlett Apr 5 '20 at 22:04
• I guess it worth asking in our site's chat. I remember some of our members (C. E., b3m2a1, kuba, etc.) have their own ways to publish to web. Some of them are actually quite sophisticated. – Silvia Apr 6 '20 at 15:28

If you load "mystyle.nb", e.g. by Get, it is easy to do what you want:

myStyle =
Export["mystyle.nb",
Notebook[{Cell[StyleData[StyleDefinitions -> "Default.nb"]],
Cell[StyleData["Notebook"], Background -> Red],
Cell[StyleData["Input"], CellFrame -> True]}]];
myNB = CreateDocument[ExpressionCell[Defer[1 - 2], "Input"]];
SetOptions[myNB, StyleDefinitions -> Get[myStyle]]
CloudDeploy[myNB] // SystemOpen


results in

Suppose your "mystylesheet.nb" is in the same folder as your notebook, or somewhere on $Path, then I would first generate a Button by executing this code in a different notebook: Button["Embed StyleSheet explicitly", Module[{nb = SelectedNotebook[], styleNB}, styleNB = FileNames[ Replace[ CurrentValue[nb, StyleDefinitions], Notebook[{Cell[StyleData[ StyleDefinitions -> myStyleFile_String]]}, __] :>myStyleFile], Prepend[$Path, NotebookDirectory[nb]]
] /. {s_String} :> s;
If[ FileExistsQ[styleNB]
,
SetOptions[nb, StyleDefinitions -> Get @ styleNB
];
NotebookSave[nb];
MessageDialog[TextCell[styleNB <> " has been embedded", "Title"],
WindowMargins -> CurrentValue[InputNotebook[], WindowMargins]]]
]
] //
(CreatePalette[#, WindowMargins -> (
AbsoluteCurrentValue[SelectedNotebook[], WindowMargins
] {{0, 1}, {1, 0}} + {{1, 0}, {0, 1}} MousePosition[])] &)


and then you first click on the main notebook you want to publish, then click on the button and then you can use the standard menu item "Publish to Cloud..." from the File menu. You may also save the Button notebook for later use. Of course something like this should automatically happen inside NotebookToolsOpenCloudPublishDialog`, but that is something the Wolfram developers will certainly do for the next version (I hope). It should be easy to generalize my short fix for more complicated private style sheets.

• Thank you! I am still trying to understand this in terms of my workflow. Your example programatically creates a notebook, whereas in practice I will create a notebook in the normal way, using the graphical user interface. Then, I save it, as "mynotebook.nb". My stylesheet is contained in "mystyle.nb". Now, how do I publish to the cloud? Do I type in a new input expression, at the end of "mynotebook.nb", of the form "SetOptions["this notebook", Get["mystyle"] "? But, if I do that, and upload to the cloud, then the stylesheet will only be loaded when someone pushes "Evaluate all cells" – Bruce Bartlett Apr 26 '20 at 12:22
• Thank you for your help. I have tried to get this working today but it does not seem to work properly. If you use SetOptions[myNB, StyleDefinitions -> Get[myStyle]] and then click Publish to the Cloud, then the document on the cloud will not use the new styles initially. There seems to be a bug. You have to first click on "Make your own copy" on the published worksheet. Then, suddenly, the new tab where your own copy has been made "communicates" to this original tab in your browser, and the styles are implemented. What is going on there, am I making a mistake? – Bruce Bartlett Apr 26 '20 at 22:48
• Did you use my Button "Embed StyleSheet explicitly"? That should just work. Maybe you have to experiment with different browsers since they have different caching behaviour, but that is something to talk to support@wolfram.com. – Rolf Mertig Apr 27 '20 at 8:03
• Yes I have tried it. Thank you very much for it. But it doesn't work. I have edited my question so that I can attach a screenshot. – Bruce Bartlett Apr 27 '20 at 11:00
• There are a few bugs, some of them mine and perhaps some of them Mathematica's. It's probably most helpful to isolate each bug and open a separate question on it. I'll start doing that shortly. – Bruce Bartlett Apr 27 '20 at 11:30