3

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?

Edit:

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.)

Alan
  • 13,686
  • 19
  • 38
  • 1
    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. – Szabolcs Sep 22 '17 at 18:11
  • 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. https://pastebin.com/2Z2rBFnT – Szabolcs Sep 22 '17 at 18:50
  • 1
    @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! – Alan Sep 22 '17 at 21:29
  • This looks helpful (I have not yet tried it): https://mathematica.stackexchange.com/questions/28286/add-keyboard-shortcut-to-the-displayformula-style 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. – Alan Sep 22 '17 at 21:39

1 Answers1

1

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

What about Ctrl-( followed by Alt-0?

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
  • 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. – Alan Sep 23 '17 at 19:46
  • 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)? – Szabolcs Sep 23 '17 at 19:49
  • @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. – Carl Woll Sep 23 '17 at 20:20
  • @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? – Carl Woll Sep 23 '17 at 20:26
  • @CarlWoll What I mean is related to the question of @Szabolcs. Start a notebook with the default style, and start typing in a text cell. Then press Alt-8 and type a word (formatted as code). Then press Alt-7 and type some more text. Then do as you suggest to enter a word with a Code style. To see the difference, press Ctrl+Shift+E to toggle the cell display. (Separately, it seems both inconsistent and undesirable that Alt-0 cannot start a style box as simply as Alt-8 does.) – Alan Sep 23 '17 at 21:14
  • @Alan I'm using a prerelease version of M11.2, and Alt-8 no longer works that way (instead, Alt-8 changes the style of the whole cell). But, that may be due to customizations I've done. When I try this in M9, M10, and M11.1, Alt-0 behaves similarly to Alt-8. What version of Mathematica are you using? – Carl Woll Sep 23 '17 at 21:47
  • @CarlWoll I'm using 11.1. I will be extremely disappointed if the Alt-8 functionality (simple style box) has gone away. Alt-8 works correctly in 11.; it is Alt-0 that was broken. You seem to be indicating that Wolfram "fixed" the wrong one. – Alan Sep 23 '17 at 22:31
  • @Alan That is very disturbing indeed :-( https://mathematica.stackexchange.com/q/156423/12 – Szabolcs Sep 24 '17 at 07:39
  • @CarlWoll I think the issue wasn't with FormatType. This is the problem: if I type something like std::vector<int> into an inline cell (this is C++ code) then it gets automatically reformatted as if it were Mathematica code. The :: is taken to denote a Message and vector is made correspondingly grey (instead of black). The spacing around < and > is increased as if they denoted inequality. This happens even if the style is "Text" and the FormatType is TextForm. I am not sure what controls it. In a top-level Text cell it does not happen (but in an Input cell it does). – Szabolcs Sep 24 '17 at 08:10
  • 1
    @Szabolcs From Support: "A correction is being worked on for a future release." – Alan Sep 25 '17 at 22:43