12
$\begingroup$

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:

MathLink`CallFrontEnd[FrontEnd`ResetMenusPacket[{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:

pic01

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:

pic02

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.

pic03

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:

pic04

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:

pic05

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:

pic06

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.

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$ May 28, 2012 at 5:27
  • $\begingroup$ @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. $\endgroup$
    – jmlopez
    May 28, 2012 at 16:43

2 Answers 2

2
$\begingroup$

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!

$\endgroup$
6
  • $\begingroup$ 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. $\endgroup$
    – jmlopez
    May 27, 2012 at 14:58
  • $\begingroup$ 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? $\endgroup$
    – magma
    May 28, 2012 at 8:46
  • $\begingroup$ @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[]. $\endgroup$
    – RMMA
    Oct 17, 2012 at 12:02
  • $\begingroup$ @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. $\endgroup$
    – magma
    Oct 17, 2012 at 22:41
  • $\begingroup$ @magma as far as I know Quit[] closes the master kernel and so also all slave kernels. $\endgroup$
    – RMMA
    Oct 19, 2012 at 12:27
3
$\begingroup$

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

You can load it like:

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

$\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.