使用 TikZ 绘制复杂的自定义函数

使用 TikZ 绘制复杂的自定义函数

我正在尝试使用 TikZ 绘制一个函数,该函数在几种情况下的行为不同。因此,我在 \def 语句中使用了 \ifthenelse。使用这样的定义,使用 TikZ 进行绘图不起作用。但是,没有 \ifthenelse 的简单函数没有问题。我有一个最小的例子来说明这个问题:

\documentclass[10pt]{article}
\usepackage{ifthen}
\usepackage{tikz}

\def\simpleFunction(#1,#2){#1*#1}
\def\complexFunction(#1,#2){\ifthenelse{\equal{#1}{0}}{0}{4}}

\begin{document}
\section{foo}

% definition of function correct and working
\complexFunction(0.005,0.00000003)
\complexFunction(0,0.00000003)

% simple function plot works
\begin{tikzpicture}
\draw [red, thick, x=0.0085cm, y=1cm]
plot [domain=-200:200, samples=100, smooth] (\x,{\simpleFunction(\x/200,0.00000003)});
\end{tikzpicture}

% complex function plot does not work
\begin{tikzpicture}
\draw [red, thick, x=0.0085cm, y=1cm]
plot [domain=-200:200, samples=100, smooth] (\x,{\complexFunction(\x/200,0.00000003)});
\end{tikzpicture}

\end{document}

我认为问题出在我的函数定义中的几个括号上。有人知道我的问题的解决方案吗?

答案1

我不确定为什么该函数在绘图中使用时会失败,但我会使用以下declare function键以不同的方式定义该函数:

\documentclass[10pt]{article}
\usepackage{tikz}

\pgfkeys{
    /pgf/declare function={f(\a,\c) = (\a < \c) * 4 + !(\a < \c) * 3;}
}

\begin{document}
\begin{tikzpicture}
\draw [red, thick, x=0.0085cm, y=1cm]
plot [domain=-200:200, samples=100, const plot] (\x,{f(\x/200, 0)});
\end{tikzpicture}
\end{document}

相关内容