I am working in a notebook. I would like to be able to easily style and restyle text fragments in a notebook text cell. I want to determine this styling in a style sheet, so that text fragments are styled separately from the other text. (So, I don't want to just select and style each fragment separately; I want stylesheet control over the styling of all code fragments at one go.) This is of course trivial in an HTML document: just place the fragment in a CODE element or a KBD and style it with CSS. How is it done in a Mma document?


I'm going to reframe this a bit to emphasize work flow. Consider notebook usage and entering text in a text cell. In addition to being able to easily enter math, here are some types of text we might want (in terms of HTML elements): code, kbd, emph, samp, strong. If I understand correctly, Mma does not build in such text-level semantics to instead provides StyleBox as a rough equivalent of a span with a specified "style" playing the role of a class attribute for the span. So it appears the right way in Mma to discard the idea of semantic elements and focus on the idea that any semantics will be embodied in the names of styles. Unfortunately, if I press Alt-0 while entering text and then enter a style, it does not start a style box but instead styles the whole cell. (Is this desirable? It seems wrong.) So I think my question has become, can I hijack Alt-0 for starting style boxes with the same interface for entering a user-specified style?

(If this edit seems to shift the emphasis of the question too much, I can change it to a new question.)

  • 1
    $\begingroup$ The question seems a bit general ... it would be easier to answer if it were about a concrete use case. Otherwise it sounds like you are asking for a tutorial on stylesheets ... A notebook is made of Cells, the second argument of which is a style name. I'd start here and follow the links at the end. $\endgroup$ – Szabolcs Sep 22 '17 at 18:11
  • $\begingroup$ I wrote up something but I hit a roadblock. I put the partial post in a pastebin. Everyone should feel free to build on it (and post an aswer) if you can fix the remaining problem. pastebin.com/2Z2rBFnT $\endgroup$ – Szabolcs Sep 22 '17 at 18:50
  • 1
    $\begingroup$ @Szabolcs Actually I would not mind a pointer to a decent stylesheet tutorial. But what you propose is quite close to what I am looking for. After reading what you wrote this seems possible enough, but I do not understand why some text might be formatted with other than the provided style (as you seem to indicate). Thanks! $\endgroup$ – Alan Sep 22 '17 at 21:29
  • $\begingroup$ This looks helpful (I have not yet tried it): mathematica.stackexchange.com/questions/28286/… It seems one can define any style and then apply it with a keyboard shortcut. My ideal would be to invoke a request for the style name with a keyboard shortcut, which could then be entered in a dialog and applied. $\endgroup$ – Alan Sep 22 '17 at 21:39

(Moved my comment to an answer to enable the formatting of kbd).

What about Ctrl-( followed by Alt-0?

  • $\begingroup$ I'm hoping to use a simple StyleBox. Compare the use of Alt-8 (in the default style) to start entering code in a style box. I'd like to use Alt-0 to do something similar, with with my choice of style. $\endgroup$ – Alan Sep 23 '17 at 19:46
  • $\begingroup$ Do you know if it's possible to prevent a FormatType from being added to the inline cell (the reason why my now deleted answer wasn't usable for writing non-Mathematica code in inline cells)? $\endgroup$ – Szabolcs Sep 23 '17 at 19:49
  • $\begingroup$ @Alan I thought you were typing in a text cell, and as an example, wanted to start a code block. Doing Ctrl-( followed by Alt-8 will do this. I don't know what it means to use Alt-8 to start entering code in a style box. $\endgroup$ – Carl Woll Sep 23 '17 at 20:20
  • $\begingroup$ @Szabolcs I don't know how to prevent a FormatType option from being added, but you can control what kind of FormatType is added by using CommonDefaultFormatTypes. For example, SetOptions[$FrontEndSession, CommonDefaultFormatTypes -> {"TextInline" -> TextForm}] will make inline cells use TextForm as the FormatType. Nevermind, you already know that! So what is the issue with FormatType? $\endgroup$ – Carl Woll Sep 23 '17 at 20:26
  • 1
    $\begingroup$ @Szabolcs From Support: "A correction is being worked on for a future release." $\endgroup$ – Alan Sep 25 '17 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.