# How to draw intervals in latex using tikz or otherwise?

Sometimes we require to represent multiple intervals on a single number line so that we could see their intersection easily. One illustration is given below

I can draw line and make nodes using tikz but I don't know how to draw line segments above the number line.

Please help me.

• Can you share a Minimum Working Example (MWE) of the line and nodes you can draw-- so that contributors could develop on your base example, rather than coding from scratch ? Jan 8 '19 at 11:37

## 2 Answers

One way to draw the line segments that are above the number line is to draw the line segment first \draw (x1,y) -- (x2,y) and then put circles with filling over them using \draw[fill=white] (x,y) circle (radius) for a white circle or \fill (x,y) circle (radius) for a black circle. So the final code would look something like this

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=0.5]
\draw (-12,0)-- (9,0); %Axis
\foreach \x in {-10,1,5,7} {
\draw (\x,0.5) -- (\x,-0.5) node[below] {\x};
}
\draw (-10,1) -- (5,1);
\draw[fill=white] (-10,1) circle (0.25);
\fill (5,1) circle (0.25);
\draw (1,2) -- (7,2);
\fill (1,2) circle (0.25);
\draw[fill=white] (7,2) circle (0.25);
\end{tikzpicture}
\end{document}


# EDIT

Denis in the comments section suggested changing \draw (\x,0.5) -- (\x,0.5) node[below] {\x}; to \draw (\x,0.5) -- (\x,0.5) node[below] {$\x$}; to get a mathematical minus sign. However upon doing this the -10 becomes off centred too much for my liking. To recenter the number you can add a phantom (invisible) minus character after the 10 to recenter it. This is done by writing \draw (-10,0.5) -- (-10,0.5) node[below] {$-10\hphantom{-}$};

Therefore the final code can be written as:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=0.5]

\draw (-12,0)-- (9,0); %AXIS
\foreach \x in {1,5,7} {
\draw (\x,0.5) -- (\x,-0.5) node[below] {$\x$};
}
\draw (-10,1) -- (5,1);
\draw[fill=white] (-10,1) circle (0.25);
\fill (5,1) circle (0.25);
\draw (1,2) -- (7,2);
\fill (1,2) circle (0.25);
\draw[fill=white] (7,2) circle (0.25);
\draw (-10,0.5) -- (-10,-0.5) node[below] {$-10\hphantom{-}$};
\end{tikzpicture}
\end{document}


Which yields:

• (there's no line at the top, that's just my bad screenshot skills Jan 8 '19 at 12:46
• I would put the numbers in math mode. The $-10$ is not nice as it is. Jan 8 '19 at 12:51
• Will be creating an edit that centres the -10 on the number now Jan 8 '19 at 12:57
• Just a hint for screenshotting: Use a border with standalone (class option) to get some white space around the content. That will help avoiding these lines. Jan 8 '19 at 15:19

A very simple code with pstricks:

\documentclass[border=3pt]{standalone}
\usepackage{pst-node }
\usepackage{auto-pst-pdf}

\begin{document}

\psset{xunit = 0.5cm}
\begin{pspicture}(-12,-0.4)(8,1.2)
\psline(-12,0)(10,0)
\dotnodes[dotstyle=|](-10,0){A}(1,0){B}(5,0){C}(7,0){D}
\foreach \n/\lbl in {A/-10,B/1,C/5,D/7}{\uput[d](\n){$\lbl$}}
\psset{linewidth=0.6pt}
\pcline[offset=16pt]{o-*}(A)(C)
\pcline[offset=30pt]{*-o}(B)(D)
\end{pspicture}

\end{document}