9

First time using LaTeX. I read some questions on here but I am still confused.

All I want to do is to draw a simple number line between -3 and 3, and plot some points, say 0.5, 0.7, and 0.9.

How do I do that?

26

Do you mean something like

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            % center the x axis
            axis x line=middle,
            % we don't need a y axis line ...
            axis y line=none,
            % ... and thus there is no need for much `height' of the axis
            height=50pt,
            % but `height' also changes `width' which is restored here
            width=\axisdefaultwidth,
            xmin=-3,
            xmax=3,
        ]
            \addplot coordinates {
                (0.5,0) (0.7,0) (0.9,0)
            };
        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

23

A pure tikz alternative:

enter image description here

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
  \begin{tikzpicture}
    \draw (-3,0) -- (3,0);
    \foreach \i in {-3,-2,...,3} % numbers on line
      \draw (\i,0.1) -- + (0,-0.2) node[below] {$\i$};
    \foreach \i in {0.5, 0.7, 0.9}% points on line
      \fill[red] (\i,0) circle (0.6 mm);
  \end{tikzpicture}
\end{document}
  • 1
    How about putting the \i inside $ so the minus signs are right? – Stefan Pinnow Oct 8 '16 at 19:51
  • 1
    oh, i have filling that i would forget something ... thank you!. corrected now. – Zarko Oct 8 '16 at 19:54
14

With a simple picture environment without TikZ:

\documentclass{article}
\usepackage{color}
\begin{document}

\setlength{\unitlength}{1cm}
\makeatletter
%\fboxsep=.1pt \fbox{% debug for bounding box
\begin{picture}(6.3,.55)(-3.21,-.45)
  \put(-3, 0){\line(1, 0){6}}
  \multiput(-3, -.1)(1, 0){7}{\line(0, 1){.2}}
  \@for\x:=-3,-2,-1,0,1,2,3\do{%
    \put(\x, -.2){\makebox(0, 0)[t]{$\x$}}%
  }%
  \color{red}%
  \@for\x:=.5,.7,.9\do{%
    \put(\x, 0){\circle*{.1}}%
  }%
\end{picture}%
% }% end of \fbox
\makeatother

\end{document}

Result

13

Here's an adaption of one I made earlier. The code is designed to support various options so not all of it is necessarily to draw the simple line in this case. Additional options have, in fact, been added to turn some of those features off.

This makes the preamble a bit complex, though potentially useful if you like that kind of thing. However, the code for the actual numberline is not especially complex.

\NumberLine*[
  dot=blue,% colour of dots marking points on the line
  dot opacity=.75,% opacity of dots
  fraction=1,% we're only marking whole numbers
  ticks above=false,% we only want standard axis marks below
  min=-3,% start from here - max is 3 by default
  number to=none,% we don't want to visualise a number as drawn along a fraction of the line
  h scale=3,% scale the line a bit as the marks are too close otherwise
  mark at={0.5,0.7,0.9},% points we want to mark on the line
]

numberline

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{xparse}
\makeatletter
\newif\ifnl@mixednumbers
\newif\ifnl@ticksabove
\newif\ifnl@isnumberto
\tikzset{% http://tex.stackexchange.com/a/159856/ - Claudio Fiandrino
  number line/.code={
    \tikzset{
      /number line/.cd,%
      #1
    }
  },
  /number line/.cd,
  dot/.store in=\nl@dot,
  dot opacity/.store in=\nl@dotopacity,
  fill/.store in=\nl@fill,
  fraction/.store in=\nl@fraction,
  h scale/.store in=\nl@hscale,
  line height/.store in=\nl@lineht,
  line above/.store in=\nl@lineabove,
  line below/.store in=\nl@linebelow,
  mark at/.store in=\nl@markat,
  max/.store in=\nl@max,
  min/.store in=\nl@min,
  mixed numbers/.is if=nl@mixednumbers,
  mixed numbers/.default=true,
  number to/.store in=\nl@numberto,
  number from/.store in=\nl@numberfrom,
  ticks above/.is if=nl@ticksabove,
  ticks above/.default=true,
  v scale/.store in=\nl@vscale,
  fraction=4,
  v scale=1.2,
  h scale=4,
  max=3,
  min=0,
  number to={7/4},
  number from=0,
  mixed numbers=false,
  fill=orange,
  dot=green,
  dot opacity=.75,
  ticks above,
  mark at=,
  line height=0.2,
  line above=0.2,
  line below=0.2,
}
\newcommand*\tnl{% modified from ref: WeCanLearnAnything at http://tex.stackexchange.com/questions/267921/macro-for-mixed-numbers-on-number-line-tikz (but I doubt this is the original source)
  \begin{scope}[xscale=\nl@hscale,yscale=\nl@vscale]
    \edef\tempa{none}%
    \edef\tempb{\nl@numberto}%
    \ifx\tempa\tempb
      \nl@isnumbertofalse
    \else\nl@isnumbertotrue
      \filldraw[\nl@fill] (\nl@numberfrom,0) rectangle (\nl@numberto,\nl@lineht);% shaded portion of number line
    \fi
    \draw
    (\nl@min,0)--(\nl@max,0)% lower part of x-axis
    (\nl@min,\nl@lineht)--(\nl@max,\nl@lineht);% higher part of x-axis
    \foreach \x in {\nl@min,...,\nl@max}
      \node [anchor=mid] at (\x,-0.5) {\x};% whole numbers underneath number line
    \pgfmathsetmacro\nl@maxfrac{int(\nl@max*\nl@fraction)}
    \pgfmathsetmacro\nl@minfrac{int(\nl@min*\nl@fraction)}
    \foreach \x in {\nl@minfrac,...,\nl@maxfrac}% fractional tick marks and numbers above number line
    {%
      \draw (\x/\nl@fraction,-\nl@linebelow)--(\x/\nl@fraction,\nl@lineht);
      \ifnl@ticksabove
        \node[above] at (\x/\nl@fraction,{\nl@lineht+0.05}) {$\frac{\x}{\nl@fraction}$}; % draw tick and mark fraction above line
      \fi
      \ifnl@mixednumbers
        \pgfmathsetmacro\intbit{int(\x/\nl@fraction)}% integer bit
        \pgfmathsetmacro\fracbit{int(\x-\nl@fraction*\intbit)}% proper fraction bit
        \ifnum\intbit=0\let\intbit\relax\fi % don't print 0 in mixed numbers
        \ifnum\fracbit=0\else
          \node [anchor=mid] at (\x/\nl@fraction,{-\nl@linebelow-0.3}) {$\intbit\frac{\fracbit}{\nl@fraction}$}; % mark the mixed number below the line
        \fi
      \fi
    }%
    \ifnl@isnumberto
      \fill[\nl@dot,opacity=\nl@dotopacity] (\nl@numberto,.5*\nl@lineht) circle[x radius=\nl@lineht cm/\nl@hscale,y radius=\nl@lineht cm/\nl@vscale];% dot
    \fi
    \foreach \i in \nl@markat
    {%
      \draw (\i,0) -- (\i,{\nl@lineht+\nl@lineabove}) node [above] {\i};% draw tick to node above line to mark point specified with mark at
      \fill[\nl@dot,opacity=\nl@dotopacity] (\i,.5*\nl@lineht) circle[x radius=\nl@lineht cm/\nl@hscale,y radius=\nl@lineht cm/\nl@vscale];% dot
    };
  \end{scope}}
\NewDocumentCommand \NumberLine { s O {} }{%
  \IfBooleanTF {#1}{%
    \tikz[number line={mixed numbers=false,#2}]\tnl;%
  }{%
    \tikz[number line={mixed numbers=true,#2}]\tnl;%
  }}
\makeatother

\begin{document}

\NumberLine*[
  dot=blue,
  dot opacity=.75,
  fill=blue,
  fraction=1,
  ticks above=false,
  min=-3,
  number to=none,
  h scale=3,
  mark at={0.5,0.7,0.9},
]

\end{document}
12

A PSTricks solution.

Same colored dots

\documentclass{article}

\usepackage{pstricks}
\usepackage{multido}

\def\Dot(#1){\psdot[dotstyle = o, fillcolor = blue](!#1 0)}

\begin{document}

\begin{pspicture}(-3.2,-0.4)(3.05,0.1) % boundry found manually
  \psline{->}(-3,0)(3,0)
  \multido{\i = -3+1}{7}{%
    \psline(\i,-0.1)(\i,0.1)
    \uput[270](\i,-0.1){$\i$}}
  \Dot(0.5)
  \Dot(0.7)
  \Dot(0.9)
\end{pspicture}

\end{document}

output1

Note that if the points are equidistantly distributed on the line, you can use the \multido macro to generalise the code.

Different colored dots

\documentclass{article}

\usepackage{pstricks}
\usepackage{multido}

\def\Dot[#1](#2){\psdot[dotstyle = o, fillcolor = #1](!#2 0)}

\begin{document}

\begin{pspicture}(-3.2,-0.4)(3.05,0.1) % boundry found manually
  \psline{->}(-3,0)(3,0)
  \multido{\i = -3+1}{7}{%
    \psline(\i,-0.1)(\i,0.1)
    \uput[270](\i,-0.1){$\i$}}
  \Dot[blue](0.5)
  \Dot[yellow](0.7)
  \Dot[red](0.9)
\end{pspicture}

\end{document}

outputB

8

Another example with PSTricks. Irrational numbers are the features of my answer!

\documentclass[pstricks,margin=10mm,preview]{standalone}
\usepackage{pst-plot}

\newcommand{\xpoint}[2][white]{%
    \pscircle[fillstyle=solid,fillcolor=#1](!#2 0){3pt}}

\begin{document}
\begin{pspicture}(-4,0)(5,0)
    \psaxes[xAxis=true](0,0)(-4,0)(5,0)
    \xpoint{2 sqrt} % the square root of 2
    \xpoint[red]{4 sqrt}% the square root of 4
\end{pspicture}
\end{document}

enter image description here

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.