TikZ is the Bestest Thing Ever (or, A Personal History of Creating Math Documents)

I resisted learning LaTeX for awhile.  It wasn’t that I felt like it was too complicated (heck, I’m someone who taught myself Python mostly so I could make my media center do exactly what I wanted it to), but in my Master’s program it was almost discouraged by faculty (bizarre I know, I still don’t understand that).  Also, after years of administrative work, I was familiar enough with the advanced features of Word that I could layout my handouts exactly how I wanted without thinking to hard about it, which was a very good thing, because it allowed me to focus on the content of what I was creating.  I also used Adobe Illustrator, combined with the Mac graphing calculator program Grapher to make whatever graphics I needed.  I actually got really good at creating graphics I was happy with, but it was incredibly time-consuming.  I also wrote a test generator for multiple choice tests, which swapped around the order of the multiple choice answers (for those of you that teach classes of reasonable size, that may seem unnecessary, but trust me when you’re teaching five classes of fifty in extraordinarily crowded rooms it matters a lot).

Of course, since I was using Word, I used MathType (the professional version of equation editor) to typeset the math.  But then the mac version of Office 2008 eliminated VBA. It was fine at first– I just stuck with 2004.  For a long time.  Eventually it seemed annoying and sluggish, especially once I upgraded the OS, but I needed VBA for the MathType and for the test generator (not to mention a bunch of other macros I wrote to make my life easier).  And eventually the compatibility issues were fixed, and at the end of 2010, Office 2011 came out.

But then there were problems.  The most serious one was that equations that showed up fine on screen printed as white space.  During final exams for the Fall of 2010, I had a pretty serious respiratory infection (not uncommon for me) but I was pushing through it (also not uncommon), and I did not proofread the printed version of the test very carefully.  On a test with forty questions, six or seven equations were missing.  Missing!  On a final exam!  I managed to figure out what needed to be there for the most part, and had students make corrections to their exams.  It came out alright in the end, but it still was a disaster.  I felt betrayed by MathType.

And so I started thinking more about latex.  It had occurred to me in the past that I would be able to write a test generator in Python using latex (and that I could really do a lot more with Python and latex than with VBA and MathType).  And, as with most major lifestyle changes (yes, I called changing how I make documents for my classes a major lifestyle change), it bubbled around in the back of my mind for a long time before I did anything about it.

It was daunting at first, especially since I am very particular (much more so than I need to be, I admit) about the way my documents look.  Obsessive even.  And TexShop… well, it worked just fine.  But when my workflow would spiral into tweak/typeset/judge/tweak/typeset/judge/tweak/etc/etc/etc…  it got to be annoying.  It doesn’t take that long to compile the sorts of documents I was working on, but it was long enough to be irritating when you add it all up.  I tried a few other applications, including TextMate, which is what I use for almost everything else that’s text-based.  But still you have the tweak/recompile/tweak/recompile/tweak/recompile cycle.

Enter Latexian.

This was a total game-changer for me.  Latexian is a latex editor with many features that I would except in any IDE: it’s got basic syntax coloring (TextMate’s was more customizable, but that’s not a major thing for me), a spell-checker that ignores latex commands (very necessary), decent code completion, and the ability to assign shortcuts to paste in frequently used code.  But all that has nothing to do with why I love this application.  I’m all about the Live Preview panel.

I make my changes, and they’re reflected in the panel to the right.  Bam!  This may not be as big a deal to anyone else (I’m honestly a little disappointed that none of my real-life mathy friends find this as wonderful as I do– although one of them said he would switch if it were free, but he doesn’t believe in paying for software) but it brings joy to my heart.  Lots and lots of joy from a $10 application.  Totally worth it.

So I switched wholeheartedly to creating documents in latex.

There have been a few challenges (especially with formatting tables) but I used Python to write a function that would take whatever strings I had, and build them into a table.  This came in very handy, since I was constantly creating handouts of problem sets for my Pre-Cal class, and I needed a quick way to write problems, switch the order around until I was happy, and then arrange them in a table for the document.

The other major challenge was creating and editing graphics until I was happy with them.  At first, I was using the same process to create graphics as before (Illustrator and Grapher), but that’s a very time-consuming way to do things.  I looked into other more TeX-y ways to create graphics, including Asymptote, which is what my colleagues use, but I found that unsatisfying.  It wasn’t really faster (although that’s mostly because I was used to my earlier method, and I had a learning curve with Asymptote) and I couldn’t get things to look exactly like I wanted them to.  And of course then you’ve got the revise and recompile issue again.  You can embed asymptote code into a latex document, but I couldn’t get that to work with Latexian (and no way was I going to give up my new favorite toy!) and then I discovered TikZ.

I can create graphics embedded in my latex document (which makes the documents MUCH easier to share, by the way) that show up perfectly and immediately in my preview panel.  Woo!  It’s a dream come true (at least for those who dream geeky math-graphics dreams like me).  And I’ve got my graphics looking exactly like I want them to.

So I’m posting some examples.  Each example below requires
\usepackage{tikz}
in the preamble. Some of the examples also require other preamble code, which I’ve noted below.

\begin{tikzpicture} [domain=-5:5, scale=0.65, baseline=0, >=stealth]
\draw[step=1cm,very thin] (-5,-5) grid (5,5);
    \draw [thick] (-5,0) -- (5,0);
    \draw [thick] (0,-5) -- (0,5);
    \draw[ultra thick, smooth] plot ( \x , {4.0/(1.0*(\x)^2 + 1.0)});
    \draw [ultra thick, dashed] (-5,0) -- (5,0);
    \filldraw [fill=white,draw=black,thick] (0.500,3.200) circle (1.7mm);
    \filldraw [fill=white,draw=black,thick] (1.500,1.231) circle (1.7mm);
    \filldraw [fill=white,draw=black,thick] (2.500,0.552) circle (1.7mm);
    \filldraw [fill=white,draw=black,thick] (3.500,0.302) circle (1.7mm);
    \draw [ultra thick, ->] (-5,0.154) -- (-5.1,0.148);
    \draw [ultra thick, ->] (5,0.154) -- (5.1,0.148);
    \draw (0,-5) node[below] {$f_2(x) = \frac{4 \left(x -\frac{1}{2}\right) \left(x -\frac{3}{2}\right) \left(x -\frac{5}{2}\right) \left(x -\frac{7}{2}\right)} {\left(x -\frac{1}{2}\right) \left(x -\frac{3}{2}\right) \left(x -\frac{5}{2}\right) \left(x -\frac{7}{2}\right)\left(x^2+1\right)} $};
\end{tikzpicture}

In the preamble:
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\usepgfplotslibrary{polar}

In the body:
\begin{tikzpicture} [baseline=4cm]
\begin{polaraxis}[ytick={0,1,...,20}, yticklabels={,,,3,,,6,,}, xticklabels={,, $\frac{\pi}{6}$, $\frac{\pi}{3}$, $\frac{\pi}{2}$, $\frac{2\pi}{3}$, $\frac{5\pi}{6}$, $\pi$, $\frac{7\pi}{6}$, $\frac{4\pi}{3}$, $\frac{3\pi}{2}$, $\frac{5\pi}{3}$,$\frac{11\pi}{6}$,}]
\addplot+[black, very thick, mark=none, domain=0:720, samples=600 ] {2-5*cos(x)};
\end{polaraxis}
\draw (3.43cm,-.7cm) node [below] {$r=2-5\cos\theta $};
\end{tikzpicture}

\newlength{\chartscale}
\setlength{\chartscale}{0.208in}
\begin{tikzpicture}[>=stealth, baseline=-0.05in]
\draw [<->, thick] (-1.25in,0) -- (1.25in,0);
\draw (-1.5in,0) node {$\displaystyle f'(x)$};
\foreach \x/\xtext in {2/nd, -1/0, -4/0, 5/0}
{    \draw [thick] (\x*\chartscale,-0.05in) -- (\x*\chartscale,0.05in);
\draw (\x * \chartscale,0) [above=.05in] node {\scriptsize{\xtext}};
\draw (\x * \chartscale,0) [below=.05in] node {\footnotesize{$\x$}};
}
\draw (-4.99*\chartscale, 0) [above=0.05in] node {\footnotesize{${}+++{}$}};
\draw (-2.49*\chartscale, 0) [above=0.05in] node {\footnotesize{${}++++{}$}};
\draw (0.4*\chartscale, 0) [above=0.05in] node {\footnotesize{${}----{}$}};
\draw (3.50*\chartscale, 0) [above=0.05in] node {\footnotesize{${}----{}$}};
\draw (5.50*\chartscale, 0) [above=0.05in] node {\footnotesize{${}+{}$}};
\end{tikzpicture}

\begin{tikzpicture} [>=stealth, baseline=0]
\draw [thick,fill=white] (0,0) circle (3cm);
\draw [thick, <->] (-3.5,0) -- (3.5,0);
\draw [thick, <->] (0,-3.5) -- (0,3.5);
\draw (0,0) --  node [fill=white] {$\frac{\pi}{4}$} (45:3);
\draw (0,0) --  node [fill=white] {$\frac{\pi}{2}$} (90:3);
\draw (0,0) --  node [fill=white] {$\frac{3\pi}{4}$} (135:3);
\draw (0,0) --  node [fill=white] {\footnotesize{$\pi$}} (180:3);
\draw (0,0) --  node [fill=white] {$\frac{5\pi}{4}$} (225:3);
\draw (0,0) --  node [fill=white] {$\frac{3\pi}{2}$} (270:3);
\draw (0,0) --  node [fill=white] {$\frac{7\pi}{4}$} (315:3);
\draw (0,0) --  node [near end, fill=white] {$\frac{\pi}{6}$} (30:3);
\draw (0,0) --  node [near end, fill=white] {$\frac{\pi}{3}$} (60:3);
\draw (0,0) --  node [near end, fill=white] {$\frac{2\pi}{3}$} (120:3);
\draw (0,0) --  node [near end, fill=white] {$\frac{5\pi}{6}$} (150:3);
\draw (0,0) --  node [near end, fill=white] {$\frac{7\pi}{6}$} (210:3);
\draw (0,0) --  node [near end, fill=white] {$\frac{4\pi}{3}$} (240:3);
\draw (0,0) --  node [near end, fill=white] {$\frac{5\pi}{3}$} (300:3);
\draw (0,0) --  node [near end, fill=white] {$\frac{11\pi}{6}$} (330:3);
\end{tikzpicture}

In the preamble:
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}

In the body:
\begin{tikzpicture} [baseline=1.2in]
\draw [thick] (1,1) -- (1,0) -- (5,0) -- (5,1) -- (6,1) -- (6,5) -- (5,5) -- (5,6) -- (1,6) -- (1,5) -- (0,5) -- (0,1) -- (1,1) ;
\draw [semithick,dashed] (1,1) -- (1,5) -- (5,5) -- (5,1) -- (1,1);
\draw [thick, decorate,decoration={brace,amplitude=10pt},xshift=0pt,yshift=2pt] (0.05,6) -- (5.95,6) node [black,midway,yshift=16pt] {\footnotesize $5$ cm};
\draw [thick, decorate,decoration={brace,amplitude=10pt},xshift=2pt] (6,5.95) -- (6,0.05) node [black,midway,xshift=26pt] {\footnotesize $5$ cm};
\end{tikzpicture}

In the preamble:
\usepackage{tikz}
\newcommand{\opendot}[2]{\filldraw [fill=white,draw=black, thick] ({#1},{#2}) circle (.13) ;}
\newcommand{\closeddot}[2]{\filldraw [fill=black,draw=black, thick] ({#1},{#2}) circle (.13) ;}

In the body:
\newlength{\graphscale}
\setlength{\graphscale}{.35cm}
\begin{tikzpicture} [domain=-10:10, scale=1, >=stealth,x=\graphscale,y=\graphscale]
\draw[step=\graphscale,very thin] (-10,-5) grid (10,8);
\draw [very thick] (-10,0) -- (10,0);
\draw [very thick] (0,-5) -- (0,8);
\draw [ultra thick] (-10,-4) -- (-6,4);
\draw [ultra thick] (0,0) arc (0:180:3 and 1);
\draw [ultra thick] (0,0) parabola (2,4) -- (5,3);
\draw [ultra thick] (5,2) -- (10,7);
\opendot{-9}{-2}
\opendot{-6}{4}
\opendot{-3}{1}
\opendot{5}{3}
\opendot{8}{5}
\closeddot{-6}{0}
\closeddot{-3}{6}
\closeddot{5}{2}
\closeddot{8}{-1}
\end{tikzpicture}

As I said, having the graphics code embedded in the document does make sharing easier.  I was planning on posting a bunch of stuff up here, just in case someone might find it useful, but apparently WordPress doesn’t trust a .tex extension.  So I’ll look around for another way to share latex documents.  Anyone know of any?

Advertisements

2 thoughts on “TikZ is the Bestest Thing Ever (or, A Personal History of Creating Math Documents)

  1. bretbenesh says:

    Kate,

    Today, you are fourth favorite human being on earth. I have been wanting to learn tikz for some time, but it was always too much of a pain to find some code to hack. You put it all right here on the blog (which I bookmarked) for me to play with.

    thankyouthankyouthankyouthankyou,
    Bret

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s