# TikZ Diagrams in Math Mode

I've attached a picture which shows what I want to be able to do. I have been making simple Hackenbush diagrams as follows:

\begin{tikzpicture}
\node[hackennode] (Bottom Blue) at (0,0) {};
\node[hackennode] (Top Blue) at (0,1) {};
\draw[hackenline,blue] (Bottom Blue) -- (Top Blue);
\end{tikzpicture}


I'd like to be able to use them along with normal mathematical symbols, as the picture shows. Thanks!

(I also don't know how to put the dotted line in, but I'm less concerned about that.)

-
The code posted in questions (and answers) should always compile as is. In particular, you should include the definition of hackennode and hackenline from my answer to your last question –  Caramdir Feb 15 '11 at 2:12

You can simply nest tikzpictures inside math environments. Basically, the only thing you have to consider is to add an appropriate baseline to get vertical centering right. Here is the code, explanation will come afterwards.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\tikzstyle{hackennode}=[draw,circle,fill=white,inner sep=0,minimum size=4pt]
\tikzstyle{hackenline}=[line width=3pt]

{\Large
$\begin{tikzpicture}[baseline=-0.65ex,scale=0.5] \draw[densely dashed] (-1,-1) -- (1,-1); \node[hackennode] (middle) at ( 0, 0) {}; \node[hackennode] (left) at (-0.5,-1) {}; \node[hackennode] (right) at ( 0.5,-1) {}; \node[hackennode] (top) at ( 0, 1) {}; \draw[hackenline,blue] (left) -- (middle) -- (right); \draw[hackenline,red] (middle) -- (top); \end{tikzpicture} = \left\{ \begin{tikzpicture}[baseline=-0.65ex,scale=0.5] \draw[densely dashed] (-0.5,-1) -- (1,-1); \node[hackennode] (middle) at ( 0, 0) {}; \node[hackennode] (right) at ( 0.5,-1) {}; \node[hackennode] (top) at ( 0, 1) {}; \draw[hackenline,blue] (middle) -- (right); \draw[hackenline,red] (middle) -- (top); \end{tikzpicture} \tikz[baseline=-0.65ex,scale=0.5] \node[inner sep=0] at (0,-1) {,\,}; \begin{tikzpicture}[baseline=-0.65ex,scale=0.5] \draw[densely dashed] (-1,-1) -- (0.5,-1); \node[hackennode] (middle) at ( 0, 0) {}; \node[hackennode] (left) at (-0.5,-1) {}; \node[hackennode] (top) at ( 0, 1) {}; \draw[hackenline,blue] (middle) -- (left); \draw[hackenline,red] (middle) -- (top); \end{tikzpicture} \middle| \begin{tikzpicture}[baseline=-0.65ex,scale=0.5] \draw[densely dashed] (-1,-1) -- (1,-1); \node[hackennode] (middle) at ( 0, 0) {}; \node[hackennode] (left) at (-0.5,-1) {}; \node[hackennode] (right) at ( 0.5,-1) {}; \draw[hackenline,blue] (left) -- (middle) -- (right); \end{tikzpicture} \right\}$
}% End group with \Large
\end{document}


• The baseline option determines which height in the picture corresponds to the baseline in the surrounding text. We want the point (0,0) to be at the height of the equality sign, which is about 0.65ex above the baseline (in Computer Modern).
• The scale property (which doesn't affect line width and node size) is useful to get pictures in exactly the size you want them, without having to fiddle with the coordinates.
• The comma is set using TikZ to easily achieve the correct vertical position.
• I added \Large to get a larger equal sign and comma.
• Dashed lines are achieved with the option (surprise!) dashed. Now guess what densely dashed means.
• In order to have the vertices overpaint the dashed line, I added a fill=white option to hackennode.
• Martin's suggestion to wrap the tikzpictures in \mathord is a good one. You should do that to get better spacing.

Note, that in your picture the math symbols were actually produced with the graphics program and not with the text typesetting program (compare the braces and equal sign). This is of course possible to do with TikZ, but I don't see a good reason to do so.

-

You can use the tikzpictures inside \mathord{ } like normal math symbols. See below for an example. If you need the same diagram several times I would suggest you save it into a macro or box.

% Draws the shown example (but without circles)
\documentclass{standalone}
\usepackage{tikz}
\begin{document}

$\mathord{% \begin{tikzpicture}[baseline={(0,1)}] \draw[dashed] (0,0) -- (2,0); \draw[blue,very thick] (.5,0) -- (1,1) -- (1.5,0); \draw[red,very thick] (1,1) -- (1,2); \end{tikzpicture}% } = \left\{ \mathord{% \begin{tikzpicture}[baseline={(0,1)}] \draw[dashed] (0,0) -- (2,0); \draw[blue,very thick] (.75,0) -- (.5,1) (1.5,1) -- (1.25,0); \draw[red,very thick] (.5,1) -- (.5,2) (1.5,1) -- (1.5,2); \end{tikzpicture}% } \left| \mathord{% \begin{tikzpicture}[baseline={(0,1)}] \draw[dashed] (0,0) -- (2,0); \draw[blue,very thick] (.5,0) -- (1,1) -- (1.5,0); \end{tikzpicture}% } \right. \right\}$

\end{document}


Result:

-