Adventures in TikZ: tkz-graph

The other day, I was writing some lecture notes for my linear algebra class, and wanted to create the following diagram (to illustrate the concept of a Markov chain):

I had a very limited time in which to finish these notes. Fortunately, I found the tkz-graph package, which made this a snap:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{fouriernc}
\usepackage{tkz-graph}

\begin{document}
\begin{tikzpicture}
\SetGraphUnit{5}

\Vertex[x=0, y=10]{0 points};
\Vertex[x=0, y=5]{1 point};
\Vertex[x=0, y=0]{Win};
\Vertex[x=5, y=5]{Lose};

\Edge[style ={->}, label={$1/3$}]({0 points})({1 point});
\Edge[style ={->}, label={$1/3$}]({1 point})({Win});
\Edge[style ={->}, label={$1/6$}]({0 points})({Lose});
\Edge[style ={->}, label={$1/6$}]({1 point})({Lose});

\Loop[style ={->}, label={$1/2$}, labelstyle={fill=white}]({0 points});
\Loop[style ={->}, label={$1/2$}, labelstyle={fill=white}]({1 point});
\Loop[style ={->}, label={$1$}, dir=EA, labelstyle={fill=white}]({Lose});
\Loop[style ={->}, label={$1$}, labelstyle={fill=white}]({Win});

\end{tikzpicture}
\end{document}

You don’t even have to specify the locations of the vertices; you can throw caution to the wind and have LaTeX decide where to place them! (I am a bit too much of a perfectionist for that.)

One slight issue I had was that the documentation for this package (at least on my computer, as retrieved by texdoc) was in French. Fortunately, I seem to have retained enough knowledge since I took the French language exam as a grad student that I could read most of the documentation.

SageTeX is awesome!

For a long time, I have been a user of the math software system Sage, and for a longer time, I have been a user of LaTeX. So, it’s with some embarrassment that I report that I only recently discovered the awesomeness that is SageTeX, a LaTeX package that allows your LaTeX document to run Sage code and include the results.

I often use Sage to create plots for use in my lectures or printed class materials. Previously, my workflow was as follows:

  1. Create the graphic by running commands in the Sage notebook.
  2. Use the save command to save the graphic as a PDF, usually with a filename based on my LaTeX source file’s name (e.g. m160-sp13-mid2-practice-1.pdf, for use in the LaTeX document with source file m160-sp13-mid2-practice.tex).
  3. Save the file in the directory where my LaTeX source file lives.
  4. \includegraphics, FTW!

Like most teachers, every time I teach a course I’ve taught before, I like to reuse old materials, often after improving them or adjusting them to suit the new class. Therefore, it’s good to record the exact Sage commands somewhere, as opposed to just keeping the graphic file, in case I want to modify the graphic. One way you can do this is to paste the Sage commands as a comment in the LaTeX file. This has the advantage of allowing future tweaks to be recorded in your version control system. (And you really should be using a version control system, so that way you won’t be afraid to make changes to your document.) Unfortunately, modifications then mean a lot of copy-and-pasting: you have to modify the code in the Sage notebook, and then copy-and-paste the modifications to the comments in the LaTeX document, or vice-versa. And then you have to save another version of your graphic from the browser. What a headache!

SageTeX makes this a lot easier. When I want to put a Sage plot in my document, I can put the Sage commands right in the document:

\begin{sagesilent}
var('x,y')
gfx = implicit_plot((x + 1)*(x^2 + y^2) == 3*x^2, (x, -2, 2),(y, -2, 2), axes=true, frame=false)
gfx = gfx + list_plot([(1/2, -1/2)], size=50)
save(gfx, "m160-sp13-mid2-practice-1.pdf", figsize=[3, 3])
\end{sagesilent}

\item (15 points) 
Consider the curve with the equation 
$(x + 1)(x^2 + y^2) = 3x^2$ (shown below). 
Use implicit differentation to find $\tfrac{dy}{dx}$, 
and then find an equation for the tangent line 
of the curve at the point
$\left(\tfrac{1}{2},-\tfrac{1}{2}\right)$.\\
\includegraphics[width=3 in]{m160-sp13-mid2-practice-1.pdf}

When you compile your document with pdflatex, it creates a Sage script file titled (in this case) m160-sp13-mid2-practice.sagetex.py. Compiling this with Sage will create (or update) the file m160-sp13-mid2-practice-1.pdf, which you can include (or update) by running pdflatex again!

That’s pretty awesome.