![如何垂直对齐两个节点内的两个文本,其中一个带下划线,或者它们具有不同的高度(由于存在降部)?](https://linux22.com/image/373103/%E5%A6%82%E4%BD%95%E5%9E%82%E7%9B%B4%E5%AF%B9%E9%BD%90%E4%B8%A4%E4%B8%AA%E8%8A%82%E7%82%B9%E5%86%85%E7%9A%84%E4%B8%A4%E4%B8%AA%E6%96%87%E6%9C%AC%EF%BC%8C%E5%85%B6%E4%B8%AD%E4%B8%80%E4%B8%AA%E5%B8%A6%E4%B8%8B%E5%88%92%E7%BA%BF%EF%BC%8C%E6%88%96%E8%80%85%E5%AE%83%E4%BB%AC%E5%85%B7%E6%9C%89%E4%B8%8D%E5%90%8C%E7%9A%84%E9%AB%98%E5%BA%A6%EF%BC%88%E7%94%B1%E4%BA%8E%E5%AD%98%E5%9C%A8%E9%99%8D%E9%83%A8%EF%BC%89%EF%BC%9F.png)
我有这里的图像和下面的代码。我怎样才能使节点中文本的垂直水平在此处正确对齐?我使用 tikz 而不是 \table,因为我需要绘制如下所示的线条 - 我认为使用常规 \table 不会遇到此问题。
这不仅仅是下划线的问题(尽管这会使它更加明显),而且“电子邮件”的 y 位置似乎也略低于“电话号码”。
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{automata,positioning,arrows.meta,shapes,calc}
\usetikzlibrary{positioning}
\begin{document}
\newcommand{\mylw}{1pt}
\begin{tikzpicture}[font=\small, minimum height=0.8cm, line width=\mylw]
\draw (0, 0)
node [rectangle, draw] (c0) {\underline{customer\_id}}
node [rectangle, draw, right= -\mylw of c0] (c1) {email}
node [rectangle, draw, right= -\mylw of c1] (c2) {phone\_ number};
\draw
node [rectangle, draw, below=of c0] (c2) {\underline{customer\_id}}
node [rectangle, draw, black, right= -\mylw of c2] (c3) {street};
node [rectangle, draw, black, right= -\mylw of c2] (c4) {suburb};
\draw[->, rounded corners=5pt] (c2.south) -- ++(0, -0.5) -- ++(-1.5, 0.0) -- ++(0, 1.5) -- ++(1.5, 0.0) -- (c0.south);
\end{tikzpicture}
\end{document}
答案1
\strut
在每行末尾添加一个将确保基线统一。 (\strut
定义为具有括号的最大高度和深度,至少对于计算机现代字体而言,其设计用于正确括住所有合理的基线符号。)
这是修改后的代码,其中有一些额外的修复(第二个块中的分号少了一个, “郊区”的定位从 改为 ,并且在其中一个连接器后省略了一个空格\draw
)。[c2
[c3]
\_
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{automata,positioning,arrows.meta,shapes,calc}
\usetikzlibrary{positioning}
\begin{document}
\newcommand{\mylw}{1pt}
\begin{tikzpicture}[font=\small, minimum height=0.8cm, line width=\mylw]
\draw (0, 0)
node [rectangle, draw] (c0) {\underline{customer\_id}\strut}
node [rectangle, draw, right= -\mylw of c0] (c1) {email\strut}
node [rectangle, draw, right= -\mylw of c1] (c2) {phone\_number\strut};
\draw
node [rectangle, draw, below=of c0] (c2) {\underline{customer\_id}\strut}
node [rectangle, draw, black, right= -\mylw of c2] (c3) {street\strut}
node [rectangle, draw, black, right= -\mylw of c3] (c4) {suburb\strut};
\draw[->, rounded corners=5pt] (c2.south) -- ++(0, -0.5) -- ++(-1.5, 0.0) -- ++(0, 1.5) -- ++(1.5, 0.0) -- (c0.south);
\end{tikzpicture}
\end{document}
答案2
我认为tikz
解决这个问题的方法是设置text height=...
和text depth=...
。
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{automata,positioning,arrows.meta,shapes,calc}
\usetikzlibrary{positioning}
\tikzset{mynode/.style={rectangle, draw, text height=2.5ex, text depth=1ex},}
\begin{document}
\newcommand{\mylw}{1pt}
\begin{tikzpicture}[font=\small, minimum height=0.8cm, line width=\mylw]
\draw (0, 0)
node [mynode] (c0) {\underline{customer\_id}}
node [mynode, right= -\mylw of c0] (c1) {email}
node [mynode, right= -\mylw of c1] (c2) {phone\_ number};
\draw
node [mynode, below=of c0] (c2) {\underline{customer\_id}}
node [mynode, black, right= -\mylw of c2] (c3) {street};
node [mynode, black, right= -\mylw of c2] (c4) {suburb};
\draw[->, rounded corners=5pt] (c2.south) -- ++(0, -0.5) -- ++(-1.5, 0.0) -- ++(0, 1.5) -- ++(1.5, 0.0) -- (c0.south);
\end{tikzpicture}
\end{document}
答案3
您可以\vphantom
在每个节点中添加一个带有文本的命令:
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{automata,positioning,arrows.meta,shapes,calc}
\usetikzlibrary{positioning}
\begin{document}
\newcommand{\mylw}{1pt}
\begin{tikzpicture}[font=\small, minimum height=0.8cm, line width=\mylw]
\draw (0, 0)
node [rectangle, draw] (c0) {\underline{customer\_id}\vphantom{pq/1}}
node [rectangle, draw, right= -\mylw of c0] (c1) {email\vphantom{pq/1}}
node [rectangle, draw, right= -\mylw of c1] (c2) {phone\_ number\vphantom{pq/1}};
\draw
node [rectangle, draw, below=of c0] (c2) {\underline{customer\_id}\vphantom{pq/1}}
node [rectangle, draw, black, right= -\mylw of c2] (c3) {street\vphantom{pq/1}};
node [rectangle, draw, black, right= -\mylw of c2] (c4) {suburb\vphantom{pq/1}};
\draw[->, rounded corners=5pt] (c2.south) -- ++(0, -0.5) -- ++(-1.5, 0.0) -- ++(0, 1.5) -- ++(1.5, 0.0) -- (c0.south);
\end{tikzpicture}
\end{document}
如果你愿意,你可以把它变成一个命令:
\newcommand{\fixvalign}{\vphantom{1/pqlHA}}
甚至可以包括更多every node
内容