现在我有这样的代码:
...
mnode/.style={circle,draw=black,fill=black,inner sep=0pt,minimum size=2pt},
...
\draw (0,0) node[mnode]{}
-- (1,1) node[mnode]{}
-- (2,2) node[mnode]{};
尽管实际的线要长得多。我可以做些什么来避免node[mnode]{}
在每个点之后进行指定吗?
答案1
在每个点放置节点(或任意 TikZ 代码)的一种方法是使用库show path construction
中的装饰decorations.pathreplacing
。以下是node at every point
一种将节点选项作为参数并在路径上的每个点放置具有所提供选项的节点的样式:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}[
mnode/.style={circle,draw=black,fill=black,inner sep=0pt,minimum size=2pt},
node at every point/.style={
decoration={
show path construction,
moveto code={\node at (\tikzinputsegmentfirst) [#1] {};},
lineto code={\node at (\tikzinputsegmentlast) [#1] {};},
curveto code={\node at (\tikzinputsegmentlast) [#1] {};}
},
postaction=decorate
}
]
\draw [node at every point=mnode] (0,0) -- (1,1) -- (2,2) -- (3,2) -- (4,5);
\end{tikzpicture}
\end{document}
或者你可以使用\foreach
循环来实现这一点:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[mnode/.style={circle,draw=black,fill=black,inner sep=0pt,minimum size=2pt}]
\draw (0,0) node[mnode]{}
\foreach \x/\y in {1/1,2/2,3/2,4/5}{
-- (\x,\y) node[mnode]{}
};
\end{tikzpicture}
\end{document}
答案2
您还可以执行以下操作:
使用此代码:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[mnode/.style={circle,draw=black,fill=black,inner sep=0pt,minimum size=2pt},
to path={ -- (\tikztotarget) node{} \tikztonodes},every node/.style={mnode}]
\draw[fill=orange!20] (0,0) node{} to (1,1) to (2,2) to (3,1) node [fill=red,minimum size=4pt]{} to (5,4) to (8,0) --cycle;
\end{tikzpicture}
\end{document}