Graphics, Figures & TablesNumber Line Help

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
tpa77l3
Posts: 19
Joined: Mon Feb 24, 2020 9:49 pm

Number Line Help

Post by tpa77l3 »

Hey guys,

I'm completely new to LaTeX and have thrown myself somewhat in the deep end when starting a maths course.

I would like some help creating a number line with another line above it. The higher of the two lines requires dots on either end; one filled and one not. Example attached.

Any pointers would be greatly appreciated. Thanks in advance.
Screenshot 2020-02-24 at 20.06.44.png
Screenshot 2020-02-24 at 20.06.44.png (35.01 KiB) Viewed 10386 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Bartman
Posts: 369
Joined: Fri Jan 03, 2020 2:39 pm

Number Line Help

Post by Bartman »

It should be easier for beginners to create the drawing with pgf/TikZ. My solution uses pgfplots:

Code: Select all

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    x=3mm,
    axis y line=none,
    axis x line=center,
    xtick align=outside,
    xtick={-5,0,...,30},
    xmin=-7, xmax=33, 
    ymin=0, ymax=1,
    xlabel=$x$,
    xlabel style={anchor=north}
]
\addplot [nodes=draw, circle, inner sep=1.5pt] 
    coordinates {(-5,.1) (25,.1)} 
        node [fill, at start] {}
        node [fill=white] {}
;
\end{axis}
\end{tikzpicture}
\end{document}
A possible replacement for the command \addplot:

Code: Select all

\draw [nodes=draw, circle, inner sep=1.5pt]
    (-5,.1) node (filled) [fill] {}
    (25,.1) node (open) {}
    (filled) -- (open)
;
tpa77l3
Posts: 19
Joined: Mon Feb 24, 2020 9:49 pm

Number Line Help

Post by tpa77l3 »

This is perfect - thank you!

Just one more addition I need to may if you wouldn't mind. I need to include a label above the top line as below:
Screenshot 2020-02-25 at 05.50.46.jpg
Screenshot 2020-02-25 at 05.50.46.jpg (27.29 KiB) Viewed 10365 times
Bartman
Posts: 369
Joined: Fri Jan 03, 2020 2:39 pm

Number Line Help

Post by Bartman »

Code: Select all

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{tikzpicture}[
    dot/.style={draw, fill=#1, circle, inner sep=1.5pt},
    filled/.style={dot},
    open/.style={dot=white}
]
\begin{axis}[
    x=3mm,
    axis y line=none,
    axis x line=center,
    xtick align=outside,
    xtick={-5,0,...,30},
    xmin=-7, xmax=33, 
    ymin=0, ymax=1,
    xlabel=$x$,
    xlabel style={anchor=north}
]
\addplot [black]
    coordinates {(-5,.1) (25,.1)} 
        node [filled, at start] {}
        node [open] {}
        node [above, midway] {A}
;
\end{axis}
\end{tikzpicture}
\end{document}
The other way:

Code: Select all

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{tikzpicture}[
    dot/.style={draw, circle, inner sep=1.5pt},
    filled/.style={dot, fill},
    open/.style={dot}
]
\begin{axis}[
    x=3mm,
    axis y line=none,
    axis x line=center,
    xtick align=outside,
    xtick={-5,0,...,30},
    xmin=-7, xmax=33, 
    ymin=0, ymax=1,
    xlabel=$x$,
    xlabel style={anchor=north}
]
\draw
    (-5,.1) node [filled] (filled) {}
    (25,.1) node [open] (open) {}
    (filled) -- node [above] {A} (open)
;
\end{axis}
\end{tikzpicture}
\end{document}
Last edited by Bartman on Wed Feb 26, 2020 2:33 am, edited 1 time in total.
tpa77l3
Posts: 19
Joined: Mon Feb 24, 2020 9:49 pm

Number Line Help

Post by tpa77l3 »

Much appreciated :D
Post Reply