我有这里的图像和下面的代码。我怎样才能使节点中文本的垂直水平在此处正确对齐?我使用 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
内容