真恶心。降部字符的存在/缺失似乎会改变文本的基线。如何正确对齐文本?
\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\def\h{0.55cm}
\def\w{1.5cm}
\tikzset{
myboxshape/.style={rectangle, outer sep=0, minimum width = \w, minimum height = \h, inner sep=0},
mybox/.style={myboxshape, very thin, draw},
}
\begin{document}
\begin{tikzpicture}
\matrix[]
{
\node[mybox] {Vertical}; & \node[mybox] {grungy}; \\
};
\end{tikzpicture}
\end{document}
答案1
我将在矩阵选项中定义节点的样式。对于所需的垂直对齐,我将在节点的样式中定义节点锚点、文本深度和文本高度:
\documentclass[tikz, border=1mm]{standalone}
\usetikzlibrary{matrix}
\def\h{5.5mm}
\def\w{15mm}
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of nodes,
nodes={draw, very thin, inner sep=0pt,
minimum width=\w, minimum height=\h,
text height=2ex, text depth=0.75ex, anchor=base}, % <---
column sep=-0.5\pgflinewidth,
row sep=0pt
]
{
Vertical & grungy \\
};
\end{tikzpicture}
\end{document}
答案2
快速破解:通过添加一个不可见的字符相同高度作为V
和相同深度但gy
零宽度。\vphantom{Vgy}
\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\def\h{0.55cm}
\def\w{1.5cm}
\tikzset{
myboxshape/.style={rectangle, outer sep=0, minimum width = \w, minimum height = \h, inner sep=0},
mybox/.style={myboxshape, very thin, draw},
}
\begin{document}
\begin{tikzpicture}
\matrix[]
{
\node[mybox] {\vphantom{gy}Vertical}; & \node[mybox] {grungy\vphantom{V}}; \\
};
\end{tikzpicture}
\end{document}
答案3
例如增加text depth=0.25ex,text height=0.8em
收益率
\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\def\h{0.55cm}
\def\w{1.5cm}
\tikzset{
myboxshape/.style={rectangle, outer sep=0, minimum width = \w, minimum height = \h, inner sep=0},
mybox/.style={myboxshape, very thin, draw,text depth=0.25ex,text height=0.8em},
}
\begin{document}
\begin{tikzpicture}
\matrix
{
\node[mybox] {Vertical}; & \node[mybox] {grungy}; \\
};
\end{tikzpicture}
\end{document}
请注意,有很多方法可以简化此过程。其中包括使用matrix of nodes
,即加载matrix
库但不真正使用它。
\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[myboxshape/.style={rectangle, outer sep=0,
minimum width = \pgfkeysvalueof{/tikz/mydims/w},
minimum height = \pgfkeysvalueof{/tikz/mydims/h}, inner sep=0},
mybox/.style={myboxshape, very thin, draw,text depth=0.25ex,text height=0.8em},
mydims/.cd,h/.initial=0.55cm,w/.initial=1.5cm]
\matrix[matrix of nodes, nodes={mybox}]
{
Vertical & grungy \\
};
\end{tikzpicture}
\end{document}