1

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

enter image description here

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.

  • 2
    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 ? – Partha D. Jan 8 at 11:37
2

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}

enter image description here

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:

enter image description here

  • (there's no line at the top, that's just my bad screenshot skills – sab hoque Jan 8 at 12:46
  • I would put the numbers in math mode. The $-10$ is not nice as it is. – Denis Jan 8 at 12:51
  • Will be creating an edit that centres the -10 on the number now – sab hoque Jan 8 at 12:57
  • 1
    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. – TeXnician Jan 8 at 15:19
2

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} 

enter image description here

  • -10 on the label – sab hoque Jan 8 at 13:18
  • 1
    Sorry for the typo! I've fixed it (and changed the code accordingly). – Bernard Jan 8 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.