# Typeset unbalanced brackets

In LaTeX, you can typeset expressions containing unbalanced brackets, by balancing them with an invisible delimiter \left. or \right.. Some such notations are common in calculus; for example, the two common notations for applying an expression at two points:

$$\int_a^b x^2 \mathrm{d} x = \left. \frac{x^3}{3} \right]_a^b \equiv \left. \frac{x^3}{3} \right|_a^b$$

$$\left. \frac{\partial f}{\partial x} \right| _{x=0}$$

The alternative notation for an open interval endpoint:

$$\left[a,b\right) \equiv \left[a, b\right[$$

But in the Mathematica symbol list, I don't see any invisible brackets. If you simply leave out the delimiter, it results in an unmatched bracket, messing up the formatting if you have multiple such expressions in the same cell.

How do you typeset such expressions in Mathematica? (This is in display formulas; it doesn't have to be evaluatable.)

• You're not evaluating these, right? Aren't these solely for typesetting? In that case, does it really matter if the brackets are not matched? I don't use these fancy typesetting options, so I don't know...
– rm -rf
Nov 15, 2012 at 22:01
• as per @rm-rf comments, if it is just for typesetting, not for evaluation, you can switch of the unmatched bracket syntax highlighting so that it looks "normal" Nov 15, 2012 at 22:06
• @rm-rf: It breaks Ctrl-. selection, and if you have several right-brackets on one line, e.g. $f[x]]_{x=1}=g[x]]_{x=1}=h[x]]_{x=1}$, the brackets will get progressively bigger. Nov 15, 2012 at 22:08
• I suspect the indication of @rojo on this answer can be translated to this context(?) Nov 15, 2012 at 22:18
• The ScriptSizeMultiplier option is probably controlling the expanding brackets. Nov 15, 2012 at 22:19

See this answer to question (101070) for the basic approach to enable the typesetting you desire for evaluating a function at one or two points. The interval typesetting is a bit trickier, but the basic idea is to give the bracket a SyntaxForm option. As a simple example, compare:

RawBoxes @ RowBox[{"[", FractionBox["x","3"], "["}]


with:

RawBoxes @ RowBox[{"[", FractionBox["x","3"], TagBox["[", #&, SyntaxForm->"]"]}]


The latter example shows the desired spanning behavior. Of course, it is not evaluatable. To make it evaluatable, one can use a TemplateBox. Here is an input alias making use of a TemplateBox:

CurrentValue[EvaluationNotebook[], {InputAliases, "oc"}] = TemplateBox[
{"\[SelectionPlaceholder]"},
"OCInterval",
DisplayFunction -> (
RowBox[{
StyleBox["[", SpanMaxSize->Infinity],
#,
StyleBox[TagBox["[", #&, SyntaxForm->"]"], SpanMaxSize->Infinity]
}]&
)
];


Here is an animation showing the alias in action:

If the problem is the size of the brackets after e.g. g[x] then you can split the inline cells. See before and after shots below

Just to be clear, the second example in the screen grab is two inline cells side by side:

• It may work for some cases, but I hope there's some more convincing workaround. Some part of the equation has to be in a different "nesting level". And what if this sum was already inside some x+(...)?
– Rojo
Nov 15, 2012 at 23:30
• @Rojo I don't know if it is possible to define different nesting levels within an inline cell therefore this seems like an option. Nov 15, 2012 at 23:33
• @Rojo I think we're talking apples and oranges. I know you can have nested inline cells. I understood your comment to be about different nesting options within a single inline cell. Nov 15, 2012 at 23:46
• Wouldn't this approach imply that you have to start a new inline cell for every invisible opening bracket that you would like to insert? If you're in an inline cell, getting out of it and starting a new one doesn't seem to cover all cases, such as x+(...]). If I'm misunderstanding something I'll probably get back to this later and not bug you and the answer with long comments
– Rojo
Nov 15, 2012 at 23:58
• @Rojo whether you have to have multiple adjacent inline cells depends on the nature of the equation. In the grabs above there are several unmatched brackets but only two inline cells. So it depends on the location of these unmatched brackets. yes x+(...]) is a problem. Nov 16, 2012 at 0:20

If I understood the question, this is a question on the automatic formatting of an expression you are trying to type in, on how the FE automatically groups it into boxes.

So, for example

RawBoxes@SubscriptBox[
RowBox[{RowBox[{SubscriptBox[
RowBox[{RowBox[{"f", "(", FractionBox["x", "6"], ")"}], "]"}],
RowBox[{"x", "=", "1"}]], "=", RowBox[{"g", "[", "x", "]"}]}],
"]"}], RowBox[{"x", "=", "1"}]]


is not the same as this

RawBoxes@RowBox[{SubscriptBox[
RowBox[{RowBox[{"f", "(", FractionBox["x", "6"], ")"}], "]"}],
RowBox[{"x", "=", "1"}]], "=",
SubscriptBox[RowBox[{RowBox[{"g", "(", "x", ")"}], "]"}],
RowBox[{"x", "=", "1"}]]}]


In those cases where the automatic grouping isn't appropriate, I don't see an easy fix so far to fix it without interrupting the typing flow.

An input alias such as the following would allow you to insert the desired box structure. The invisible TagBox is important so that the front end doesn't regroup automatically

PrependTo[CurrentValue[InputNotebook[], InputAliases],
"app2p" ->
TagBox[RowBox[{"\[SelectionPlaceholder]",
SubsuperscriptBox["]", "\[Placeholder]", "\[Placeholder]"]}],
"app2p"]]


As all input aliases, it works by typing the keyword between Esc. In this case, the keyword is app2p

A more general solution of this sort is to define an input alias to insert only the invisible tag box

   PrependTo[CurrentValue[InputNotebook[], InputAliases],
"tb" ->  TagBox["\[SelectionPlaceholder]", "tb"]]


You just have to remember that there's an invisible box and exit it when appropriate

• @MechanicalSnail, added an edit
– Rojo
Nov 16, 2012 at 12:38

I don't get the described problems when using inline math in a text cell (using Ctrl-(), nor when I copy this math to an empty space. I get this:

• The brackets after g[x] and h[x] shouldn't be as big as those grouping fraction boxes if everything was fine I believe
– Rojo
Nov 15, 2012 at 22:34
• @Rojo if that is the case then the expression could be split into multiple inline cells. Nov 15, 2012 at 22:55
• @MikeHoneychurch, yes, but that feels like a compromise to me. Inline cells inside inline cells toggle the style from text to expression cells I think
– Rojo
Nov 15, 2012 at 23:10
• @Rojo see my screen grabs. These are not inline inside inline. these are adjacent inline cells. i.e. you step out of the inline cell and create another one next to it. Nov 15, 2012 at 23:16