我想在 TikZ 中实现以下目标:
要获得线条的精确宽度,我没有遇到任何问题 - 问题在于沿着线条添加任意数量的障碍,这些障碍之间的距离相等。我还希望能够在障碍之间添加文本(例如 0、1、2、...,但这只是一个例子)并且我不想手动添加和定位障碍,因为我相信 TikZ 会为我做得更好。但是怎么做呢?
答案1
您可以使用border
在TikZ
(参见手册,第 324 页)。不幸的是,装饰不会在一条线上画出最后一个刻度,因此我采取了一些解决方法,向后绘制了第二个装饰:
\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{decorations}
\usetikzlibrary{decorations.pathreplacing}
\newcommand{\tickedline}[6]{% Start, End, line options, segment length, amplitude, angle
\begin{scope}[decoration={border, segment length=#4, amplitude=#5, angle=#6}]%
\draw[#3,postaction={decorate,draw}] (#1) -- (#2);%
\draw[decoration={border, segment length=#4, amplitude=#5, angle=180-#6, mirror}, #3, postaction={decorate,draw}] (#2) -- (#1);%
\end{scope}%
}
\begin{document}
\begin{tikzpicture}
\tickedline{0,0}{5.7,0}{thin, green}{3mm}{5mm}{45}
\tickedline{1,1}{6,1}{very thick, red}{5mm}{2mm}{90}
\tickedline{0,2}{5,5}{thick, blue}{2mm}{4mm}{38}
\end{tikzpicture}
\end{document}
一些例子:
您可能会注意到,第一条和最后一条蓝线比其他所有线都细。这是因为我绘制了一条前向和一条后向装饰线,并且线的长度不是线宽的整数倍。因此必须任何一个为线宽选择合适的线段长度或者找到一种方法来强制TikZ
在最后一段上绘制装饰线。
编辑1:我解决了这个问题。现在,您无需指定段长度,而是可以指定段数或刻度数(即段数 +1):
\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{decorations}
\usetikzlibrary{decorations.pathreplacing}
\newcommand{\tickedline}[6]{% Start, End, line options, segment length, amplitude, angle
\begin{scope}[decoration={border, segment length=#4, amplitude=#5, angle=#6}]%
\draw[#3,postaction={decorate,draw}] (#1) -- (#2);%
\draw[decoration={border, segment length=#4, amplitude=#5, angle=180-#6, mirror}, #3, postaction={decorate,draw}] (#2) -- (#1);%
\end{scope}%
}
\newcommand{\tikzsegments}[8]{% x1,y1, x2,x2, line options, number of segments, amplitude, angle
\pgfmathsetmacro{\lengthsegment}{sqrt(pow(#3-#1,2)+pow(#4-#2,2))/#6}
\begin{scope}[decoration={border, segment length=\lengthsegment cm, amplitude=#7, angle=#8}]%
\draw[#5,postaction={decorate,draw}] (#1,#2) -- (#3,#4);%
\draw[decoration={border, segment length=\lengthsegment cm, amplitude=#7, angle=180-#8, mirror}, #5, postaction={decorate,draw}] (#3,#4) -- (#1,#2);%
\end{scope}%
}
\newcommand{\tikzticks}[8]{% x1,y1, x2,x2, line options, number of ticks, amplitude, angle
\pgfmathsetmacro{\lengthsegment}{sqrt(pow(#3-#1,2)+pow(#4-#2,2))/(#6-1)}
\begin{scope}[decoration={border, segment length=\lengthsegment cm, amplitude=#7, angle=#8}]%
\draw[#5,postaction={decorate,draw}] (#1,#2) -- (#3,#4);%
\draw[decoration={border, segment length=\lengthsegment cm, amplitude=#7, angle=180-#8, mirror}, #5, postaction={decorate,draw}] (#3,#4) -- (#1,#2);%
\end{scope}%
}
\begin{document}
%\begin{tikzpicture}
%\tickedline{0,0}{5.7,0}{thin, green}{3mm}{5mm}{45}
%\tickedline{1,1}{6,1}{very thick, red}{5mm}{2mm}{90}
%\tickedline{0,2}{5,5}{thick, blue}{2mm}{4mm}{38}
%\end{tikzpicture}
\begin{tikzpicture}
\tikzsegments{0}{0}{5.7}{0}{thin, green}{19}{5mm}{45}
\tikzsegments{1}{1}{6}{1}{very thick, red}{14}{2mm}{90}
\tikzsegments{0}{2}{5}{5}{thick, blue}{30}{4mm}{38}
\end{tikzpicture}
\hspace{2cm}
\begin{tikzpicture}
\tikzticks{0}{0}{5.7}{0}{thin, green}{11}{5mm}{45}
\tikzticks{1}{1}{6}{1}{very thick, red}{11}{2mm}{90}
\tikzticks{0}{2}{5}{5}{thick, blue}{11}{4mm}{38}
\end{tikzpicture}
\end{document}
现在,即使在非水平线上它也能正常工作:
答案2
这里有一个例子。一个\drawsnags
带有三个参数的新命令,即起点、线长和障碍数。
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
%\drawsnags{starting point}{line width}{number of snags}
\newcommand{\drawsnags}[3]{
\draw[line width=3pt, line cap=rect] #1--++(#2,0);
\foreach \i in {0,...,#3}
\draw[line width=3pt,shift={#1}] ({\i*#2/#3},0) --++(0,0.2);}
\begin{document}
\begin{tikzpicture}
\drawsnags{(0,0)}{5}{3};
\drawsnags{(0,2)}{8}{5};
\drawsnags{(1,-1)}{3}{4};
\end{tikzpicture}
\end{document}
您必须调整起点和终点的连接。
编辑:根据 Andrew 的建议,我添加了line cap=rect
选项。新结果是:
仍不完美但已经有所进步。