![如何改进 tikZ 图形箭头渲染?](https://linux22.com/image/255308/%E5%A6%82%E4%BD%95%E6%94%B9%E8%BF%9B%20tikZ%20%E5%9B%BE%E5%BD%A2%E7%AE%AD%E5%A4%B4%E6%B8%B2%E6%9F%93%EF%BC%9F.png)
我有以下 tikZ 图:
由。。。生产:
\tikzset{actor/.style={
rectangle,
minimum size=6mm,
very thick,
draw=red!50!black!50,
top color=white,
bottom color=red!50!black!20
}}
\tikzset{arrow/.style={
-latex, thick
}}
\begin{tikzpicture}[
background rectangle/.style={fill=yellow!10,rounded corners=1ex}, show background rectangle,
node distance=5mm and 8mm
]
\node (Input) [align=center]{GZip\\Stream};
\node (HeaderParser) [actor, right=of Input] {HeaderParser};
\node (HeaderCrc) [actor,above =of HeaderParser] {Crc Header};
\node (Inflate) [actor, right=of HeaderParser] {Inflate};
\node (Crc) [actor,above =of Inflate] {Crc};
\node (FooterParser) [actor,right=of Inflate] {Footer Parser};
\node (Output) [right=of FooterParser,align=center]{Decoded\\Stream};
\draw[arrow] (Input.east)--(HeaderParser.west);
\draw[arrow] (HeaderParser.north)--(HeaderCrc.south);
\draw[arrow] (HeaderParser.east)--(Inflate.west);
\draw[arrow] (Inflate.north)--(Crc.south);
\draw[arrow] (Inflate.east)--(FooterParser.west);
\draw[arrow] (FooterParser.east)--(Output.west);
\draw[arrow] (Crc.east)--(FooterParser.west);
\end{tikzpicture}
除了箭头之外,我对它几乎很满意。我不喜欢箭头末端和两个箭头在同一个节点上结束。我该如何改进?
答案1
这些shorten
键允许在节点周围留出更多空间(无需使用outer sep
其他黑客)这确实可以净化身材。
-|
其次,使用(|-
表示另一种方式)使向下的箭头沿直线行进。
east|north|south|west
最后,除非您使用曲线,否则不需要,--
将被解释为两个对象(在本例中为east -- west
和south -- north
)之间的最短路径。
所以基本上我会这样做:
\tikzset{actor/.style={
rectangle,
minimum size=6mm,
very thick,
draw=red!50!black!50,
top color=white,
bottom color=red!50!black!20
},
arrow/.style={
-latex, thick, shorten <=2pt,shorten >=2pt
}
}
\begin{tikzpicture}[background rectangle/.style={
fill=yellow!10,rounded corners=1ex
},
node distance=5mm and 8mm]
\node (Input) [align=center]{GZip\\Stream};
\node (HeaderParser) [actor, right=of Input] {HeaderParser};
\node (HeaderCrc) [actor,above =of HeaderParser] {Crc Header};
\node (Inflate) [actor, right=of HeaderParser] {Inflate};
\node (Crc) [actor,above =of Inflate] {Crc};
\node (FooterParser) [actor,right=of Inflate] {Footer Parser};
\node (Output) [right=of FooterParser,align=center]{Decoded\\Stream};
\draw[arrow] (Input)--(HeaderParser);
\draw[arrow] (HeaderParser)--(HeaderCrc);
\draw[arrow] (HeaderParser)--(Inflate);
\draw[arrow] (Inflate)--(Crc);
\draw[arrow] (Inflate)--(FooterParser);
\draw[arrow] (FooterParser)--(Output);
\draw[arrow] (Crc)-|(FooterParser);
\end{tikzpicture}
这使:
最后的解决方法是将之前的两行合并FooterParser
,为此您需要这个calc
库:
\draw[thick,shorten <=2pt] (Crc)-|($(Inflate.east)!.5!(FooterParser.west)$);
这将产生:
答案2
如果您不喜欢结束箭头,您可以更改它:在 pgfmanual 上有一个专门介绍此内容的部分(请参阅 23 Arrow Tip Library,2010 年 10 月 25 日版本)。
对于FooterParser
节点的终止连接点,甚至可以指定箭头结束的边界角度。为了更好地理解这一点,请参阅以下答案:通过 tikzmark 宏进行对齐。
请参阅以下 MWE(顺便说一句:发布的不是最小工作示例),其中我使用箭头尖端stealth
和端部连接点FooterParser.175
:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,backgrounds,positioning}
\tikzset{actor/.style={
rectangle,
minimum size=6mm,
very thick,
draw=red!50!black!50,
top color=white,
bottom color=red!50!black!20
}}
\tikzset{arrow/.style={
-stealth, thick
}}
\begin{document}
\begin{tikzpicture}[
background rectangle/.style={fill=yellow!10,rounded corners=1ex}, show background rectangle,
node distance=5mm and 8mm
]
\node (Input) [align=center]{GZip\\Stream};
\node (HeaderParser) [actor, right=of Input] {HeaderParser};
\node (HeaderCrc) [actor,above =of HeaderParser] {Crc Header};
\node (Inflate) [actor, right=of HeaderParser] {Inflate};
\node (Crc) [actor,above =of Inflate] {Crc};
\node (FooterParser) [actor,right=of Inflate] {Footer Parser};
\node (Output) [right=of FooterParser,align=center]{Decoded\\Stream};
\draw[arrow] (Input.east)--(HeaderParser.west);
\draw[arrow] (HeaderParser.north)--(HeaderCrc.south);
\draw[arrow] (HeaderParser.east)--(Inflate.west);
\draw[arrow] (Inflate.north)--(Crc.south);
\draw[arrow] (Inflate.east)--(FooterParser.west);
\draw[arrow] (FooterParser.east)--(Output.west);
\draw[arrow] (Crc.east)--(FooterParser.175);
\end{tikzpicture}
\end{document}
结果: