新界面

新界面

我该如何改变这个宏来将混合数字放在数字线下方?

在这个特定的例子中,我想要 1 1/4、1 2/4、1 3/4、2 1/4 等等。

不知道这是否需要一个单独的线程,但是有没有办法让宏选项是否包括这些混合数字?

我知道这不是真正的最小这是一个可行的示例,但我认为它使上下文更加清晰。

\documentclass{article}
\usepackage{tikz}

\newcommand{\NL}[5]
{\tikz[xscale=#1,yscale=#2]
 {
 \filldraw[orange] (0,0) rectangle (#5,0.2); %shaded portion of number line
 \draw
  (0,0)--(#3,0) %lower part of x-axis
  (0,0.2)--(#3,0.2); %higher part of x-axis
 \foreach \x in {0,...,#3}
  \node[below] at (\x,-0.2) {\x}; %whole numbers underneath number line
  \pgfmathparse{#3*#4}
 \foreach \x in {0,...,\pgfmathresult} %fractional tick marks and numbers above number line
  {
   \draw (\x/#4,-0.2)--(\x/#4,0.2);
   \node[above] at (\x/#4,0.25) {$\frac{\x}{#4}$};
  }
 \fill[green,opacity=0.75] (#5,0.1) circle[x radius=0.2cm/#1,y radius=0.2cm/#2]; %green dot
 }
}

\begin{document}

Draw and label a number line from 0 to 3 with tick marks at every quarter, emphasizing \(\frac{7}{4}=1\frac{3}{4}\). Show your answer using both a length and a point.

\NL{4}{1.2}{3}{4}{7/4}
%{x-scale}{y-scale}{from 0 to 3}{denominator}{emphasized point}
\end{document}

该代码产生如下结果:

数字线宏

答案1

也许是这样的?

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{xparse}
% 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)
\NewDocumentCommand\NL { s m m m m m }
{\tikz[xscale=#2,yscale=#3]
 {
 \filldraw[orange] (0,0) rectangle (#6,0.2);% shaded portion of number line
 \draw
  (0,0)--(#4,0)% lower part of x-axis
  (0,0.2)--(#4,0.2);% higher part of x-axis
 \foreach \x in {0,...,#4}
  \node [anchor=mid] at (\x,-0.5) {\x};% whole numbers underneath number line
  \pgfmathparse{#4*#5}
 \foreach \x in {0,...,\pgfmathresult}% fractional tick marks and numbers above number line
  {
   \draw (\x/#5,-0.2)--(\x/#5,0.2);
   \node[above] at (\x/#5,0.25) {$\frac{\x}{#5}$};
   \IfBooleanF {#1}{
     \pgfmathsetmacro\intbit{int(\x/#5)}
     \pgfmathsetmacro\fracbit{int(\x-#5*\intbit)}
     \ifnum\intbit=0\let\intbit\relax\fi
     \ifnum\fracbit=0\else
     \node [anchor=mid] at (\x/#5,-0.5) {$\intbit\frac{\fracbit}{#5}$};
     \fi
   }
  }
 \fill[green,opacity=0.75] (#6,0.1) circle[x radius=0.2cm/#2,y radius=0.2cm/#3];% green dot
 }
}

\begin{document}

\NL{4}{1.2}{3}{4}{7/4}
\NL*{4}{1.2}{3}{4}{7/4}

\end{document}

现在你看到它们 - 现在你看不到它们

新界面

但是,我很想重新考虑用户界面。很难记住需要多少个参数以及哪个是哪个。另外,如果能够改变线条的其他方面就好了:点的颜色和填充、点的不透明度等等。

为了实现这一点,您可以使用键值接口。由于您已经在使用 TikZ,因此使用其密钥管理可能是明智的。

我还会配置命令,以便您可以将一个或多个数字线包含到更大的图片中,并且使您在设置键时尽可能灵活。

以下是一份草稿,当然可以通过多种方式进行微调:

  • \tnl在 TikZ 图片内创建数字线,因此\tikz\tnl;将该线绘制为独立图片;

  • number line={<number line settings>}是 TikZ 的一个选项,可以按通常的方式设置,例如\begin{tikzpicture}[number line={<number line settings>}]\tikzset{number line={<number line settings>}}等。默认设置基于您的 MWE。它设置数字线的各种属性:

    • fraction=<opt> 分母4
    • v scale=<opt> 垂直缩放1.2
    • h scale=<opt> 水平扩展4
    • max=<opt> 最高数字3
    • number to=<opt> 填充线至{7/4}
    • mixed numbers=<opt> 打印混合数字false
    • fill=<opt> 填充线颜色orange
    • dot=<opt> 点颜色green
    • dot opacity=<opt> 点不透明度.75
  • \NumberLine*[<number line settings>]旨在用于 TikZ 环境之外并创建独立数字线。带星号的版本不打印混合数字。

首先,仅使用默认值重现早期代码的输出,以检查输出是否匹配:

\NumberLine

数轴

\NumberLine*

数轴

具有一些非默认设置的数字线:

\NumberLine[
  dot=red,
  dot opacity=.5,
  fill=blue,
  fraction=3,
  number to={2/3},
  h scale=5,
]

数轴

让我们改变默认值:

\tikzset{
  number line={
    fraction=5,
    mixed numbers=true,
    number to={8/5},
    h scale=2,
    v scale=1.25,
    max=6,
  }
}

并尝试将两个数轴组合成一张图片:

\begin{tikzpicture}
  \tnl;
  \begin{scope}[yshift=-30mm]
    \tnl;
  \end{scope}
\end{tikzpicture}

数轴 x 2

完整代码:

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{xparse}
\makeatletter
\newif\ifnl@mixednumbers
\tikzset{% http://tex.stackexchange.com/a/159856/ - Claudio Fiandrino
  number line/.code={
    \tikzset{
      /number line/.cd,%
      #1
    }
  },
  /number line/.cd,
  fraction/.store in=\nl@fraction,
  v scale/.store in=\nl@vscale,
  h scale/.store in=\nl@hscale,
  max/.store in=\nl@max,
  number to/.store in=\nl@numberto,
  mixed numbers/.is if=nl@mixednumbers,
  fill/.store in=\nl@fill,
  dot/.store in=\nl@dot,
  dot opacity/.store in=\nl@dotopacity,
  fraction=4,
  v scale=1.2,
  h scale=4,
  max=3,
  number to={7/4},
  mixed numbers=false,
  fill=orange,
  dot=green,
  dot opacity=.75,
}
\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]
    \filldraw[\nl@fill] (0,0) rectangle (\nl@numberto,0.2);% shaded portion of number line
    \draw
    (0,0)--(\nl@max,0)% lower part of x-axis
    (0,0.2)--(\nl@max,0.2);% higher part of x-axis
    \foreach \x in {0,...,\nl@max}
      \node [anchor=mid] at (\x,-0.5) {\x};% whole numbers underneath number line
    \pgfmathparse{\nl@max*\nl@fraction}
    \foreach \x in {0,...,\pgfmathresult}% fractional tick marks and numbers above number line
    {
      \draw (\x/\nl@fraction,-0.2)--(\x/\nl@fraction,0.2);
      \node[above] at (\x/\nl@fraction,0.25) {$\frac{\x}{\nl@fraction}$};
      \ifnl@mixednumbers
        \pgfmathsetmacro\intbit{int(\x/\nl@fraction)}
        \pgfmathsetmacro\fracbit{int(\x-\nl@fraction*\intbit)}
        \ifnum\intbit=0\let\intbit\relax\fi
        \ifnum\fracbit=0\else
          \node [anchor=mid] at (\x/\nl@fraction,-0.5) {$\intbit\frac{\fracbit}{\nl@fraction}$};
        \fi
      \fi
    }
    \fill[\nl@dot,opacity=\nl@dotopacity] (\nl@numberto,0.1) circle[x radius=0.2cm/\nl@hscale,y radius=0.2cm/\nl@vscale];% green 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

\NumberLine*

\NumberLine[
  dot=red,
  dot opacity=.5,
  fill=blue,
  fraction=3,
  number to={2/3},
  h scale=5,
]

\tikzset{
  number line={
    fraction=5,
    mixed numbers=true,
    number to={8/5},
    h scale=2,
    v scale=1.25,
    max=6,
  }
}
\begin{tikzpicture}
  \tnl;
  \begin{scope}[yshift=-30mm]
    \tnl;
  \end{scope}
\end{tikzpicture}

\end{document}

相关内容