当闭合曲线被虚线化时,起点/终点处的虚线间距可能非常不合适。是否有选项可以告诉 TikZ 应稍微调整虚线间距以避免这种情况?(我当然可以自己做,但我有很多这样的曲线,因此很繁琐。)
以下是一个示例问题:
\documentclass[tikz,border=5]{standalone}
\begin{document}
\begin{tikzpicture}
\draw [dashed, line width=1pt]circle(0.655cm);
\end{tikzpicture}
\end{document}
答案1
使用dash pattern
\documentclass[border=5]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw [line width=.5pt,dash pattern=on 1pt off 2pt]circle(0.655cm);
\end{tikzpicture}
\end{document}
另请参阅手册第 168 页
答案2
这是一个解决方案:
\documentclass[border=5]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations}
\tikzset{
cycled dash pattern/.code args={on #1 off #2}{
% Use csname so catcode of @ doesn't have do be changed.
\csname tikz@addoption\endcsname{%
\pgfgetpath\currentpath%
\csname pgf@decorate@parsesoftpath\endcsname{\currentpath}{\currentpath}%
% Length of path
\pgfmathparse{\csname pgf@decorate@totalpathlength\endcsname}\let\lc=\pgfmathresult%
% Length of pattern
\pgfmathparse{#1+#2}\let\lp=\pgfmathresult%
% Scaling factor for pattern
\pgfmathparse{\lc/(\lp*round(\lc/\lp))}\let\f=\pgfmathresult%
% Actually scale the pattern
\pgfmathparse{#1*\f}\let\on=\pgfmathresult%
\pgfmathparse{#2*\f}\let\off=\pgfmathresult%
% Tell PGF to dash this line
\pgfsetdash{{\on}{\off}}{0pt}}%
}
}
\begin{document}
\begin{tikzpicture}
% The built-in version for comparison
\draw [line width=1pt, dash pattern=on 4pt off 4pt] (0,0) circle(0.655);
% Our version with automatically adapted pattern length
\draw [line width=1pt, cycled dash pattern=on 4pt off 4pt] (2,0) circle(0.655);
\end{tikzpicture}
\end{document}
大部分代码取自TikZ 虚线可以模拟 PSTricks 虚线吗?。我只是对其进行了修改,使得破折号以 开头on
并以 结尾off
,这与引用的答案中的破折号以 开头和结尾相反on
。