I'm trying to use user-defined math functions with pgf to make a plot. The plot will consist of several straight lines given by a function of three arguments. (I know there are easier ways to do this, but I need to annotate the plot quite extensively, for which TikZ will be great.) The MWE below illustrates what I'm trying to do:
\documentclass[10pt]{article}
\usepackage{tikz}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\pgfmathdeclarefunction{r51}{3}{%
\pgfmathsetmacro{gterm}{(#1+1.0)/(2.0*#1)}%
\pgfmathsetmacro{denom}{1.0+gterm*(#2-1.0)}%
\pgfmathparse{(1.0+0.5*(#2-1.0))*#3/denom+((#2-1.0)/(2.0*#1)/denom)}%
}
\newcommand{\drawline}[4]{
\draw (#3,r51(#1,#2,#3))--(#4,r51(#1,#2,#4));
}
\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}
% axis limits
\def\xamin{- 0.25}
\def\yamin{- 0.25}
\def\xamax{ 8.25}
\def\yamax{ 7.25}
% draw grid
\draw[very thin,color=gray] (\xamin,\yamin) grid (\xamax,\yamax);
% draw axes
\draw[->] (\xamin,0.0) -- (\xamax,0.0) node[right] {$x$};
\draw[->] (0.0,\yamin) -- (0.0,\yamax) node[above] {$y$};
% draw first line
\drawline{1.4}{1.5}{0.1}{1.67}
\end{tikzpicture}
\end{document}
When I run it, I get the following error:
! Missing control sequence inserted.
<inserted text>
\inaccessible
l.31 \drawline{1.4}{1.5}{0.1}{1.67}
I'm sure this is quite a basic question, but I have not been able to find any other examples that would have given me a clue as to how to fix it. Thanks for any help.
gnuplot
in the future, you should also see this related question: How to consistently specify a function and use it for computation and plotting