# How to input open intervals

I want to write open and half-open intervals using the following notation:

]a, b[
]a, b]
]-∞, b]


When writing them just like that in the LaTeX source, the spacing doesn't come out right. For example, for this equation:

 $$X = ]-\frac{π}{2}, +\frac{π}{2}[$$


the minus sign is typeset as a binary operator. I can solve this by surrounding the whole value in braces, but then the spacing around the equal sign is still not correct. For other combinations of intervals and operators, different spacing inconsistencies arise.

What is the correct and easy way to input intervals in order to avoid having to take care of this at all?

-
$$X = \left]-\frac{π}{2}, +\frac{π}{2}\right[$$ ? – Martin Scharrer Jul 20 '11 at 21:24
...or you could use wrap the interval in {}: $$X = ]{-\frac{π}{2}, +\frac{π}{2}}[$$. However, this would work best when the contents is not in displaymath mode. – Werner Jul 20 '11 at 21:33
@Martin and Werner: thanks for your responses. Please post them as answers so I can accept them. – Roberto Bonvallet Jul 20 '11 at 21:35

You can use \left] and \right[ then the brackets are taken as delimiters (and also get resized accordantly to the content):

$$X = \left]-\frac{π}{2}, +\frac{π}{2}\right[$$


-
I had overlooked this obvious solution, since I had always used \left and \right only for the resizing behavior. Thanks! – Roberto Bonvallet Jul 20 '11 at 21:43
Knuth speaks about "perverse mathematicians" that use this notation. :) In case the resizing is not needed, it's better to say \mathopen]a^2,b^2\mathclose[ (this is indeed a case where resizing is bad). – egreg Jul 20 '11 at 22:05
@egreg: Thanks, I didn't know \mathopen and \mathclose before. I'm not a mathematician at all, neither a perverse one or otherwise :-) – Martin Scharrer Jul 20 '11 at 22:50
@egreg, @Martin: Then all French mathematicians are perverse ;-) More seriously, the best for intervals is a macro which takes care of everything (and allows to choose between the resizing and non-resizing version). Since with \left/\right you can have spacing problems (e.g. in \left]a,b\right[. the dot is too far away) due to the fact that it makes what it encloses of type \mathinner instead of \mathopen to the left and \mathclose to the right, the best when you want the resizing behavior is to use \mathopen{}\mathclose{\left]...\right[}. – Philippe Goutet Jul 21 '11 at 8:29
@Philippe: I'm not fond of "automatic" \left and \right; in the case of the interval (sqrt(2),2), for example, or (a^2,b^2), using \left and \right produces too big delimiters. That all French mathematician are perverse is well known. :) – egreg Jul 21 '11 at 9:56

At least two options exist.

Option 1: $$X = \left]-\frac{π}{2}, +\frac{π}{2}\right[$$

This works well, even in displaymath mode ($...$) since the delimiters are extensible. However, in inline math mode you'll notice the braces may expand more than your liking. For that, use Option 2.

Option 2: Wrap the interval in {}: $$X = ]{-\frac{π}{2}, +\frac{π}{2}}[$$

-
 The latter option has the advantage that it doesn't clutter the markup too much. I may end up prefering it for simple cases. Thanks! – Roberto Bonvallet Jul 22 '11 at 23:18

Here is a macro which takes care of the spacing and allows you to choose between a resizable version (when followed by a star, for use in display) and a fixed-size version (without a star, for use in text). You have four macros \intervalcc, \intervaloo, \intervaloc and \intervalco for the various types of intervals (o is for open and c for closed):

\documentclass{article}

\makeatletter
\DeclareRobustCommand{\genericinterval}[2]{%
\@ifstar{\genericinterval@star{#1}{#2}}{\genericinterval@nostar{#1}{#2}}}
\newcommand{\genericinterval@star}[4]{\mathopen{}\mathclose{\left#1#3,#4\right#2}}
\newcommand{\genericinterval@nostar}[4]{\mathopen{#1}#3,#4\mathclose{#2}}
\newcommand{\intervalcc}{\genericinterval[]}
\newcommand{\intervaloo}{\genericinterval][}
\newcommand{\intervaloc}{\genericinterval]]}
\newcommand{\intervalco}{\genericinterval[[}
\makeatother

\begin{document}

$I = \intervalcc{-a^2}{b^2} \cap \intervalcc{-\frac{\pi}{2}}{\frac{\pi}{2}}.$

$I = \intervalcc{-a^2}{b^2} \cap \intervalcc*{-\frac{\pi}{2}}{\frac{\pi}{2}}.$

$I = \intervaloo{-a^2}{b^2} \cap \intervaloo{-\frac{\pi}{2}}{\frac{\pi}{2}}.$

$I = \intervaloo{-a^2}{b^2} \cap \intervaloo*{-\frac{\pi}{2}}{\frac{\pi}{2}}.$

$I = \intervaloc{-a^2}{b^2} \cap \intervaloc{-\frac{\pi}{2}}{\frac{\pi}{2}}.$

$I = \intervaloc{-a^2}{b^2} \cap \intervaloc*{-\frac{\pi}{2}}{\frac{\pi}{2}}.$

$I = \intervalco{-a^2}{b^2} \cap \intervalco{-\frac{\pi}{2}}{\frac{\pi}{2}}.$

$I = \intervalco{-a^2}{b^2} \cap \intervalco*{-\frac{\pi}{2}}{\frac{\pi}{2}}.$

$\intervaloo*{-\frac{\pi}{2}}{\frac{\pi}{2}}^2.$

\end{document}

-
 I don't like how the markup ends up looking like. Too much clutter! Thanks anyway, I always learn something new when looking at someone else's macros :) – Roberto Bonvallet Jul 22 '11 at 23:23 @Roberto: if your TeX editor uses different colors to highlight differently nested braces, the formulas remain very legible. The reason these macro are useful is if you need to do things more complicated with, e.g., the interval separator (I use \mathclose{}\mathpunct{}; for a correct spacing of the semi-colon in French and these macros allow me not to have to type it each time). – Philippe Goutet Jul 23 '11 at 12:39