26

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?

4
  • 3
    \(X = \left]-\frac{π}{2}, +\frac{π}{2}\right[\) ? Jul 20, 2011 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, 2011 at 21:33
  • @Martin and Werner: thanks for your responses. Please post them as answers so I can accept them. Jul 20, 2011 at 21:35
  • 2
    take a look at \DeclarePairedDelimiter from mathtools. (i haven't time to write a more complete answer.) Sep 28, 2013 at 21:52

5 Answers 5

31

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[\)

Result

8
  • 1
    I had overlooked this obvious solution, since I had always used \left and \right only for the resizing behavior. Thanks! Jul 20, 2011 at 21:43
  • 21
    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, 2011 at 22:05
  • 1
    @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[}. Jul 21, 2011 at 8:29
  • 3
    @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, 2011 at 9:56
  • 1
    @egreg: yes, but in display mode, you might need to use \left/\right and it's still important to have correct spacing. Jul 21, 2011 at 12:13
19

While I'm a little late to the party, here goes for a slightly more concise solution:

\usepackage{xparse}

\NewDocumentCommand{\INTERVALINNARDS}{ m m }{
    #1 {,} #2
}
\NewDocumentCommand{\interval}{ s m >{\SplitArgument{1}{,}}m m o }{
    \IfBooleanTF{#1}{
        \left#2 \INTERVALINNARDS #3 \right#4
    }{
        \IfValueTF{#5}{
            #5{#2} \INTERVALINNARDS #3 #5{#4}
        }{
            #2 \INTERVALINNARDS #3 #4
        }
    }
}

which allows you to do:

\begin{align*}
    \interval[{a,b}] &&           && \\
    \interval[{a,b}) && \text{or} && \interval[{a,b}[ \\
    \interval({a,b}] && \text{or} && \interval]{a,b}] \\
    \interval({a,b}) && \text{or} && \interval]{a,b}[
\end{align*}

to obtain something like: Interval open / closed example

Or to use manual or automatic resizing:

\begin{equation*}
    \interval[{a,\infty})[\Bigg] \quad \text{or} \quad
        \interval*[{-\frac{\pi}{2},\frac{\pi}{2}}]
\end{equation*}

to obtain:

enter image description here

Edit: Added the changes suggested by egreg (this was my answer, was just unregistered at the time).

1
  • Welcome to TeX.SX! This is nice; I'd exchange the conditions and use * to get the \left-\right version: I don't like this kind of automation that can give unwanted spacing; also macros defined with \DeclarePairedDelimiters (mathtools package) use the *-version for the "automatic size" version. It wouldn't be difficult to add an optional argument for accommodating \big and friends in a similar fashion.
    – egreg
    Sep 28, 2013 at 21:25
12

The interval package takes care of the spacing and the scaling of the brackets.

Edit. An example code:

\documentclass{article}
\usepackage{interval}

\begin{document}

    \[ \interval[scaled]{-\frac{\pi}{2}}{\frac{\pi}{2}}\qquad\interval[scaled=\bigg]{-\frac{\pi}{2}}{\frac{\pi}{2}} \]%

\end{document} 

enter image description here

2
  • Could you add an example?
    – shuhalo
    Feb 27, 2020 at 22:56
  • @shuhalo: Sorry, I've just discovered your question. I've added an example. Note such command can also be defined with \DeclarePairedDelimiter from mathtools.
    – Bernard
    Mar 15, 2020 at 12:21
4

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}}[\)


4
  • 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! Jul 22, 2011 at 23:18
  • The latter option has the drawback, that it does not give proper spacing with \in, e.g. x\in]{-2},2[ Aug 21, 2017 at 7:51
  • @PhilippImhof: Then use $x \in {}]{-2},2[$.
    – Werner
    Aug 21, 2017 at 9:56
  • 2
    Thanks. I rather stick to \mathopen and \mathclose. It's more typing, but its logic is straight forward. Aug 21, 2017 at 11:46
3

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}
2
  • 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 :) Jul 22, 2011 at 23:23
  • 1
    @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). Jul 23, 2011 at 12:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .