# Fancy Equations in Latex/beamer with Tikz

So, when I discovered that Tikz and Beamer are made by one and the same guy (Till Tantau), I was very much pleased, since I then assumed that I could use the \onslide<> command inside tikzpictures (to make fancy equations, as hinted at in the title). It turns out that this is indeed the case, though complications arose in connections to this, and I thought I would document here how I went about it:
So, the mission was simple: use Tikz to make equations with colored labels on the terms.
I like to use this when doing presentations involving math, and it is a nice feature to only have the labels appear one at a time. My previous hack was to just make the equation in LaTex, dump a screen-shot into paint, and then apply the labels one at a time, finally using \onslide<> to show the different pictures resulting.
This is obviously a work-around, and I had been on the look out for something better (you could say that I still am, but more on that later).
First, the final version, code and everything:

(it is an explanation of the TUM model for neuron dynamics, in case you were wondering)

 Latex Code: \documentclass{beamer} \usepackage{graphicx} \usepackage{beamerthemeJLTree} \usepackage[latin1]{inputenc} \graphicspath{{figurer/}}               %%Tikz’ing:     \usepackage{tikz} \usetikzlibrary{matrix} \usetikzlibrary{chains} \usetikzlibrary{positioning}     \everymath{\displaystyle}     \tikzstyle{mathbox} = [inner sep=0pt, anchor=base]     \tikzstyle{every picture}+=[remember picture]     \usetikzlibrary{calc}     \usetikzlibrary{positioning}     \xdefinecolor{darkgreen}{RGB}{175, 193, 36}          %%%%%%%%%      \begin{document} \begin{frame}[fragile]   \frametitle{Tsodyks-Uziel-Markram model}        \begin{figure} \begin{tikzpicture}[every node/.style=mathbox]   \matrix (m) [row sep =.5 em,column sep=0em,   start chain=lhs1, start chain=lhs2, start chain=rhs2, start chain=rhs1, node distance=0pt]{      \node[on chain=lhs1] (taum){$\tau_m$}; \node[on chain=lhs1] {$\frac{dV_{i}}{dt}$}; &\node[above=-1pt]{=};&       \node[on chain=rhs1,above left= -2 pt and -5pt](Vrest){$V_{rest}$}; \node[on chain=rhs1] {$-V_i+$}; \node[on chain=rhs1] (rm) {$R_m$}; \node[on chain=rhs1] {$I_{syn}$};  \\                 \node[on chain=lhs2] {$\hspace{3 ex}$};\node[on chain=lhs2](Isyn){$I_{syn}$}; & \node{=};& \node[on chain=rhs2]{$\frac{1}{R_m}\sum_j$};  \node[on chain=rhs2] (w){$w_{ji}$}; \node[on chain=rhs2] (Y) {$Y_{ji}$}; \node[on chain=rhs2] (Vrev) {$(V_{rev}$}; \node[on chain=rhs2] {$-V_i)$};  \\     };%matrix end      \onslide<2->{\node[blue,text width = 5em,above = of taum,inner sep = 3pt] (taumText) {Membrane leak time} edge [blue,thick,->] (taum);} \onslide<3->{\node[red,text width = 5em,above = of rm,inner sep = 3pt] (rmText) {Membrane resistance} edge [red,thick,->] (rm);} \onslide<4->{\node[black,text width = 4em,left=of Isyn] (IsynText) {Synaptic Current} edge [black,thick,->] (Isyn);} \onslide<5->{\node[green!50,text width = 5em,below left=of w]  (wText) {Couplingstrength i to j} edge [green!50,thick,->] (w);} \onslide<6->{\node[darkgreen,text width = 6em,below right = of Y]  (YText) {Active Transmitters} edge[darkgreen,thick,->] (Y);} \onslide<7->{\node[red!60!white!80,text width = 5em,above right=of Vrev] (VrevText) {Reversal potential} edge [red!60!white!80,thick,->] (Vrev);}        \end{tikzpicture}\end{figure}       \end{frame}  \end{frame}

So, now you have an approximately working solution that you can just copy-paste. If you’re still reading, I expect it’s because you are thinking about cooking something up yourself. I salute you, and hope that you’ll tell me when you come up with something better =)
Stuff that didn’t work:
Right off the bat, I should admit that if I hadn’t insisted that the “=”-signs were aligned, the job would probably have been a lot easier. But I did, ’cause that’s what LaTex is all about.
• Using the align-environment with \tikz-environments stuffed inside, as in the otherwise nice example at Tikz Examples. The problem is that Latex can’t figure out what to do with the arrows going from your labels (which would necessarily have to originate in a different tikzpicture, as align will have none of that “placing stuff outside equations”-business), and the result seemed to be that on each compilation the tikzpicture with the label in would be moved further away from the equation, because LaTex thought it had to make room for the connecting line. Because of this, you have to make everything within a tikzpicture, ’nuff said.
• Brute-forcing the whole thing and telling Tikz where to place everything (come on, might as well just draw the equation in paint, it’s never going to be pretty).
• Using the tikz matrix-package with the option “matrix of math-nodes” (look in the pgf-manual for a description). On the face of it, this looks like a really neat solution, as what it does is that it basically gives you an align-environment in which to go crazy, with what is, almost, the minimal syntax to get this particular job done. However, as it turns out, this entails changing all lines in an equation-array when one is changed, simply because you most likely just changed the number of columns. Furthermore, if a big thing is above a small thing, that small thing is going to look absolutely ridiculous surrounded by all that white-space. And there is nothing you can do about it.
• Using a matrix without the chain. This would work, however, then you have to nameeverything, and detail on every god-damn node that it is to the left of the preceding node. It is not pretty, nor shorter, than using the chain.
Main problems with this solution:
As can be seen in the code, the placement is still erratic. I don’t know why, it probably has something to do with me using a graphing-package to do math, but I’m not smart/dedicated enough to delve into pgf itself and sort it out. Of course, there are work-arounds, in the form of the “above left”-commands used, but we all know that that shouldn’t be necessary. I can forgive tikz for not knowing that the position of a summation should not change just because the indexing below it is large, but it would be nice to have a way to just move that one node-label downwards, without influencing the rest of the chain.
Another issue, obviously, is the lack of left/right-alignment of a tikz matrix column. I get around that by adding extra white-space, but not having to do that would be pretty nice. I have made a feature-request at the tikz-project, but I don’t imagine that they are a ton of people working on it, so I’m not holding my breath.
We have to write [on chain] after every node. I think this would be possible to avoid by placing each chain within a scope, for which the node-style was changed, but it didn’t seem to work when I tried it.
As I said, if you come up with something smarter, or solve some of the above issues, I’d really like to hear about it =)
Finally, I need to include credit to the below page, for telling me to put the [fragile]-thing on my frame:

http://tex.stackexchange.com/questions/15093/unknown-error-using-tikz-matrix-of-nodes