I am to begin to write some lecture notes for my students and I will have to do multiple diagrams like the one shown below to represent the simple notion of derivatives graphically. The problem I face is to find a way to automate the creation of such figures. For example, what am looking for is:
- Define a function f(x) (in
TikZof course) - Declare the x-coordinates of two points, say x1 and x2. Now these two coordinates can be used to calculate their corresponding y coordinates and the distances shown in the triangle.
Thus am looking for something like, \derivative{x*x}{x1}{x2}. I don't know how the domain can be incorporated but I guess that can be done later.
\documentclass[letterpaper]{article}
\usepackage{fullpage}
\usepackage{amsmath,amssymb,enumitem}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\pdfpageheight\paperheight
\pdfpagewidth\paperwidth
\parindent0pt
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,>=stealth',x=1.0cm,y=1.0cm,domain=-0.5:2.25]
\draw[->,color=black] (-1,0) -- (4,0);
\foreach \x in {1,2,3}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\draw[color=black] (4,0) node [right] { $x$};
\draw[->,color=black] (0,-1) -- (0,5);
\foreach \y in {1,2,3,4}
\draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt) node[left] {\footnotesize $\y$};
\draw[color=black] (0,5) node [above] { $y$};
\draw[color=red,<->] plot[id=quad] function{x*x} node[right] {$f(x) =x^2$};
\fill[black] (0.5,0.25) circle (1.25pt);
\fill[black] (1.5,2.25) circle (1.25pt);
\draw (0.5,0.25)--(1.5,2.25)--(1.5,0.25)--cycle;
\draw[|-|,yshift=-0.25cm] (0.5,0.25)--(1.5,0.25);
\draw[yshift=-0.25cm](1,0.25) node[fill=white] {1};
\draw[|-|,xshift=0.25cm] (1.5,0.25)--(1.5,2.25);
\draw[xshift=0.25cm] (1.5,1.25) node [fill=white] {2};
\end{tikzpicture}
\end{document}
Image of the result: 




P_1andP_2or the like and create some form of a legend on a corner (right maybe) specifying the coordinates of these two points? – azetina May 12 '12 at 22:46P_2=(2,4), or do you want the y-coordinate to be calculated automatically? – Jake May 12 '12 at 22:56P_2=(2,4). Though this would be as a legend and onlyP_2would appear beside the specific point. This of course is just aesthetics for the displayed figure. – azetina May 12 '12 at 23:13at={(rel axis cs:1.1,0.5)}and how to interpret it as this I suppose controls the position of the legend, right? – azetina May 12 '12 at 23:18(rel axis cs:<x>,<y>)is the relative axis coordinate system:(rel axis cs:0,0)is the bottom left corner of the plot area,(rel axis cs:1,1)is the top right. The value I used,(rel axis cs:1.1,0.5)is in the vertical centre of the plot, slightly outside the right border of the plot area. – Jake May 13 '12 at 07:48Math Error: The function 'f' already existsoccurs? – Faceb Faceb Jul 02 '18 at 22:26declare functiondoesn't work inside the\scope[...]anymore, something must have changed in recent versions of PGF/TikZ. I haven't been keeping up with the developments in recent years, so unfortunately I don't know how to solve this problem. Maybe you could open a new question? – Jake Jul 03 '18 at 20:04