改进 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 手动移动“应用程序”和“设备”节点code> 将它们彼此分开;我确信必须有一种更优雅的方式来生成简单的树状结构
这些行(topKern -- botKern
和topUser -- botUser
)从顶部开始图片底部;使用 xshift=7mm
在两个节点之间的 x 轴上手动对齐它们。
我使用 \phantom{p}
来确保标签“Hardware”与其他两个标签具有相同的基线。
I have just finished my first diagram in Tikz. It looks as I wanted it to, but am unhappy with how I have 'coded' it:
\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}
The things which I am uncomfortable with are:
How I have manually moved the "Application" and "Device" nodes using yshift
to space them apart from one another; I am sure that there must be a more elegant way of producing a simple tree-like structure
The lines (topKern -- botKern
and topUser -- botUser
) going from the top of the picture to the bottom; these are manually aligned on the x-axis to be between two nodes using xshift=7mm
.
My use of \phantom{p}
to ensure the label "Hardware" has the same baseline as the other two labels.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要构建树结构,请参阅
pgfmanual.pdf
,让树生长。对于线条,我将创建代表两个节点中间的节点,然后像您一样使用垂直坐标系。您还可以使用
当前边界框
来识别“边界”。要正确对齐基线,请指定
文本高度
和文本深度
。在您的情况下,例如在样式每个标签
中。但正如你所看到的,我将标签作为下面的节点......To build a tree structure, consult
pgfmanual.pdf
, Making Trees Grow.For the lines, I would create nodes representing in the middle of two nodes, and then use the perpendicular coordinate system as you did. Also you can use
current bounding box
to identify the "border".To align baselines correctly, specify
text height
andtext depth
. In your case, for instance in the styleevery label
. But as you see, I did the labels as nodes below...