提克茲合身功能非常好,但是(1)如果我想將多個適合框對齊到相同的左側/寬度位置,以便所有適合框匹配,從而使不同的適合框很好地垂直對齊,該怎麼辦?
以及(2)(更高級),我還可以動態控制限定框之間的間距嗎?因此,這需要將子級從畫布座標空間變更為父級(限定框)座標空間,然後根據某些屬性佈局所有限定框(框之間的間距相同)。
PS:對於這個簡單的情況,您當然可以想出一些簡單的解決方案,例如在頂部/底部限定框中添加一個額外的不可見節點以使所有內容對齊,但是當限定框獲得更複雜的內容時,這當然不起作用。
一張圖來澄清:
微量元素:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usetikzlibrary{shapes}
\usetikzlibrary{arrows}
\usetikzlibrary{fit}
\begin{document}
\begin{tikzpicture}[>=stealth',semithick,auto]
\tikzstyle{surround} = [fill=blue!10,thick,draw=black,rounded corners=2mm]
\tikzstyle{obj} = [circle, minimum width=10pt, draw, inner sep=0pt]
\node[obj] (id1) at (2,2) {};
\node[obj] (id2) at (2,3) {};
\node[obj] (id3) at (2.5,3) {};
\node[obj] (id4) at (2.5,4) {};
\begin{pgfonlayer}{background}
\node[surround] (background) [fit = (id1)] {};
\node[surround] (background) [fit = (id2)(id3)] {};
\node[surround] (background) [fit = (id4)] {};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
答案1
下一個程式碼顯示了第一個問題的解決方案。您想要相似的擬合節點,那麼如果您使用相似的內部節點來建立它們,它們將具有相同的大小。
舉個例子
\node[surround, fit = (id1)(id3.east|-id1.center)] {};
將建立一個足夠大的節點id1
(固定高度和西部邊界),但它也將包含id3.east|-id1.center
固定東部邊界的座標。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usetikzlibrary{shapes}
\usetikzlibrary{arrows}
\usetikzlibrary{fit}
\begin{document}
\begin{tikzpicture}[>=stealth',semithick,auto,
surround/.style={fill=blue!10,thick,draw=black,rounded corners=2mm},
obj/.style={circle, minimum width=10pt, draw, inner sep=0pt}]
\node[obj] (id1) at (2,2) {};
\node[obj] (id2) at (2,3) {};
\node[obj] (id3) at (2.5,3) {};
\node[obj] (id4) at (2.5,4) {};
\begin{scope}[on background layer]
\node[surround, fit = (id2)(id3)] {};
\node[surround, fit = (id1)(id3.east|-id1.center)] {};
\node[surround, fit = (id4)(id2.west|-id4.center)] {};
\end{scope}
\end{tikzpicture}
\end{document}
更新:擬合節點之間的固定距離。
我不知道這個解決方案是否有效,但如果您已經知道您的擬合節點有多大,您可以將它們繪製在您想要的位置並objects
稍後填充:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,positioning}
\usetikzlibrary{shapes}
\usetikzlibrary{arrows}
\usetikzlibrary{fit}
\begin{document}
\begin{tikzpicture}[>=stealth',semithick,auto,
surround/.style={fill=blue!10,thick,draw=black,rounded corners=2mm},
obj/.style={circle, minimum width=10pt, draw, inner sep=0pt}]
\node[obj] (id2) at (2,3) {};
\node[obj] (id3) at (2.5,3) {};
\begin{scope}[on background layer]
\node[surround, fit = (id2)(id3)] (fit1) {};
\node[surround, fit = (id2)(id3), above=5mm of fit1] (fit2) {};
\node[surround, fit = (id2)(id3), below=15mm of fit1] (fit3) {};
\end{scope}
\node[obj] (id1) at (fit2-|id2) {};
\node[obj] (id1) at (fit3-|id3) {};
\end{tikzpicture}
\end{document}
第三版:和matrix of nodes
如果您obj
或多或少遵循正則分佈,fitting
則可以繪製 ,而不是它們matrix of nodes
。如果全部matrix
包含相同數量的列,則所有列都將具有相同的寬度,並且其高度將根據當前行數進行固定。與matrix
常規節點一樣,您可以固定它們之間的距離。
一個小例子:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,arrows}
\begin{document}
\begin{tikzpicture}[>=stealth',semithick,auto,
surround/.style={fill=blue!10, thick, draw=black,
rounded corners=2mm, matrix of nodes, nodes in empty cells, nodes={obj}, column sep=3pt, row sep=3pt},
obj/.style={circle, minimum width=10pt, draw, inner sep=0pt}]
\matrix[surround] (A) {& |[draw=none]| &\\
|[draw=none]| & &|[draw=none]|\\};
\matrix[surround, below=.5 of A] (B) { & &|[draw=none]|\\};
\matrix[surround, below=.5 of B] (C) {& \\& |[draw=none]| &\\};
\end{tikzpicture}
\end{document}