2010-08-07 115 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