How to properly typeset math intervals

In the TeXbook, Knuth refers to people using “]a,b[” notation for open intervals as “perverse mathematicians” (page 171, exercise 18.14). I don't fully agree with the adjective, but I find the notation very awkward nonetheless.

There is already a package for this, which avoids reinventing the wheel:

\documentclass{amsart}
\usepackage{interval}

\begin{document}

\begin{equation}
\interval[open]{-\pi}{0}
 \qquad
{-\pi} % this needs braces because it is between Ord atoms
 \qquad
\int_{-\pi}^0
 \qquad
\interval{-\pi}{0}
\end{equation}

\begin{equation}
x\in\interval[open]{-\pi}{0}
\end{equation}

\end{document}

The middle -\pi needs braces, but it's a very different problem.

See the documentation for interval to learn about other options.

enter image description here


Note about Werner's solution

Consider the following code and compile it to see what results:

$x \in ]{-\pi},0[$

$x \in \mathopen]-\pi,0\mathclose[$

(properly embed it in a standard document).

enter image description here

It should be clear that simply bracing -\pi is not sufficient.


TeX will provide the correct spacing if you inform it that you are using ] and [ in a non-standard way, which can be done by means of \mathopen and \mathclose:

\[ \mathopen]-\pi,0\mathclose[ \]

This tells TeX exactly what is going on.

More precisely, TeX assumes that [ is an Open[ing] atom and the ] is a Close[ing] one (and I sympathize with it! ;-) If you write

\[ ]-\pi,0[ \]

TeX will build the following list of atoms: Close, Bin, Ord, Punct, Ord, Open; this leads it to compute the difference between ] and \pi, as Werner has already said; indeed, compare the above with the formula

(a+b)-c

where the minus is the second Bin atom in the resulting sequence Open, Ord, Bin, Ord, Close, Bin, Ord.

Note that Werner’s suggestion, that is,

]{-\pi},0[

yields Close, Ord{…}, Punct, Ord, Open, which does not correspond to the intended meaning, although it gives the correct spacing (but only, of course, in this particular case—see @egreg’s answer), as you can check in the table on p. 170 of The TeXbook.

IMHO, however, the best thing of all is to define an abstract command: it’s very easy to do so using the mathtools package and its \DeclarePairedDelimiterX command; the command defined in this way provides easy means to deal with size issues: see the documentation of the mathtools package, subsection 3.6, for details.

Here is a complete example that illustrates both solutions:

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage{mathtools}

% [ bracket matching
\DeclarePairedDelimiterX{\openinterval}[2]{]}{[}{#1,#2}
% ] bracket matching



\begin{document}

Wrong:
\[ ]-\pi,0[ \]
Direct method, not recommended:
\[ \mathopen]-\pi,0\mathclose[ \]
With an abstract command:
\[ \openinterval{-\pi}{0} \]

Variants of the abstract command:
\begin{align*}
    &\openinterval*{-\frac{\pi}{2}}{+\frac{\pi}{2}}
            && \text{auto-resizing;}  \\
    &\openinterval[\bigg]{-\frac{\pi}{2}}{+\frac{\pi}{2}}
            && \text{with optional argument for size specification.}
\end{align*}

\end{document}

And here is the output it produces:

Output of the code sample


TeX considers your usage as ] minus \pi. Remove this ambiguity by using (say) {-\pi}:

enter image description here

\documentclass{article}

\begin{document}

\[
  ]{-\pi},0[ \qquad -\pi \qquad \int_{-\pi}^0
\]

\end{document}

For more complex interactions, consider using Gustavo's answer. For example, to show membership or another relation/operator with respect to the set, you'll have to use an empty atom for proper spacing:

x \in{} ]{-\pi},0[ {}\ni x

enter image description here