3
$\begingroup$

I'm writing a tech note in Mathematica, and would like to include some key combination shortcuts. For example, Ctrl+/ for entering fractions. How do I typeset key combinations so that they look like the ones in the Mathematica documentation?

enter image description here

$\endgroup$
4
  • $\begingroup$ An imperfect method: $$ $$ commands = {{"ctrl", "/"}}; $$ $$ TextGrid[commands, Frame -> All, Background -> {{LightGray, White}}] $$ $$ This doesn't have the beveled edges. I'll update if I can find something better. One thing I noticed that may be helpful though: if you copy the key combination box and paste into a notebook, it pastes as` Key1[ctrl,/]` with red text. If you hover over this it says The TemplateBox was unable to use the specified display function, or it was unable to locate the display function possibly because style sheet is missing $\endgroup$
    – ydd
    Commented Jul 4 at 5:18
  • $\begingroup$ Still unable to figure this out but I have been able to tease out the TemplateBox commands have to look like TemplateBox[{ctrl, return}, Key1, BaseStyle -> "HowToText" ...] (if you type this into google minus the ellipses and search verbatim you will see this is how it's written in the documentation). I don't know where this BaseStyle comes from and I don't really have any experience with boxes, but maybe someone will see this who can help. $\endgroup$
    – ydd
    Commented Jul 5 at 3:12
  • 1
    $\begingroup$ If you zoom in on the image in the question, you can see on the left of the frame that the gray background leaks out of the rounded frame. So I don't think it's worth trying to reproduce this janky help style exactly. What I usually do is simply use Pane["ctrl",FrameMargins->0] and similarly for the other key. $\endgroup$
    – Jens
    Commented Jul 5 at 4:47
  • $\begingroup$ Sorry, I had a typo: It's Panel, not Pane... Here's my corrected suggestion: Panel["ctrl", FrameMargins -> 0, BaselinePosition -> Baseline]. You can copy the output into your text. $\endgroup$
    – Jens
    Commented 2 days ago

2 Answers 2

7
$\begingroup$

The specific boxes you ask for are:

TemplateBox[{"ctrl", "/"}, "Key1", BaseStyle -> {"ExampleText", FontWeight -> "Plain", FontFamily -> "Source Sans Pro"}]

Unfortunately, it doesn't seem like the style definitions for this template box exists in a normal notebook:

enter image description here

If you evaluate the same code in a documentation notebook it does work:

enter image description here

I might be able to figure out what DisplayFunction is being used to achieve this typesetting.

Edit

Ah, found it. If you go into a documentation notebook and evaluate:

CurrentValue[InputNotebook[], {StyleDefinitions, "Key1", TemplateBoxOptions}]

you get the display function for the "Key1" TemplateBox. You can copy-paste that to a normal notebook and assign it to a variable. You can then use these options in the TemplateBox expression from earlier:

boxOpts = {BaseStyle -> {}, DefaultBaseStyle -> {}, 
   DisplayFunction -> (FrameBox[
       TagBox[GridBox[{{ItemBox[
            TemplateBox[{"\[ThickSpace]", #1, "\[ThickSpace]"}, 
             "RowDefault"], 
            Background -> GrayLevel[0.9333333333333333], 
            StripOnInput -> False], 
           ItemBox[TemplateBox[{"\[ThickSpace]", #2, "\[ThickSpace]"},
              "RowDefault"], Background -> GrayLevel[1], 
            StripOnInput -> False]}}, 
         GridBoxAlignment -> {"Columns" -> {{Center}}, 
           "Rows" -> {{Baseline}}}, AutoDelete -> False, 
         GridBoxDividers -> {"ColumnsIndexed" -> {2 -> GrayLevel[
              0.8352941176470589]}, "Rows" -> {{None}}}, 
         GridBoxItemSize -> {"Columns" -> {{All}}, 
           "Rows" -> {{1.45`}}}, 
         GridBoxSpacings -> {"Columns" -> {{0}}}, 
         BaselinePosition -> {{1, 1}, Baseline}], "Grid"], 
       RoundingRadius -> 4, 
       Background -> GrayLevel[0.9333333333333333], 
       FrameMargins -> {{0, 0}, {-1, -1}}, 
       FrameStyle -> {GrayLevel[0.8352941176470589], 
         AbsoluteThickness[1]}, BaselinePosition -> Baseline, 
       StripOnInput -> False] &), InterpretationFunction -> Automatic,
    CopyFunction -> Automatic, EditingHighlightStyle -> {}, 
   DefaultEditingHighlightStyle -> {}, 
   EditingHighlightArgumentStyle -> {}, 
   DefaultEditingHighlightArgumentStyle -> {}, 
   EditingHighlightBackgroundMargins -> Automatic, 
   EditingHighlightArgumentBackgroundMargins -> Automatic, 
   Editable -> Automatic, Selectable -> Automatic, 
   SyntaxForm -> Automatic, StripWrapperBoxes -> False, 
   Inactive -> False, InactiveStyle -> "InactiveStyle", 
   Tooltip -> None, TooltipStyle -> {}, 
   DefaultTooltipStyle -> "TooltipLabel", TooltipDelay -> 0.`};

TemplateBox[{"ctrl", "/"},
     "Key1",
     BaseStyle -> {"ExampleText", FontWeight -> "Plain", 
    FontFamily -> "Source Sans Pro"},
  boxOpts
  ] // RawBoxes

enter image description here

You should now be able to put different things in the first argument of the template box to get different shortcuts. You can copy-paste the output to put it where you need it.

$\endgroup$
1
$\begingroup$

You might want to use a button. This you can do as follows: First, you start writing a text within a text cell: "Enter in 2D form using "

Then you make Ctrl+9 which creates an inline mathematics and type: Button["Ctrl+/",FrontEndExecute[FrontEndToken["Fraction"]]]

Second, you select this command and go to Menu/Evaluation/EvaluateInPlace. Then, you may or may not enter a further text.

This is what you get on the screen:

enter image description here

The fraction template appears upon pressing the button.

Hope it helps. Have fun!

$\endgroup$
1
  • $\begingroup$ I'm sorry, but OP isn't asking about this. $\endgroup$
    – xzczd
    Commented Jul 5 at 2:10

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