![如何在不改变单元格位置的情况下将 TikZ 矩阵单元格内容锚定到“基线”?](https://linux22.com/image/251744/%E5%A6%82%E4%BD%95%E5%9C%A8%E4%B8%8D%E6%94%B9%E5%8F%98%E5%8D%95%E5%85%83%E6%A0%BC%E4%BD%8D%E7%BD%AE%E7%9A%84%E6%83%85%E5%86%B5%E4%B8%8B%E5%B0%86%20TikZ%20%E7%9F%A9%E9%98%B5%E5%8D%95%E5%85%83%E6%A0%BC%E5%86%85%E5%AE%B9%E9%94%9A%E5%AE%9A%E5%88%B0%E2%80%9C%E5%9F%BA%E7%BA%BF%E2%80%9D%EF%BC%9F.png)
我的表格中的单元格通常包含带上标的表达式。这会导致文本基线看起来在相邻单元格之间“游移”。例如
\begin{tikzpicture}[every node/.style={minimum size=7mm}]
\matrix{
\node[fill=gray!10]{$a$}; & \node[fill=gray!20]{$a^2$}; & \node[fill=gray!30]{$b$}; & \node[fill=gray!40]{$b^2$}; \\};
\end{tikzpicture}
生产
我理解将所有单元格的文本对齐到公共基线的方法是使用anchor=base
,但这会产生单元格混乱的副作用。例如
\begin{tikzpicture}[every node/.style={anchor=base,minimum size=7mm}]
\matrix{
\node[fill=gray!10]{$a$}; & \node[fill=gray!20]{$a^2$}; & \node[fill=gray!30]{$b$}; & \node[fill=gray!40]{$b^2$}; \\};
\end{tikzpicture}
生产
有没有办法将所有文本对齐到公共基线,而无需移动单元格?
\documentclass[]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[every node/.style={minimum size=7mm}]
\matrix{
\node[fill=gray!10]{$a$}; & \node[fill=gray!20]{$a^2$}; & \node[fill=gray!30]{$b$}; & \node[fill=gray!40]{$b^2$}; \\};
\end{tikzpicture}
\begin{tikzpicture}[every node/.style={anchor=base,minimum size=7mm}]
\matrix{
\node[fill=gray!10]{$a$}; & \node[fill=gray!20]{$a^2$}; & \node[fill=gray!30]{$b$}; & \node[fill=gray!40]{$b^2$}; \\};
\end{tikzpicture}
\end{document}
答案1
你可以固定高度和文本深度:
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[every node/.style={anchor=base,
text height=.8em,text depth=.2em,minimum size=7mm}]
\matrix{
\node[fill=gray!10]{$a$};&
\node[fill=gray!20]{$a^2$};&
\node[fill=gray!30]{$b$}; &
\node[fill=gray!40]{$b^2$};\\
};
\end{tikzpicture}
\end{document}