Tikz 代数数学工作表中的多个数轴未对齐

Tikz 代数数学工作表中的多个数轴未对齐

我是一名特殊教育老师,主要教授数学,我正在尝试构建一堂关于多项式加法的课程。我实际上为自己感到自豪,因为我构建了一个 Python 脚本,可以自动执行下面的这个 tex 文件(通常它会编译十个问题,但在这个 MWE 中,只有一个)。这节课的重点是让学生将同类项组合起来,然后在数字线上显示出来。我想为他们完成一些问题,但正如你所见,数字线没有对齐。我通过调整此代码创建了数字线不等式数轴帖

我对 Tikz 不是很有经验,但我花了很多时间评论上面链接的帖子,以弄清楚不同的数字对数字线的作用,以便尝试将其用于我的目的。

我正在尝试为每个“同类项”创建一条数轴(回想一下高中代数),但正如您所见,有些不对劲,但我就是想不通,因为我知道当它只是 x^2 项时,它工作得很好。

这是涉及三条数轴的一个问题的 MWE 代码。

\documentclass{exam}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{arrows}

\begin{document}
\begin{enumerate}


    % Problem 0
    % Problem 0
    % Problem 0
    % Problem 0
    % Problem 0
\item Simplify: $(-5x^2+ \; 9x- \; 12) + (8x^2+ \; 8x+ \; 11)$ 
\vspace{0.5cm}

\textit{For your $x^2$ term:}
\vspace{0.5cm}

\begin{tikzpicture}[xscale=0.5]
    \draw[-latex] (-8,0) -- (20,0) ; 
\draw[latex-] (-8,0) -- (20,0) ; 

\foreach \x in  {-6, -4, -2, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, } 
\draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt);

\foreach \x in  {-6, -4, -2, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, } 
\draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt) node[below] {$\x$};
 \draw[*-] (-5,0.09) -- ++ (0,0);

\draw [red,  thick, -stealth] (-5,0) -- ++(0,0.5) -- ++(8,0);
\end{tikzpicture} 

\vspace{0.5cm} 

\textit{For your $x$ term:}
\vspace{0.5cm}

\begin{tikzpicture}[xscale=0.5]
\draw[-latex] (6,0) -- (34,0) ; 
\draw[latex-] (6,0) -- (34,0) ; 

\foreach \x in  {8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, } 
\draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt);

\foreach \x in  {8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, } 
\draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt) node[below] {$\x$};
 \draw[*-] (9,0.09) -- ++ (0,0);

\draw [red,  thick, -stealth] (9,0) -- ++(0,0.5) -- ++(8,0);
\end{tikzpicture} 

\textit{For your constants:}
\vspace{0.5cm}

\begin{tikzpicture}[xscale=0.5]
    \draw[-latex] (6,0) -- (34,0) ; 
\draw[latex-] (6,0) -- (34,0) ; 

\foreach \x in  {8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, } 
\draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt);

\foreach \x in  {8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, } 
\draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt) node[below] {$\x$};
 \draw[*-] (-12,0.09) -- ++ (0,0);

\draw [red,  thick, -stealth] (-12,0) -- ++(0,0.5) -- ++(11,0);
\end{tikzpicture} 

\end{enumerate}
\end{document}

Latex 文件的 PDF 输出图片

答案1

为了进行最初步的近似,您可以编写一个宏,将起始系数和移位转换为tikzpicture

\documentclass{exam}
\usepackage{tikz}
\usepackage{amsmath}
% This defines a macro that takes three arguments, one optional one and two
% mandatory ones. The mandatory arguments are the first and second coefficients,
% or, in other words, a coefficient and the shift
\newcommand{\NumberLine}[3][]{\begin{tikzpicture}[>=stealth,#1]
\pgfmathsetmacro{\mymin}{min(#2,#2+#3)}% minimum of first coefficient and sum of coefficients
\pgfmathsetmacro{\mymax}{max(#2,#2+#3)}% maximum of first coefficient and sum of coefficients
\pgfmathtruncatemacro{\itest}{ifthenelse(\mymax-\mymin>0.5,1,0)} %
% check if the values are sufficiently large apart from each other
% at this point tiny difference (<=0.5) are are not supported
\ifnum\itest=1
 \pgfmathtruncatemacro{\xstart}{int(\mymin/2)*2-2} % first tick
 \pgfmathtruncatemacro{\xnext}{\xstart+2} % next tick
 \pgfmathtruncatemacro{\xend}{int(\mymax/2)*2+2} % last tick
 \pgfmathsetmacro{\myscale}{12/(\xend-\xstart)} % zoom factor
 \draw[thick,<->] % draw the horizontal line with arrows
  (\myscale*\xstart-1,0) -- (\myscale*\xend+1,0);
 % draw the blob 
 \path (\myscale*#2,0) node[circle,fill,inner sep=1.5pt] (start){};
 % draw the ticks
 \draw foreach \X in {\xstart,\xnext,...,\xend}
  {(\myscale*\X,3pt) -- ++ (0,-6pt) node[below] {$\X$}};
 % draw the red arrow 
 \draw[red,thick,->] (start) |- ({\myscale*(#3+#2)},1em);
\else
% code for tiny differences could go here
\fi
\end{tikzpicture}}
\begin{document}
\begin{enumerate}


    % Problem 0
    % Problem 0
    % Problem 0
    % Problem 0
    % Problem 0
\item Simplify: $(-5x^2+ \; 9x- \; 12) + (8x^2+ \; 8x+ \; 11)$ 
\bigskip

\textit{For your $x^2$ term:}
\bigskip

\NumberLine{-5}{8}
\bigskip

\textit{For your $x$ term:}
\bigskip

\NumberLine{9}{8}
\bigskip

\textit{For your constants:}
\bigskip

\NumberLine{-12}{11}

\end{enumerate}
\end{document}

在此处输入图片描述

如您所见,它放大了相关区域。您可以使其更加通用,但要做到这一点,我需要了解实际用例。

编辑:增加了解释。

相关内容