14

I am using a TikZ picture followed by a text. How do I create a picture that aligns with the top of the text?

\begin{minipage}[t]{0.45\textwidth}
\tikz[baseline=(current bounding box.north)] {
    \draw (0,0) rectangle (2,-3);
} text
\end{minipage}

enter image description here

Martin Scharrer
  • 262,582
Regis Santos
  • 14,463

1 Answers1

11

You need to shift the baseline lower to shift the tikzpicture higher, e.g. by the height of a \strut or something between 1ex (height of lowercase letter) and 1.6ex (~height of uppercase letter), depending on your liking.
See also my question Determine height and depth of letters relative to the font size for a discussion about the correct height.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{minipage}[t]{0.45\textwidth}
\tikz[baseline={([yshift={-\ht\strutbox}]current bounding box.north)}] {
    \draw (0,0) rectangle (2,-3);
} text
\end{minipage}
\end{document}

yshift={-\ht\strutbox}:

Result

yshift={-1.2ex}:

Result

Martin Scharrer
  • 262,582
  • By trial and error, I found yshift={-1.1*\ht\strutbox} was about the right amount to center a circle vertically with the text. Anyone know a more exact way to accomplish this? – Addison Klinke Jul 26 '20 at 14:24