Align the top of a TikZ picture with the text

Usually the bottom of a tikzpicture is aligned with the baseline of the text. If you want to change that, you can use the option baseline on the picture. This lets the bottom of the tikzpicture be 5 ex below the baseline of the text:

\draw [fill=blue!20!white] (0,0) circle [radius=1.5];
\draw [red, very thick, dashed] (1,0) -- (0,0) -- (0,1);

Instead of manually calculating the height of the drawing you want to include, you can use the provided current bounding box which is the rectangle containing all of the elements in the picture. This is a node with the usual anchors, i.e., north, south, east, west. To align the top of the box with the baseline of the text, use the following (note the parentheses, it’s a reference to a node!):

\begin{tikzpicture}[baseline=(current bounding box.north)]

Now probably you don’t want to align your picture with the baseline but with the top of the text. You can play around with the tikzpicture baseline further by introducing a yshift. For example a lowercase x is 1 ex high, so to align the top of the tikzpicture with the top of a lowercase x use a yshift of 1 ex (for uppercase letters or t, f, etc. try 1.6 ex). Be careful to copy all the different {( and [!:

\begin{tikzpicture}[baseline={([yshift={-1ex}]current bounding box.north)}]