tikz沿路径装饰的一个问题

发布于 2021-06-11 12:07:04

下面的一段代码,在不同长度的路径上装修图案的个数是一样的,不好看!

\documentclass[tikz]{standalone}
 \usetikzlibrary{decorations.markings}
 \tikzset{zhuangding/.style={
 decoration={markings,
 mark=between positions 0 and 1 step 0.2 with
 {\node[circle,draw=black,fill=white]{};},
 },postaction={decorate}
 }
 }
 \begin{document}
 \begin{tikzpicture}
 \draw[zhuangding](0,0)--(11,0);
 \draw[zhuangding](0,-3)--(16,-3);
 \end{tikzpicture}
 \end{document}

下面的一段代码,在路径末尾“并没有装饰图案”,也不好看。

\documentclass[tikz]{standalone}
 \usetikzlibrary{decorations.markings}
 \tikzset{zhuangding/.style={
 decoration={markings,
 mark=between positions 0 and 1 step 15mm with
 {\node[circle,draw=black,fill=white]{};},
 },postaction={decorate}
 }
 }
 \begin{document}
 \begin{tikzpicture}
 \draw[zhuangding](0,0)--(11,0);
 \draw[zhuangding](0,-3)--(16,-3);
 \end{tikzpicture}
 \end{document}

请问各位大佬,应该如何设置 step 呢?

查看更多

关注者
0
被浏览
1.2k
寄存器
寄存器 2021-06-11
这家伙很懒,什么也没写!

cm为单位试试。

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.markings}
\tikzset{zhuangding/.style={
           decoration={markings,
                       mark=between positions 0 and 1 step 1cm with
                       {\node[circle,draw=black,fill=white]{};
             },
           },
           postaction={decorate}
  }
}
\begin{document}
  \begin{tikzpicture}
    \draw[zhuangding](0,0)--(11,0);
    \draw[zhuangding](0,-3)--(16,-3);
  \end{tikzpicture}
\end{document}

PS. 这种拼音命名,实在是不习惯。

2 个回答
越来越少
越来越少 2021-06-12
这家伙很懒,什么也没写!

这里的 step 应该是相邻两个 mark 的间距,在

between positions 0 and 1 step <x> with ...

中,参数 <x> 会被 \pgfmathparse 处理,参数 <x> 的两个特征:(1)正负号,(2)是否带有长度单位,都对间距有影响。例如 0.2, 那么间距就是 0.2*L, 其中的 L 是被装饰路径的总长度。作为装饰的 mark 是逐个创建的,在创建过程中,如果剩余的、尚未被装饰的路径长度小于 0.2*L, 那么就不做装饰。详细情况参考《pgflibrarydecorations.markings.code.tex》。

如果希望 mark 出现在被装饰路径的末端,那么应注意路径长度与 mark 间距相搭配,就是说,L/间距 应该是个整数。

如果不搭配,还想把一个 mark 放在路径的末端,可以这么做

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.markings}
\tikzset{
  zhuangding A/.style={
    decorate,
    decoration={
      markings,
      mark=between positions 0 and 1 step 15mm with
        {\node[circle,draw=black,fill=white]{};},
    },
  },
  zhuangding B/.style={
    decorate,
    decoration={
      markings,
      mark=at position 1 with
        {\node[circle,draw=black,fill=white]{};},
    },
  },
}
\begin{document}
\begin{tikzpicture}
  \draw[postaction={zhuangding A},postaction={zhuangding B}](0,0)--(11,0);
  \draw[red,postaction={zhuangding A},postaction={zhuangding B}](0,-3)--(16,-3);
\end{tikzpicture}
\end{document}

我大略查了一下,如果想单纯获取一个路径的长度,可以这么做:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations}
\begin{document}
\begin{tikzpicture}
  \draw [save path=\aaaa] (0,0) arc [start angle=60,end angle=180,x radius=2,y radius=1];
\end{tikzpicture}

\makeatletter
\pgf@decorate@parsesoftpath\aaaa\bbbb
\meaning\pgf@decorate@totalpathlength

\meaning\bbbb
\makeatother
\end{document}

其中的 \pgf@decorate@totalpathlength 就是路径的总长度。

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览