# Creating and editing new stylesheets

I'm trying to create a stylesheet, but the process has become painful. I'm sure that this has to do something with resetting something in Mathematica everytime I make changes to the stylesheet. To explain my problem better I will describe the steps I take to create a very simple stylesheet with only one style defined.

In a new notebook enter the following:

CreateDocument[{
Cell[StyleData[StyleDefinitions -> "Core.nb"]],
Cell[StyleData["Title"],
ConversionRules -> {
"TeX" -> {"\\title{", # &, "}"},
"HTML" -> {"<h1 class=\"Title\"\n>", # &, "\n</h1>"}
},
FontSize -> 26,
FontWeight -> "Bold",
FontColor -> RGBColor[0, 0, 0]
]
},
StyleDefinitions -> "StylesheetFormatting.nb",
Editable -> True,
Saveable -> True
]


The new notebook that appears will contain two cells. It will have the style as all the stylesheets that you have seen and you will be able to edit and save it.

Save this notebook in \$UserBaseDirectory/SystemFiles/FrontEnd/StyleSheets/. I name this notebook Style01.nb. Once you have this enter this command:

MathLinkCallFrontEnd[FrontEndResetMenusPacket[{Automatic, Automatic}]]


I found that command here, this command resets the menu so that Mathematica can list your stylesheet in the menu. At this point we are ready to use the stylesheet. Open a new notebook and create a Title cell. Save this notebook as TestNb.nb in a directory of your choice. Go to Format > Stylesheet > Style01.

# Test 1:

The current view should be this:

Now we select the Title cell in Style01.nb and we show the expression by going to Cell > Show Expression. The next screenshot shows the view with the modifications:

Now we change back the title cell to how it was originally by doing Cell > Show Expression again. In a mac we can press command + shift + E. Now we see that the changes took effect.

No problem there. We can repeat this procedure to keep adjusting the Title cell.

# Test 2:

Now lets see if our conversions rules work. Lets create a new notebook called ExportNb.nb in the same directory as TestNb.nb. Put the following in it:

nb = NotebookOpen[NotebookDirectory[] <> "TestNb.nb"];
ExportString[nb, "HTML", "FullDocument" -> False]


This is what I obtained after evaluating those commands:

The conversion rules took effect. But, I made a mistake (purposely made when I created the style sheet). The angle bracket should have been on the top. In any case, let us correct this mistake:

After we revert back, and save Style01.nb, we try to see if the new conversion rules will work.

Unfortunately, the changes in the ConversionRules do not take effect. I have found that if I quit the Kernel by going to Evaluation > Quit Kernel > local and then I run the commands again then it works:

# Conclusion and Questions:

It seems that if we want to the way your notebook looks like you can create styles and you will see how the changes take effect after you modify your stylesheet as I described in Test1. But, if you are to export your notebook as an HTML file or Tex, then you better beware of the ConversionRules. I'm sure you will keep making many mistakes when you change the ConversionRules but you will not be able to see the change of this unless you restart the kernel.

Questions: Is there any way for Mathematica to know that there has been changes in the conversion rules? Is there something that we need to restart other than the Kernel to see that this changes have been made? If restarting the kernel is the only way, is there a command to restart it? I'm getting tired of having turning it off and on all the time manually.

• Have you tried ClearSystemCache[]? If it does not help you could try to find affected symbols by monitoring the ByteCount (or even OwnValues and DownValues) for the all non-protected symbols in all contexts using something like the code I provided here. – Alexey Popkov May 28 '12 at 5:27
• @AlexeyPopkov, I tried ClearSystemCache[] but that does not work. I guess at this point the best is to just evaluate the quit command and then evaluate the code I showed. I'll look more into your function later. Seems useful. – jmlopez May 28 '12 at 16:43

Instead of going to Evaluation > Quit Kernel > local, you simply type Quit and the kernel stops, then run the commands again in the new session. That's it!

• I figured that much but it just seems like a little too much to restart everything when there's probably something else that takes care of the style definitions. Anyway, +1 for answering. – jmlopez May 27 '12 at 14:58
• I answered this way because, from your question, it seemed like you were not aware of the Quit command. Quitting and restarting used to be a slow and painful process with the first versions of MMA, partly because PCs were comparatively slow back then, but now it takes a few millisecs to restart the whole thing and have a clean slate, so why bother? – magma May 28 '12 at 8:46
• @magma maybe here is not the right place to post a question. But I just wondered if there is a difference between Quit[] and CloseKernels[]. – RMMA Oct 17 '12 at 12:02
• @Frink good question! I am not sure since I never really experimented with parallel computations, but my imression is that Quit only closes one kernel, while CloseKernels closes all open kernels. Other ideas? I also find strange that Quit and CloseKernels are not related in the documentation. In my opinion this is wrong. – magma Oct 17 '12 at 22:41
• @magma as far as I know Quit[] closes the master kernel and so also all slave kernels. – RMMA Oct 19 '12 at 12:27

I've finally packaged up my stylesheet editing code so that it's easy to use. You can find it here.

Get["https://raw.githubusercontent.com/b3m2a1/mathematica-tools/master/StylesheetEdit.wl"


And then it provides you with functions for finding style cells:

StyleSheetCells["MyCell"]


Making new style cells:

StyleSheetNew["MyCell"]


And editing cells:

StyleSheetNew["MyCell",
{
FontColor->Pink,
FontFamily->"CMU Serif"
}
]


Hopefully this makes working with stylesheets less painful