12

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.

1 Answer 1

21

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.

1
  • 1
    I found yshift=-.5ex most satisfying, especially in combination with both ascenders (bdf...) and descenders (gjp...). Apr 14, 2017 at 0:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .