## TikZ is the bestest thing ever

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 $\LaTeX$ 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.

### 8 Responses to “TikZ is the bestest thing ever”

1. identityelement Says:

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.)

• bretbenesh Says:

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

2. Joss Ives Says:

Hi Bret,

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

• bretbenesh Says:

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

3. Adam Glesser Says:

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 -.