# What sets of boxes can be selected?

In this question I am only interested in mathematical expressions arising in output cells, not more elaborate code which is typically found in input cells. My understanding is that such output is made of boxes which, like every expression in Mathematica, have a the structure of a tree.

I would have naively thought that what can be selected in an output cell is any subtree of that tree. E.g. in the fraction x+y over z (with box form FractionBox[RowBox[{"x","+","y"}],"z"]), one cannot select y and z without selecting the whole fraction.

However, this is too naive: MakeBoxes[-x y] gives the nested structure RowBox[{RowBox[{-, x}], , y}], but one can nevertheless select x and y in the output of -x y without selecting -.

Another similar case is that MakeBoxes[ab cd] gives RowBox[{"ab"," ","cd"}] but one can select (in the output of ab cd) b c, whose box representation is not a subtree.

I think my question boils down to: is RowBox is the only case where one can select a non-subtree?

• Another difficult case: in RowBox[{"b", "+", SuperscriptBox["b", "2"]}] (i.e. b+b^2), one can select b+b. – Bruno Le Floch Apr 11 '16 at 16:23
• MakeBoxes[Grid[{{1, 2}, {3, 4}}]] gives ...GridBox[{{"1", "2"}, {"3", "4"}}...]... but you can select {{"1"},{"3"}} – andre314 Apr 11 '16 at 19:50

You can get this behaviour by setting StucturedSelection to True at the Global, Notebook, or Cell level. See the Options for Expression Input and Output guide.

This can be done with the Options Inspector or with SetOptions. To set it at the notebook level:

SetOptions[EvaluationNotebook[], StructuredSelection -> True]


After this is done you will only be able to select complete sub-expressions in the notebook.

Hope this helps.

• Nice! I'll set this option temporarily to get a subexpression. Strangely, when inside a button (Button["click", SetOptions[EvaluationNotebook[], StructuredSelection -> True]]) the setting does not seem to take effect immediately. More precisely, if I click the button I can still select b+b in b+b\^2, but if I query the value of the option with Options[EvaluationNotebook[], StructuredSelection] it shows up as True and selection is now structured. Also, if I keep track of the option's value with Dynamic the option takes effect immediately. Do you know what is going on? – Bruno Le Floch Apr 11 '16 at 23:31
• @BrunoLeFloch Not certain why this is happening from Button`. Are you using verison 10.4? – Edmund Apr 11 '16 at 23:43
• \$Version is "10.0 for Microsoft Windows (64-bit) (June 30, 2014)". – Bruno Le Floch Apr 12 '16 at 0:40
• @BrunoLeFloch I have tried the button in 10.3 and it works as expected. I can try it in 10.4 later. It not working in 10.0 maybe specific to that version. Upgrade? – Edmund Apr 12 '16 at 12:14
• I won't be able to update very soon, but it is good to know that 10.3 and 10.4 are better behaved than 10.0. – Bruno Le Floch Apr 12 '16 at 15:02