在我看来,tikz 中的 (path) fill 命令与 (default) 节点配合得不太好。请参阅下面的示例以了解我的意思。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node [above]{Using nodes};
\draw (0,0) node(a){}--+(0:1) node(b){};
\draw (a)--++(-120:1) node(c){}--+(0:2) node(d){}--(b);
\fill (a) ..controls +(-2.5,-2) and +(-2,-3) ..(d)--(c)--(a);
\end{tikzpicture}
\begin{tikzpicture}
\node [above]{Using coordinates};
\tikzstyle every node=[coordinate]
\draw (0,0) node(a){}--+(0:1) node(b){};
\draw (a)--++(-120:1) node(c){}--+(0:2) node(d){}--(b);
\fill (a) ..controls +(-2.5,-2) and +(-2,-3) ..(d)--(c)--(a);
\end{tikzpicture}
\begin{tikzpicture}
\node [above]{Using anchors};
\draw (0,0) node(a){}--+(0:1) node(b){};
\draw (a)--++(-120:1) node(c){}--+(0:2) node(d){}--(b);
\fill (a.base) ..controls +(-2.5,-2) and +(-2,-3) ..(d.base)--(c.base)--(a.base);
\end{tikzpicture}
\end{document}
填充出现这种奇怪行为的原因是什么?
答案1
要查看非封闭路径的问题,只需像这样绘制填充:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node [above]{Using nodes};
\path (0,0) node(a){}--+(0:1) node(b){};
\path (a)--++(-120:1) node(c){}--+(0:2) node(d){}--(b);
\draw[red, fill=yellow] (a) ..controls +(-2.5,-2) and +(-2,-3) ..(d)--(c)--(a);
\end{tikzpicture}
\end{document}
为了纠正它;这里有一种方法,即使用节点的中心点:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node [above]{Using nodes};
\path (0,0) node(a){}--+(0:1) node(b){};
\path (a)--++(-120:1) node(c){}--+(0:2) node(d){}--(b);
\draw[red, fill=yellow] (a.center) ..controls +(-2.5,-2) and +(-2,-3) ..(d.center)--(c.center)--cycle;
\end{tikzpicture}
\end{document}