# How to properly typeset math intervals

I have the code

\documentclass{amsart}

\begin{document}
\begin{equation}
\end{equation}
\end{document}


which produces Why is the minus sign on the left so far away from pi and how can this be avoided?

• Forgive my ignorance, but why do you want to write the brackets like that in the first place? – Phill Aug 24 '16 at 3:13
• I think you should accept an answer since you at least have a working one. No need to keep it unanswered. – Diaa Aug 24 '16 at 9:13
• @Phill It’s a common convention for denoting (half-)open intervals. (depending on culture/language). – Konrad Rudolph Aug 24 '16 at 10:40

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}
{-\pi} % this needs braces because it is between Ord atoms
\int_{-\pi}^0
\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. 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). It should be clear that simply bracing -\pi is not sufficient.

• Your answer made me smile (perhaps I am a perverse mathematician). I encountred this notation in the first book on higher mathematics I've read four years ago: Analysis I by Vladimir A. Zorich. And since then the notation stayed with me. Somehow I prefer it to $(a,b)$, since for me $(a,b)$ represents and ordered pair. Why do you find it awkward? I think it is a matter of choice. – TheGeekGreek Aug 24 '16 at 8:58
• @TheGeekGreek I too suffered from the disease, because as a freshman I was taught that (French originated, possibly Bourbaki) notation. In my calculus notes I use (a .. b) for open intervals, [a .. b] for closed intervals and mix parentheses and brackets for half-open intervals; I learnt the double dot notation from a paper of Knuth's and I like it. – egreg Aug 24 '16 at 9:05
• @egreg if \in is replaced with \in\  in Werner solution, would it be printed correctly? – Diaa Aug 24 '16 at 9:11
• @egreg My professor uses the same notation (without the double dots). Perhaps I will like it more as time goes by. But I will definitely have a closer look on Knuths TeXbook. Thanks a lot. – TheGeekGreek Aug 24 '16 at 9:11
• @TheGeekGreek I also used square brackets $[a,b]$ in the beginning. Then I noticed that the parentheses reminded me somehow that $a$ and $b$ are limit points. The rounded shape is a bit like it gets close but never reaches. This sounded cool in my mind and I switched. – Spak Aug 24 '16 at 21:19

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}{]}{[}{#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: TeX considers your usage as ] minus \pi. Remove this ambiguity by using (say) {-\pi}: \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 • Typically braces, brackets and parentheses are used in its closed form for grouping content. There's no necessary indication that ] represents a "closed interval" to TeX... it may just as well be the end of a previous term. – Werner Aug 23 '16 at 22:23
• @Bernard -- these possibilities were considered, but the frequency with which they occur is much less than the "usual" forms, and knuth would simply have made macros that "do the right thing" instead of trying to detect such anomalous uses automatically. i'm not able to check references at the moment, but places to look are the texbook and manmac.tex for a start. index terms to check might be "open", "closed" and "interval". – barbara beeton Aug 24 '16 at 0:45
• I can't understand how this wrong answer got so many votes. – egreg Aug 24 '16 at 7:58
• Just try $x \in ]{-\pi},0[$ to see how much this is wrong. Please, remove this. – egreg Aug 24 '16 at 8:21
• The edit doesn't really improve the quality, I'm afraid. – egreg Aug 24 '16 at 19:54

I usually care also about the input, readability and usability. Related to this other answer, you can do something like

\def\intv#1]#2[{\mathopen{#1]}#2\mathclose{#1[}}


That way you can use easily \intv]a,b[, but also “extend” the size with something like \intv\Big]a,b[.

Full code

\def\intv#1]#2[{\mathopen{#1]}#2\mathclose{#1[}}
$\intv]a,b[ \intv\big]a,b[ \intv\Bigg]\frac{a}{b},c[$


Taking egreg's suggestion about interval package, the suggestion is still the same, to have a nice and easy to input interface, for instance

\usepackage{interval}
\usepackage{etoolbox}
\def\intv#1]#2,#3[{\ifblank{#1}{\interval[open]}{\interval[open,scaled=#1]}{#2}{#3}}
..
I prefer
$x \in \intv]-\pi,0[$
rather than
$x \in \interval[open]{-\pi}{0}$


You might also be interested in my solution, which allows to simply type \interv]-a,+b[, \interv]{1,5},2], etc., with the appropriate spacing. As improved by egreg's 1st comment, it also works in headings and the table of contents.

• Thanks. I will surely have a look at it, since egreg also commented there. For the moment I will stick to the package mentioned above. – TheGeekGreek Aug 24 '16 at 10:48