5

I'm trying to plot a function with TikZ that behaves different for several cases. Therefore I use an \ifthenelse in the \def statement. Using such a definition a plot with TikZ does not work. However, simple functions without \ifthenelse are no problem. I have a minimal example to show this problem:

\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}

I assume that the problem are the several brackets within my function definition. Does anyone know a solution for my problem?

9

I'm not sure why the function fails when used in a plot, but I would define the function differently, using the declare function key:

\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}
  • 1
    Conditional expressions in defining functions, learned something new. :) – juliohm Nov 6 '13 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.