创建一个函数来获取包含零和不存在点的符号表

创建一个函数来获取包含零和不存在点的符号表

我创建了一个代码来获取一个符号表。我已经尝试了现有的解决方案,但我不喜欢它们。我几乎已经实现了我的所有目标,但我获得的表格如下:

在此处输入图片描述

这是我创建的代码的一个工作示例

\documentclass[11pt]{article}

\usepackage{tikz, pgfplots, pgfmath, pgffor, amssymb}

\begin{document}

Sign table for the function
\[
f(x) = \frac{x(x^2-1)}{x+2}
\]

\bigskip

\begin{tikzpicture}[scale=.80]

% variables and sign array
%
\def\h{.9};                         % row height 
\def\w{2.5};                        % column width
%
\def\zeros{{"-2", "-1", 0, 1}};         % zeros and point of not existence
\def\factors{{"x+2", "x", "x^2-1", "f(x)"}};            
\def\signs{{{"-"    ,"\nexists" ,"+"    ,       ,"+"    ,       ,"+"    ,       ,"+"},          % "\nexists" is for point of not existence
            {"-"    ,           ,"-"    ,       ,"-"    ,"0"    ,"+"    ,       ,"+"},
            {"+"    ,           ,"+"    ,"0"    ,"-"    ,       ,"-"    ,"0"    ,"+"},
            {"+"    ,"\nexists" ,"-"    ,"0"    ,"+"    ,"0"    ,"-"    ,"0"    ,"+"},
            }};
%
\pgfmathsetmacro\nzeros{dim(\zeros)-1};         % number of zeros
\pgfmathsetmacro\ncol{2*dim(\zeros)};           % Number of columns
\pgfmathsetmacro\nrow{dim(\factors)-1};         % number of rows

% Abscissa axis
%
\draw[->]   (-\w, 0)            -- ({(\nzeros+1)*\w+0.125}, 0)      node[above]{$x$};
\draw       (-\w, {-\nrow*\h})  -- ({(\nzeros+1)*\w}, {-\nrow*\h});

% Zeros and vertical (dotted) lines
%
\foreach \i in {0,...,\nzeros} {
    \draw[dotted, gray!80]  (\i*\w, {-(\nrow+0.125)*\h})    -- (\i*\w, 0);
    \draw                   (\i*\w, 0)                      -- (\i*\w, +0.125) node[above] {$\pgfmathparse{\zeros[\i]}\pgfmathresult$}; 
}

% Signs and zeros table
%
\foreach \row in {0,...,\nrow} {
    \node[left]     at (-\w-0.125, {-(\row+0.5)*\h})        {\footnotesize signs of\,  $\pgfmathparse{\factors[\row]}\pgfmathresult$};
    \foreach \col in {0,...,\ncol} {
        \node       at ({(\col-1)*\w/2}, {-(\row+0.5)*\h})  {$\pgfmathparse{\signs[\row][\col]}\pgfmathresult$};    % <-- (1)
    }                               
}       
\end{tikzpicture}

\end{document}

现在我想要:

  1. 转换代码以使其具有功能,即

\signtable{..zeros...}{...factors...}{...signs...}

  1. 避免零和 \nexists 与虚线重叠。我尝试在 (1) 中使用一个框(参见代码中的注释标记),但它也创建了一个字符为空的框。我尝试在 (1) 中使用轮廓,但它给了我以下错误

Incomplete \iffalse; all text was ignored after line 52.}

第 52 行是最后一个 \foreach 的结尾

此外,还可以通过自动计算最后一行来改进代码。

有人能帮助我吗?

相关内容