TikZ – How to Position a TikZPicture Vertically in the Middle of a Tabularray Cell

tabularraytikz-pgf

I want to place a tikzpicture in a tabularray cell but somehow it is not in the middle but offset to the upper half…

\documentclass{article}

\usepackage{tabularray}
\usepackage{tikz}

\newcommand{\resultgraphproportion}[3]{\fpeval{(#3-#1)/(#2-#1)}}

\newcommand{\resultgraphheight}{0.5}
\NewDocumentCommand{\showresultgraphT}{}{
    \begin{tikzpicture}
        \draw[fill=red] (0,0) rectangle (1,\resultgraphheight);
        \node[gray] at (0,-0.1) {\tiny 20};
    \end{tikzpicture}
}

\begin{document}
    \begin{tblr}{lcc}
        \hline
        abc &   \showresultgraphT & great \\
        \hline
    \end{tblr}
\end{document}

How can I make the tikzpicture appear in the vertical middle of the cell?

enter image description here

Best Answer

This solution centers a \strut within the tikzpicture, then uses its baseline.

\documentclass{article}

\usepackage{tabularray}
\usepackage{tikz}

\newcommand{\resultgraphproportion}[3]{\fpeval{(#3-#1)/(#2-#1)}}

\newcommand{\resultgraphheight}{0.5}
\NewDocumentCommand{\showresultgraphT}{}{
    \begin{tikzpicture}[baseline=(phantom.base)]
        \draw[fill=red] (0,0) rectangle (1,\resultgraphheight);
        \node[gray, inner sep=0pt] at (0,-0.1) {\tiny 20};
        \node[inner sep=0pt] (phantom) at (current bounding box.center) {\strut};
    \end{tikzpicture}
}

\begin{document}
    \begin{tblr}{lcc}
        \hline
        abc &   \showresultgraphT & great \\
        \hline
    \end{tblr}
\end{document}

demo