[Tex/LaTex] Vertically center a node with the current line in TikZ

nodestikz-pgfvertical alignment

Consider the following:

\documentclass{article}

\usepackage{tikz}

\begin{document}

  This is a
  \begin{tikzpicture}
    \node[draw, rectangle, align=left] {%
      And here \\[.3em]
      is
      \tikz \node[draw, rectangle, align=center] {a \\ nested};
    };
  \end{tikzpicture}
  one.
\end{document}

This produces

enter image description here

I would like to have the nodes placed so that the the outer node is vertically centered with the text outside the tikzpicture and the inner node is vertically centered with the "is" line of the outer node.

Best Answer

Add baseline=(current bounding box.center) as option to the tikzpicture and \tikz command. This places the baseline of the diagrams at its center and will cause a vertical alignment with the other baselines(!).

However, if you want to align it with the vertical center of the text, i.e. the center of "is", which looks most likely better, use
baseline={([yshift=-1ex]current bounding box.center)} instead. Adjust the value for your liking. Maybe .8ex might be better (an uppercase letter is about 1.6ex).

\documentclass{article}

\usepackage{tikz}

\begin{document}

  This is a
  \begin{tikzpicture}[baseline={([yshift=-.8ex]current bounding box.center)}]
    \node[draw, rectangle, align=left] {%
      And here \\[.3em]
      is
      \tikz [baseline={([yshift=-.8ex]current bounding box.center)}]
        \node[draw, rectangle, align=center] {a \\ nested};
    };
  \end{tikzpicture}
  one.
\end{document}

Result


Some other hints:

In general you can use \raisebox{<length>}{<content>} to raise or lower some material. You can use then \height, \depth, \width and \totalheight to refer to the original dimensions of the content. Use \dimexpr to do math:

\raisebox{\dimexpr-.5\height+.5\depth+.8ex\relax}{<content>}

There is also \vcenter{...} which will center things vertical, but it is intended to be used in mathmode.