TikZ 中带有圆点的虚线

TikZ 中带有圆点的虚线

好吧,我想标题已经说明了一切。如果这对所有 TikZ 专家来说太简单了,请见谅:但我想要一种像“点状”一样的虚线图案,但要用圆点而不是小方块。我怎样才能以简单的方式实现这一点?

答案1

on您可以定义一个长度为 的自定义虚线图案0pt。如果您设置line cap=round,您将得到完美的圆圈。这种方法比使用 来实现相同目的dash pattern要快得多。decorations

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\draw [line width=3pt, line cap=round, dash pattern=on 0pt off 2\pgflinewidth] (0,0) -- (3,0) to [out=0, in=0, looseness=2] (3,-1);
\end{tikzpicture}

\end{document}

当然,整个东西可以做成一种新的风格,所以你只需要使用dots,它允许你舒适地使用设置点直径dot diameter和使用设置点间距dot spacing

\draw [dots] ...

\draw [red, dot diameter=5pt, dot spacing=5pt, dots] ...

\documentclass{article}

\usepackage{tikz}

\begin{document}

\makeatletter
\tikzset{
    dot diameter/.store in=\dot@diameter,
    dot diameter=3pt,
    dot spacing/.store in=\dot@spacing,
    dot spacing=10pt,
    dots/.style={
        line width=\dot@diameter,
        line cap=round,
        dash pattern=on 0pt off \dot@spacing
    }
}
\makeatother

\begin{tikzpicture}
\draw [dots] (0,0) -- (3,0) to [out=0, in=0, looseness=2] (3,-1);
\end{tikzpicture}

\begin{tikzpicture}
\draw [red, dot diameter=5pt, dot spacing=5pt, dots] (0,0) -- (3,0) to [out=0, in=0, looseness=2] (3,-1);
\end{tikzpicture}

\end{document}

答案2

另一种可能性是利用decorationsLionel 所建议的库:所展示的一个示例将基于 ,decorations.markings而另一个示例将基于decorations.shapes

装饰.标记

代码:

\documentclass[png,tikz,border=3pt]{standalone}
\usepackage{tikz} 
\usetikzlibrary{decorations.markings}

\pgfkeys{/tikz/.cd,
  circle color/.initial=black,
  circle color/.get=\circlecolor,
  circle color/.store in=\circlecolor,
}

\tikzset{dotted pattern/.style args={#1 and #2}{
   postaction=decorate,
   decoration={
    markings,
    mark=
    between positions 0 and 1 step #2
      with
      {
       \fill[radius=#1,\circlecolor] (0,0) circle;
      }
    }
  },
  dotted pattern/.default={1pt and 1.5mm},
}

\begin{document}
\begin{tikzpicture}
\path [dotted pattern] 
  (0,0) -- (3,0) to [out=0, in=0, looseness=2] (3,-1);
\end{tikzpicture}

\begin{tikzpicture}
\path [circle color=red,
   dotted pattern=1.5pt and 4mm] 
  (0,0) -- (3,0) to [out=0, in=0, looseness=2] (3,-1);
\end{tikzpicture}
\end{document}

提供:

在此处输入图片描述

在此处输入图片描述

确实,dotted pattern您可以分别通过样式自定义半径和圆之间的距离,而通过键circle color您可以更改颜色。

装饰.形状

代码:

\documentclass[png,tikz,border=3pt]{standalone}
\usepackage{tikz} 
\usetikzlibrary{decorations.shapes}

\pgfkeys{/tikz/.cd,
  circle color/.initial=black,
  circle color/.get=\circlecolor,
  circle color/.store in=\circlecolor,
}

\tikzset{dotted pattern/.style args={#1 and #2}{
   decorate,
   fill=\circlecolor,
   decoration={
    shape backgrounds,
    shape=circle,
    shape size=#1,
    shape sep={#2, between center}, 
    }
  },
  dotted pattern/.default={1pt and 1.5mm},
}

\begin{document}
\begin{tikzpicture}
\path [dotted pattern] 
  (0,0) -- (3,0) to [out=0, in=0, looseness=2] (3,-1); 
\end{tikzpicture}

\begin{tikzpicture}
\path [circle color=red,
   dotted pattern=1.5pt and 4mm] 
  (0,0) -- (3,0) to [out=0, in=0, looseness=2] (3,-1);
\end{tikzpicture}
\end{document}

结果仍是如上图所示。为了使用\draw而不是,可以在样式中\path添加。draw=nonedotted pattern

相关内容