非常简单的带点数轴

非常简单的带点数轴

第一次使用 LaTeX。我读了这里的一些问题,但我仍然感到困惑。

我想要做的就是在 -3 和 3 之间画一条简单的数轴,并绘制一些点,比如 0.5、0.7 和 0.9。

我怎么做?

答案1

你是指类似

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

该图显示了上述代码的结果

答案2

一个纯粹的tikz替代方案:

在此处输入图片描述

\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$}; % tick and their labels
    \foreach \i in {0.5, 0.7, 0.9}% points on line
      \fill[red] (\i,0) circle (0.6 mm);
  \end{tikzpicture}
\end{document}

答案3

这是我之前做的一个改编版。代码旨在支持各种选项,因此在这种情况下,并非所有代码都必然要绘制简单的线条。事实上,还添加了其他选项来关闭其中一些功能。

这使得序言有点复杂,但如果你喜欢这种东西,它可能会很有用。然而,实际数字线的代码并不特别复杂。

\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
]

数轴

\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,{-\[email protected]}) {$\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}

答案4

PSTricks 解决方案。

相同颜色的圆点

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

输出1

请注意,如果点在线上等距分布,则可以使用\multido宏来概括代码。

不同颜色的圆点

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

输出B

相关内容