Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 8 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.