4
$\begingroup$

I have been given a large notebook that was created in mathematica 6. It contains a structure of sections and subsections and the input text is formatted in an unusual (possibly just old) style (see screen grab below). original formatting

My main aim is to update the formatting of the input cells to the standard mathematica 8 format as I find this format much easier to read/debug (example below).

new formatting

My first thought was to simply copy the entire notebook into a text editor and then copy back into a notebook. However, this results in the generation of a single large input cell that is prohibitively tedious to break up...

Applying a new stylesheet does not change the input cell style.

Is it possible to update the formatting/style throughout a notebook whilst preserving the cell structure and hierarchy?

Edit: An excerpt from the original notebook can be found here,

and here is the same excerpt after clearing formatting (but without syntax highlighting).

$\endgroup$
3
  • $\begingroup$ Please upload a sample Notebook if you can. It sounds like this may be a compound problem as I think Clear Formatting should have restored the original syntax highlighting if there is not something else going on. $\endgroup$
    – Mr.Wizard
    Feb 25, 2013 at 12:21
  • $\begingroup$ @Mr.Wizard not sure how I upload a notebook?... I have a before and after ready to go... $\endgroup$
    – geordie
    Feb 25, 2013 at 12:40
  • $\begingroup$ Notebook files are text, so you can open one in a text editor, copy the text, and use pastebin.com (512kB limit) $\endgroup$
    – Mr.Wizard
    Feb 25, 2013 at 12:41

2 Answers 2

6
$\begingroup$

If applying a new stylesheet doesn't change the appearance then it looks like the styling is local to each cell. If you have a look at the underlying expression of some of these input cells by going to the menu and choosing Cell > Show Expression you should see some StyleBoxes, as per this example using your code that you have supplied:

enter image description here

So if it is the case that all this styling has been introduced locally at each cell then go to the menu and select Edit > Select All to select all cells. Then select Format > Clear Formatting. This will remove all the local styling through out your notebook and your notebook will now respond to your choice of stylesheet.


Based on your feedback it appears other cells have localized styling as well and you want to keep that. So then you just need to clear the formatting for the input cells only.

Further inspection of the code you have been given reveals it to be a mess -- it is inputform without row boxes. With this information I'd actually recommend converting to standard form as per Mr Wizards answer but then your comments will be lost. Therefore I recommend the following, more laborious approach:

  1. Make the input cell a text cell (or other cell).

  2. Now make it back to an input cell.

  3. Now clear the formatting.

Th screen grab shows the stages:

enter image description here

and to prove it works when I change the stylesheet:

enter image description here

If you have several of these notebooks these steps could be done programmatically but for a one off I'll leave it at that.

$\endgroup$
3
  • $\begingroup$ Thanks for your answer. Unfortunately, while clearing the formatting causes all of the text to go black, applying a new stylesheet does not cause the input cells to change to the standard colours (e.g. blue for variables, green for arguments, etc.). $\endgroup$
    – geordie
    Feb 25, 2013 at 11:32
  • $\begingroup$ that is strange behaviour. So even the text has local styling then? If you want to keep local styling for everything except Input then just clear the formatting for input cells only. $\endgroup$ Feb 25, 2013 at 21:22
  • $\begingroup$ Your second strategy pretty much sorted the problem. Thanks! $\endgroup$
    – geordie
    Feb 27, 2013 at 0:46
3
$\begingroup$

Here are two cells with weird embedded styles, including colors, background colors, and sizes:

Mathematica graphics

Select both cells and press Shift+Ctrl+N to re-parse to StandardForm:

Mathematica graphics

$\endgroup$
4
  • 1
    $\begingroup$ If you do that you would lose any comments that are present in input cells in this notebook he has been given. Better to simply clear the formatting. $\endgroup$ Feb 25, 2013 at 7:56
  • $\begingroup$ @Mike I suppose so. I wanted to provide another option and I frequently make use of this when I get code in a format that I don't like, say excessive use of infix notation :o) , and want to put it back into a "normal" form. By the way, I voted for yours. $\endgroup$
    – Mr.Wizard
    Feb 25, 2013 at 8:00
  • $\begingroup$ It is another option for sure but it should come with the warning that comments will be lost if employed. $\endgroup$ Feb 25, 2013 at 11:23
  • $\begingroup$ This approach works great, except that i would like to keep the comments... is there any other way to do this? perhaps I should post a question about parsing to std- or input-form whilst retaining comments...? $\endgroup$
    – geordie
    Feb 25, 2013 at 11:35

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.