请考虑以下示例:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{xcolor}
\begin{document}
\def\mydist{1mm}
\begin{tikzpicture}[
mystyle/.style={
rectangle,
rounded corners,
draw=black,
very thick,
text width=2cm,
}
]
\node [mystyle] (A) {A\\text\\text\\text};
\node [mystyle, anchor=north] (B) at ($(A.south) - (0,\mydist)$) {B\\text\\text};
\node [mystyle, anchor=south west] (C) at ($(B.south east) + (\mydist,0)$) {C\\text};
% here is the problem
\node [mystyle, anchor=south, red] (D) at ($(C.north) + (0,\mydist)$) {D\\???};
\end{tikzpicture}
\end{document}
如何计算minimum height
节点 D 以使节点 A 和节点 D 的上边缘对齐?
或者我应该采用不同的方法?
答案1
这是一种可能性,使用let
语法;节点text depth
用于提供正确的大小:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\usepackage{xcolor}
\begin{document}
\begin{tikzpicture}[
mystyle/.style={
rectangle,
rounded corners,
draw=black,
line width=1pt,
text width=2cm,
},
node distance=2mm
]
\node [mystyle] (A) {A\\text\\text\\text\\text};
\node [mystyle,below=of A] (B) {B\\text\\text};
\node [mystyle, right=of B.south east,anchor=south west] (C) {C\\text};
\path let \p1=([yshift=2mm]C.north), \p2=(A.north) in
node [mystyle, anchor=south,red,text depth={\y2-\y1-\pgflinewidth-1.1\baselineskip},above=of C]
(D) {A\\B\\C\\D};
\end{tikzpicture}
\end{document}
与问题无关,但我使用node distance
(这保存了新长度的定义)和库提供的功能positioning
来定位节点。