1
$\begingroup$

Consider the following button:

Button[Test, 
  NotebookApply[EvaluationNotebook[], 
    TemplateBox[{RowBox[{"[", ToBoxes@SelectionPlaceholder[Species], 
    "]"}]}, "chemical", Editable -> True, Selectable -> True], 
    Placeholder]]

The style "chemical" will be given below. There are three conditions for applying this button. Two of them work how I would like, but the third does not.

  1. When the cursor is somewhere within an existing input cell, [Species] is inserted (where Species is a named placeholder), and Species is left selected, and the needed tags and styles are applied.
  2. When something is selected before pressing the button, pressing the button pastes square brackets (with the appropriate tags and styles) and the cursor is left after the last bracket.
  3. (The problematic one). When the cursor is between cells, an input cell is created and [Species] is inserted (just as with 1 above), but the whole thing is left selected, not just the placeholder.

enter image description here

I am curious why the behavior is what it is in case 3, but more importantly am looking for a workaround for case 3 that only has the placeholder selected afterward.

The style definition is given by:

SetOptions[EvaluationNotebook[], 
  StyleDefinitions -> 
    Notebook[{Cell[
      StyleData[
        StyleDefinitions -> 
        FrontEnd`FileName[{"Report"}, "StandardReport.nb", 
        CharacterEncoding -> "UTF-8"]]], 
      Cell[StyleData["chemical"], StyleMenuListing -> None, 
        TagBoxOptions -> {SyntaxForm -> "symbol"}, 
        ShowStringCharacters -> False, ShowAutoStyles -> False, 
        ZeroWidthTimes -> True, FontWeight -> Plain, 
        FontFamily -> "Times", 
        TemplateBoxOptions -> {DisplayFunction -> (RowBox[{#}] &), 
        SyntaxForm -> "symbol"}]}]]
$\endgroup$
1
$\begingroup$

Use:

Button[
    Test,
    NotebookApply[
        EvaluationNotebook[],
        TemplateBox[{ToBoxes@SelectionPlaceholder[Species]},"chemical"],
        Placeholder
    ]
]

and in your stylesheet:

TemplateBoxOptions->{DisplayFunction->(RowBox[{"[",#,"]"}]&),SyntaxForm->"symbol"}

That is, put the brackets inside of DisplayFunction so that they can't be selected.

$\endgroup$
  • $\begingroup$ Interesting idea. I will have to think about if that will work for my application. The issue is that I have some symbols (assigned to complicated labels using the Notation package), and I have a way to assign new, related symbols to the bracketed form. Picking an easy to type example (Imagine NaCl is much more complicated), “NaCl” is given the symbol sodiumChloride and I enter NaCl using a menu or palette so I can maintain tags. For all symbols I define the notation [NaCl] as the original symbol name with conc appended (concentration of NaCl). I want a separate palette for [Placeholder]. $\endgroup$ – Kevin Ausman Apr 29 '19 at 5:45
  • $\begingroup$ And yes, I think this will work. I needed to make a second style (the original for the chemical, and the new one for the concentration), but the nesting of the styles seems to do what I want. Thank you! $\endgroup$ – Kevin Ausman Apr 29 '19 at 5:55

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.