2010-08-07 117 views
0

我剛剛在Tikz完成了我的第一個圖。看起來,我想它,但我不滿意我怎麼都「編碼」吧:改善Tikz圖

\begin{tikzpicture} 
[node distance=14mm, 
item/.style={rounded corners,rectangle, 
    thick, 
    minimum width=20mm, minimum height=10mm}] 

\node[item,draw=blue!50,fill=blue!20] (stack) {1394 Stack}; 
\node[item,left=of stack,draw=green!50,fill=green!20,yshift=-9mm] (app1) {Application}; 
\node[item,left=of stack,draw=green!50,fill=green!20,yshift=9mm] (app2) {Application}; 
\node[item,right=of stack,draw=orange!50,fill=orange!20] (ohci) {OHCI}; 
\node[item,right=of ohci,yshift=-15mm,draw=yellow!70,fill=yellow!35] (dev1) {Device}; 
\node[item,right=of ohci,yshift=0mm,draw=yellow!70,fill=yellow!35] (dev2) {Device}; 
\node[item,right=of ohci,yshift=15mm,draw=yellow!70,fill=yellow!35] (dev3) {Device}; 

\draw[thick] (app1) -- (stack) 
      (app2) -- (stack) 
      (stack) -- (ohci) 
      (ohci) -- (dev1) 
      (ohci) -- (dev2) 
      (ohci) -- (dev3); 

\node[xshift=7mm,yshift=1mm] (topUser) at (app1.east |- dev3.north) {}; 
\node[xshift=7mm,yshift=-1mm,label=above left:User space] (botUser) at (app1.east |- dev1.south) {}; 
\draw[dashed] (topUser) -- (botUser); 

\node[xshift=7mm,yshift=1mm] (topKern) at (stack.east |- dev3.north) {}; 
\node[xshift=7mm,yshift=-1mm,label=above left:Kernel space, 
label=above right:Hardware\phantom{p}] (botKern) at (stack.east |- dev1.south) {}; 
\draw[dashed] (topKern) -- (botKern); 
\end{tikzpicture} 

對此我難受的事情是:

如何我已經手動移動「應用程序」和「設備」節點使用yshift將它們彼此分開;我確信必須有一種更優雅的方式來製作簡單的樹狀結構

從圖片頂部到底部的行(topKern -- botKerntopUser -- botUser)使用xshift=7mm將這些手動對齊在x軸上以在兩個節點之間。

我使用\phantom{p}確保標籤「硬件」具有與其他兩個標籤相同的基準。

+1

僅供參考,這種問題會找上了[的TeX/LaTeX的堆疊交換網站(HTTP一個很好的家:// TEX。 stackexchange.com/),現在在公開測試版中。如果你願意,你可以關閉這個問題並在其他網站上重新發布。 – 2010-08-08 00:44:52

回答

1

要構建樹狀結構,請參閱pgfmanual.pdfMaking Trees Grow

對於線條,我會創建表示在兩個節點中間的節點,然後像使用垂直座標系一樣。您也可以使用current bounding box來識別「邊界」。

要正確對齊基線,請指定text heighttext depth。你的情況,例如在every label的風格。但是,正如你看到的,我做的標籤,如下節點...

\begin{tikzpicture}[level distance=35mm,node distance=15mm,text height=1.5ex,text depth=0.25ex] 

\begin{scope}[every node/.style={rounded corners,rectangle,thick,minimum width=20mm, minimum height=10mm}] 
\begin{scope}[level 1/.style={sibling distance=19mm,nodes={fill=green!20,draw=green!50}}] 
\node[draw=blue!50,fill=blue!20] (stack) {1394 Stack} [grow=left] 
    child {node (app2) {Application}} 
    child {node (app1) {Application}}; 
\end{scope} 

\begin{scope}[level 1/.style={sibling distance=15mm,nodes={fill=yellow!70,draw=yellow!35}}] 
\node[right= of stack,draw=orange!50,fill=orange!20] (ohci) {OHCI} [grow=right] 
    child {node {Device}} 
    child {node {Device}} 
    child {node {Device}}; 
\end{scope} 
\end{scope} 

\node[below=0mm of app1] (userspace) {User space}; 
\node at (userspace -| stack) (kernel) {Kernel}; 
\node at (userspace -| ohci) (hardware) {Hardware}; 

\path (app1) -- (stack) node[coordinate,midway] (between1) {}; 
\draw (ohci) -- (stack) node[coordinate,midway] (between2) {}; 

\draw[dashed] (current bounding box.north -| between1) -- (current bounding box.south -| between1); 
\draw[dashed] (current bounding box.north -| between2) -- (current bounding box.south -| between2); 

\end{tikzpicture} 
+0

\ path和\ draw命令(app1 - stack)和(ohci) - (stack)之間有什麼區別;我可以看到,將\路徑改爲\繪製會導致「雙重線」。只是爲了稍後獲得座標嗎? – 2010-08-08 18:54:44

+0

@Freddie:'\ path'命令只是構造一個路徑。 '\ draw'命令也構造一條路徑,但是另外繪製路徑。 '\ path'命令只是爲了獲得座標,'\ draw'命令一次完成兩件事。這是一個黑客。我真的想把所有東西放在一棵樹上,但這是不可能的。它應該是,但是如果我嘗試在左側和右側增加兩個節點,左側節點將按照左側將有三個節點的方式進行佈局。 – grddev 2010-08-09 06:47:14