Inspired by Kate McInnis, I taught myself how to start to use TikZ on Friday. It was ridiculous. I was creating graphs for calculus so that students could find critical points and inflection points given the graph of f or f’. It is a pain to graph these using polynomial equations, since you have to do a lot of fiddling if you want critical points and inflection points to occur at nice values.

For instance, here is a link to a problem. Here is the entire code in that generates it:

Let $f$ be a function defined and continuous on the interval $[-5,5]$. The

graph of $f’$ (NOT $f$) is given here.\footnote{This problem is stolen from Robin Gottlieb’s \emph{Calculus: An Integrated Approach to Functions and Their Rates of Change}}

\begin{center}

\begin{tikzpicture}

%\draw[help lines] (0,-3) grid (5,3);

\draw [->][very thick] (-6,0) — (6,0);

\draw [][very thick] (0,-4) — (0,4);

\foreach \x/\xtext in {-1/-1, -2/-2, -3/-3, -4/-4, -5/-5, 1/1, , 2/2, 3/3, 4/4, 5/5}

\draw[shift={(\x,0)}] (0pt,2pt) — (0pt,-2pt) node[below] {$\xtext$};

\foreach \y/\ytext in {1/1}

\draw[shift={(0,\y)}] (2pt,0pt) — (-2pt,0pt) node[left] {$\ytext$};

\draw[ultra thick] (-5,2.5) to [out=0,in=180] (-1.5,0) to [out=0,in=180] (0,1) to [out=0,in=180] (2,1);

\draw[ultra thick] (2,-1) to [out=0,in=180] (3,-1) to [out=50,in=220] (5,1);

\filldraw [fill=white,draw=black,thick] (2,1) circle (1.2mm);

\filldraw [fill=white,draw=black,thick] (2,-1) circle (1.2mm);

\filldraw [fill=black,draw=black,thick] (-5,2.5) circle (1.2mm);

\filldraw [fill=black,draw=black,thick] (5,1) circle (1.2mm);

\draw (0,4) node[above] {$f’$};

\draw (6,0) node[right] {$x$};

\end{tikzpicture}

\end{center}

\begin{enumerate}

\item Identify all critical points.

\item For what value of $x$ does $f$ take on its maximum value?

\item For what value of $x$ does $f$ take on its minimum value?

\end{enumerate}

Consider the line:

\draw[ultra thick] (-5,2.5) to [out=0,in=180] (-1.5,0) to [out=0,in=180] (0,1) to [out=0,in=180] (2,1);

This simply says “draw a curve from the point (-5,2.5) to (-1.5,0), another curve from (-1.5,0) to (0,1), and another from (0,1) to (2,1). In each case, you should leave at an angle of 0 degrees, and enter the point at an angle of 180 degrees.”

The angles of “0” and “180” were chosen to create extreme values; I could have chosen any angles. This is much, *much* easier to do than to figure out polynomial functions that go through the points.

Till Tandau has done so much to make my life better.

October 8, 2012 at 7:19 pm |

This is awesome! Thanks so much for posting this. I struggle with this all the time. I can manage piecewise linear pretty well with xfig, but getting curvy curves with the right properties is hard. (xfig has splines, but I can never make them pass through the right points.)

October 9, 2012 at 1:30 am |

I know! I have been piecing together graphs from GeoGebra and Apple’s Grapher for years. This should totally replace them.

October 9, 2012 at 5:33 pm |

Hi Bret,

I know that I found Tikz quite useful when I was creating a lot of Feynman diagrams for my dissertation.

October 9, 2012 at 5:51 pm |

Fortunately, I didn’t have many diagrams for my dissertation, and I haven’t needed pictures for any of my research papers yet. But I wish that I would have buckled down and learned TikZ years ago to help my teaching. Bret

October 10, 2012 at 4:21 pm |

That’s a really great trick for drawing those graphs! Incidentally, I did a little investigation and found that if you modify your first “\foreach” line to the following:

\foreach \x/\xtext in {-1/\llap{$-$}1, -2/\llap{$-$}2, -3/\llap{$-$}3, -4/\llap{$-$}4, -5/\llap{$-$}5, 1/1, , 2/2, 3/3, 4/4, 5/5}

then the label will center on just the number instead of on the negative sign + number. The only difference is putting \llap{$-$} instead of just -.

October 10, 2012 at 4:26 pm |

You are awesome, Adam. And—for the record—I was not clever enough to even come up with that \foreach statement; I stole it off of the internets. So that makes me even more impressed at your improvement. Bret

October 10, 2012 at 4:59 pm

Why reinvent the wheel, right? Incidentally, here is where I learned about \llap:

http://www.tug.org/TUGboat/Articles/tb22-4/tb72perlS.pdf

October 10, 2012 at 5:31 pm

Thanks!