# Aligning a Tikz Picture in line with the surrounding Text

I'd like to align a Tikz Picture in line with the surrounding Text. Here is a MWE Example:

\documentclass[a4paper,10pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows,chains,matrix,positioning,scopes}

\makeatletter
\tikzset{joinlabel/.code=\tikzset{after node path={%
\ifx\tikzchainprevious\pgfutil@empty\else(\tikzchainprevious)%
edge[every join]#1(\tikzchaincurrent)\fi}}}
\makeatother

\tikzset{>=stealth',every on chain/.append style={join},
every join/.style={->}}

\begin{document}

Text, more text
\begin{tikzpicture}[start chain] {
\node[on chain] {$0$};
\node[on chain] {$A$} ;
\node[on chain, joinlabel={node[above] {$\scriptstyle\varphi$}}] {$B$};
\node[on chain, joinlabel={node[above] {$\scriptstyle\psi$}}] {$C$};
\node[on chain] {$0$}; }
\end{tikzpicture}
text and more text go here...
\end{document}


Here's the Output:

To make things more explicit: I am looking for a solution that does not change the code much but however lets me make align the TikZ picture in line with the surrounding Tikz.

Code taken from: This code is taken from here -- Stephan Kottwitz's TeXamples.

(I have some very bad work-arounds that I got to know when I posted it on chat. I may compile them and write them up as a CW answer.)

-
text \begin{tikzcd}[baseline=-0.85ex] 0 \arrow{r}{} & A \arrow{r}{\varphi} & B \arrow{r}{\psi} & C \arrow{r}{} & 0 \end{tikzcd} text compiles well! But, this changes the code drastically. (Credit: Gonzalo Medina in Chat) – kan Aug 5 '12 at 13:15
Have you tried the wrapfig package? – N3buchadnezzar Aug 5 '12 at 13:15
@N3buchadnezzar Not yet! I'll check that out. Thanks for your comment! – kan Aug 5 '12 at 13:16
You could give a name to a node, say the first zero with (0), and then for the tikzpicture options, add a baseline=(0.base). But do you really need tikz for this when you could just do: \def\To{\mathop{\;\hbox to2.5em{\rightarrowfill}\;}\limits} Text, more text $0\To A \To^\varphi B \To^\psi C \To 0$ text and more text go here\dots \bye – morbusg Aug 5 '12 at 13:27
@morbusg Yes, your first suggestion solves the problem! And, what are those \dots \bye meant for? They fail to compile. But, yeah, thanks for that suggestion. – kan Aug 5 '12 at 13:50

For minimal intervention in general situations like this, use the baseline option for tikzpicture combined with the coordinate of a node in the diagram. The simplest thing in this case is to label the first node with O (letter O) and use its baseline. Here is the working code:

\begin{tikzpicture}[baseline=(O.base),start chain] {
\node (O) [on chain] {$0$};
\node[on chain] {$A$};
\node[on chain, joinlabel={node[above] {$\scriptstyle\varphi$}}] {$B$};
\node[on chain, joinlabel={node[above] {$\scriptstyle\psi$}}] {$C$};
\node[on chain] {$0$}; }
\end{tikzpicture}


However as @percusse points out, for this particular example the nodes are automatically given the names chain-# where # is the node number. So real minimal intervention is

\begin{tikzpicture}[baseline=(chain-1.base),start chain] {
\node[on chain] {$0$};
\node[on chain] {$A$};
\node[on chain, joinlabel={node[above] {$\scriptstyle\varphi$}}] {$B$};
\node[on chain, joinlabel={node[above] {$\scriptstyle\psi$}}] {$C$};
\node[on chain] {$0$}; }
\end{tikzpicture}


Of course, there are many other ways to make your particular diagram, with or without tikz. One possibility is $$0 \longrightarrow A \stackrel{\varphi}{\longrightarrow} B \stackrel{\psi}{\longrightarrow} C \longrightarrow 0$$:

which has slightly shorter arrows and is thus probably better for an inline case like this (I also prefer the vertical placing of the arrows). amsmath has some extensible arrows that could also be used.

-
If the chain is not named explicitly, the nodes are named in the order of appearance via (chain-#). Hence instead of (O) you can directly use (chain-1.base). – percusse Aug 5 '12 at 16:20
Excellent. That is less intervention in this case. – Andrew Swann Aug 6 '12 at 11:01