定义用于制作功能线/包的新命令

定义用于制作功能线/包的新命令

您可能已经从标题猜到了,我的母语不是英语。因此,我不知道“功能线”是否是我所寻找的正确术语。

基本上我想做一个类似于下面的命令

\functionalines{3x}{x-4}{F_x = (3x)(x-4)}

然后输出应该类似于下面的 tikz 代码。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[%
negativ/.style={blue,dashed},
positiv/.style={red},
vertlinje/.style={dotted,opacity=.7},
node distance=1.5ex,
nullpunkt/.style={fill=white,inner sep= 1pt}]
  \draw [->,>=stealth] (-5,0) node (linestart) {} -- (5,0) node (lineend) {};
  \node (null1) at (-2,0) [label=above:-2] {};
  \node (null2) at (1,0) [label=above:1] {};
  \node [matrix] (produktledd) [below left=of linestart]{
    \node [left] (f1) {$x+2$}; \\
    \node [left] (f2) {$x-1$}; \\
    \node [left] (f)  {$f(x)= x^2+ x - 2$}; \\
   };
  \draw [vertlinje] (null1)       -- (null1 |- f);
  \draw [vertlinje] (null2)       -- (null2 |- f);
  \draw [positiv]   (null1 |- f1) -- (lineend |- f1);
  \draw [negativ]   (f1)          -- (null1 |- f1) node[nullpunkt] {$0$};
  \draw [negativ]   (null1 |- f)  -- (null2 |- f);
  \draw [positiv]   (null2 |- f2) -- (lineend |- f2);
  \draw [negativ]   (f2)          -- (null2 |- f2) node[nullpunkt] {$0$};
  \draw [positiv]   (f)           -- (null1 |- f)  node[nullpunkt] {$0$}
                (null2 |- f) node[nullpunkt] {$0$} -- (lineend |- f);
\end{tikzpicture}
\end{document}

一个非常好的朋友向我展示了这段代码 =) 虽然有一些小的编辑会更好。顶部矢量线应该稍微降低一点,并越过方程式。在方程式和矢量上方应该有一个 x,表示我们正在查看 x 轴。最好在方程式下方有一条线,长度与上方的线相同。下面的图片。这可以实现吗?

代码看起来如何

http://www.diskusjon.no/index.php?app=core&module=attach§ion=attach&attach_rel_module=post&attach_id=461590

如果我能以任何我想要的方式获得输出,我希望它像下面这样。

在此处输入图片描述

那么,是的,有什么想法吗,如何制作一个能够提供所需输出的命令?

答案1

你可以使用 PGFplots 来实现这一点。通过使用

 \addplot [restrict y to domain=0.5:inf] {(x-2)>0};

所有不满足条件的坐标将被丢弃。

functionallines我定义了以下形式的环境

\begin{functionallines}[<pgfplots arguments>]{<zero positions>}{<lower>:<upper>}

<zero positions>参数采用逗号分隔的 x 值列表,其中任何函数为零或未定义。在这些位置,将绘制一条穿过函数图的垂直线,并且这些位置将以黑色打印在图表顶部。如果您想更改这些位置的标签,请使用 来<pgfplots arguments>提供extra x tick labels={<first label>,<second label>,...}

可选参数还可用于诸如通过设置来关闭常规刻度标签xtick=\empty,或通过设置来更改函数的绘制样式等negative/.style={orange,dashed}, positive/.style={green, ultra thick}

<lower>和参数<upper>定义了图的 x 范围。

然后可以使用

\functionalline[<LaTeX math code>]{<expression>}{<vertical position>}{<list of zeros>}

其中可选参数[<LaTeX math code>]可用于指定用于打印方程的代码。

如果函数在其中一个零位置处计算出非实数值(除以零),则会x打印 而不是0

以下是几个示例:

\begin{functionallines}{-1,1}{-2:2}
    \functionalline{x-1}{1}{1}
    \functionalline{x+1}{2}{-1}
    \functionalline[f(x) = \dfrac{(x-1)}{(x+1)}]{(x-1)/(x+1)}{3.5}{-1,1}
\end{functionallines}


\begin{functionallines}[
    xtick=\empty,
    negative/.style={orange,dashed},
    positive/.style={green, ultra thick}
    ]{0,90,180,270,360}{-20:380}
    \functionalline[\cos(x)]{cos(x)}{1}{90,270}
    \functionalline[\sin(x)]{sin(x)}{2}{0,180,360}
    \functionalline[\cos(x) \cdot \sin(x)]{cos(x) * sin(x)}{3}{0,90,180,270,360}
\end{functionallines}


使用 设置精确零位置的标签extra x ticks labels,使用 指定正常刻度xtick={<list>}以避免重叠。

\begin{functionallines}[
    extra x tick labels={$\sqrt{5}$,$\pi$,$2\pi$},
    xtick={0,1,4,5}]{2.23,3.14,6.3}{0:6.5}
    \functionalline[x-\sqrt{5}]{x-2.23}{1}{2.23}
    \functionalline[\sin(x)]{sin(x*180/3.14)}{2}{3.14,6.28}
    \functionalline[(x-\sqrt{5})\cdot \sin(x)]{(x-2.23)*sin(x*180/3.14)}{3}{2.23,3.14,6.28}
\end{functionallines}


完整代码如下:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{amsmath}

\begin{document}

\pgfplotsset{
    shift down/.style={
         y filter/.code={\pgfmathparse{\pgfmathresult*(#1)}}
    },
    shift down/.default=1,
    every axis plot post/.style={restrict y to domain=0.5:inf},
    positive/.style={
        no markers,
        red
    },
    negative/.style={
        no markers,
        blue
    },
    /tikz/function label/.style={
        anchor=east
    },
    step functionallinenumber/.code={
        \stepcounter{functionallinenumber}
    },
    title entries/.initial={}
}

\makeatletter
\newcommand\functionalline[4][\@empty]{
    \edef\plots{
        \noexpand\addplot [negative, shift down=#3, forget plot] {#2<0};
        \noexpand\addplot [positive, shift down=#3, forget plot] {#2>0};
    }
    \plots
    \node at (axis cs:\pgfkeysvalueof{/pgfplots/xmin},#3) [function label] {%
        \ifx#1\@empty%
            $#2$%
        \else%
            $#1$%
        \fi
    };

    \pgfplotsinvokeforeach {#4} {
       \node at (axis cs:##1,#3) [
        fill=white,
        inner sep=1pt,
        declare function={x=##1;} % Set 'x' to current position
    ] {%
    \pgfkeys{/pgf/fpu}% Use the fpu library, because it doesn't throw an error for divide by zero, but sets result to +/- inf
    \pgfmathparse{#2}%
    \pgfmathfloatifflags{\pgfmathresult}{0}{\hspace{-0.75ex}0}{x}% Check whether result is zero. The \hspace is necessary because of a bug in the fpu library. (Update 11 June 2012: Doesn't seem to be the case anymore, the \hspace can be removed)
    \pgfkeys{/pgf/fpu=false}%
    };
    }   
}

\newenvironment{functionallines}[3][]{
    \begin{tikzpicture}
    \begin{axis}[        
        extra x ticks = {#2},
        grid=none,
        xticklabel pos=right,
        hide y axis,
        x axis line style={draw=none},
        every tick label/.style={
            anchor=base,
            yshift=1ex,
            gray!50
        },
        every extra x tick/.style={
            every tick label/.style={
                anchor=base,
                yshift=1ex,
                inner xsep=0pt,
                fill=white,
                text=black
            }
        },
        extra x tick style={grid=major},
        xtick pos=right,
        major tick length=0pt,
        enlarge x limits=false,
        enlarge y limits={abs=0.75},
        domain=#3,
        samples=100,
        y dir=reverse, y = -0.5cm,
        clip=false,
        #1
    ]
}{
    \coordinate (bottom right) at (rel axis cs:1,0);
    \coordinate (top right) at (rel axis cs:1,1);
    \end{axis}
    \draw [-latex] (top right-|current bounding box.west) -- (top right) node [right] {$x$};
    \draw (bottom right) -- (bottom right-|current bounding box.west);
    \end{tikzpicture}
}



\begin{functionallines}{-1,1}{-2:2}
    \functionalline{x-1}{1}{1}
    \functionalline{x+1}{2}{-1}
    \functionalline[f(x) = \dfrac{(x-1)}{(x+1)}]{(x-1)/(x+1)}{3.5}{-1,1}
\end{functionallines}

\hspace{1cm}

\begin{functionallines}[xtick=\empty]{0,90,180,270,360}{-20:380}
    \functionalline[\cos(x)]{cos(x)}{1}{90,270}
    \functionalline[\sin(x)]{sin(x)}{2}{0,180,360}
    \functionalline[\cos(x) \cdot \sin(x)]{cos(x) * sin(x)}{3}{0,90,180,270,360}
\end{functionallines}

\hspace{1cm}

\begin{functionallines}[
    extra x tick labels={$\sqrt{5}$,$\pi$,$2\pi$},
    xtick={0,1,4,5}]{2.23,3.14,6.3}{0:6.5}
    \functionalline[x-\sqrt{5}]{x-2.23}{1}{2.23}
    \functionalline[\sin(x)]{sin(x*180/3.14)}{2}{3.14,6.28}
    \functionalline[(x-\sqrt{5})\cdot \sin(x)]{(x-2.23)*sin(x*180/3.14)}{3}{2.23,3.14,6.28}
\end{functionallines}

\end{document}

答案2

更改\functionallines为以下内容(五个输入参数)以输入未定义函数的点列表。只是 Jake 代码的扩展。

\makeatletter
\newcommand\functionalline[5][\@empty]{
    \ifx#1\@empty
        \edef\equation{#2}
    \else
        \edef\equation{#1}
    \fi
    \edef\plots{
        \noexpand\addplot [negative, shift down=#3] {#2<0};
        \noexpand\addplot [positive, shift down=#3] {#2>0};
        \noexpand\node at (axis cs:\noexpand\pgfkeysvalueof{/pgfplots/xmin},#3)     [function label] {$\equation$};
    }
    \plots
    \pgfplotsinvokeforeach {#4} {
        \node at (axis cs:##1,#3) [fill=white, inner sep=1pt] {0};
    }   
    \pgfplotsinvokeforeach {#5} {
        \node at (axis cs:##1,#3) [fill=white, inner sep=1pt] {x};
    }   
}

相关内容