我想将一个放置tikzpicture
在一个tabularray
单元格中,但不知何故它不在中间,而是偏移到上半部分......
\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}
我怎样才能让它tikzpicture
出现在单元格的垂直中间?
答案1
该解决方案将\strut
tikzpicture 置于中心,然后使用其基线。
\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}
答案2
您需要使用图像选项将图像基线位置移动到其垂直中心baseline=(current bounding box.center)
:
\documentclass{article}
\usepackage{tabularray}
\usepackage{tikz}
\newcommand{\resultgraphproportion}[3]{\fpeval{(#3-#1)/(#2-#1)}}
\newcommand{\resultgraphheight}{0.5}
\NewDocumentCommand{\showresultgraphT}{}{
\begin{tikzpicture}[baseline=(current bounding box.center)]
\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}
编辑:
关于您的评论:
- 图形肯定是垂直居中的......
- 要看到这一点,只需用灰色文本在节点周围绘制边框:
\documentclass{article}
\usepackage{tabularray}
\usepackage{tikz}
\newcommand{\resultgraphproportion}[3]{\fpeval{(#3-#1)/(#2-#1)}}
\newcommand{\resultgraphheight}{0.5}
\NewDocumentCommand{\showresultgraphT}{}{
\begin{tikzpicture}[baseline=(current bounding box.center)]
\draw[fill=red] (0,0) rectangle (1,\resultgraphheight);
\node[draw, gray, font=\tiny] at (0,-0.1) {20};
\end{tikzpicture}
}
\begin{document}
\begin{tblr}{hlines,
colspec = {lcc},
}
abc & \showresultgraphT & great \\
\end{tblr}
\end{document}
现在的结果是:
如果您希望第二个节点内容(编号 20)和底部表格之间的垂直距离与\hline
橙色框和顶部之间的垂直距离相同\hline
,那么您需要重新设计图像:删除节点内容和其边框之间的垂直分隔,并手动对齐其基线,如下所示:
\documentclass{article}
\usepackage{tabularray}
\usepackage{tikz}
\newcommand{\resultgraphproportion}[3]{\fpeval{(#3-#1)/(#2-#1)}}
\newcommand{\resultgraphheight}{0.5}
\NewDocumentCommand{\showresultgraphT}{}{
\begin{tikzpicture}[baseline=(([yshift=3pt] current bounding box.base)}] % <--- changed
\draw[fill=red] (0,0) rectangle (1,\resultgraphheight);
\node[inner ysep=0pt, % <--- added
gray, font=\tiny] at (0,-0.1) {20};
\end{tikzpicture}
}
\begin{document}
\begin{tblr}{hlines,
colspec = {lcc},
}
abc & \showresultgraphT & great \\
\end{tblr}
\end{document}
现在图像和文本到行边框之间的垂直距离相同。
这就是你要找的吗?