考慮以下範例:
\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
來定位節點。