我正在尝试从另一个程序输出低级 tikz 命令,作为绘制任意线条(和点等)的通用方法。我对语法不够熟悉,不知道在哪里可以找到好的例子(而且手册一开始有点让人不知所措)。
用伪代码来说,我希望实现以下目标:
% define some arrays of numbers and parameters
x = {x1, x2, x3, ..., xn}
y = {y1, y2, y3, ..., yn}
c = {c1, c2, c3, ..., cn}
w = {w1, w2, w3, ..., wn}
% set the start coordinate
current_point = (x(1), y(1))
% loop over all arrays to draw a path
for each (i = 1:n){
draw [colour = c(i), line width = w(i)] current_point -- ++ (x(i), y(i)) coordinate current_point;
}
我认为这是一个足够通用的语法,但仍然可读。然而,我坚持使用定义多个数组并在 foreach 循环中重用它们的语法。这是我失败的尝试(应该显示正弦波),
\documentclass[tikz]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[x=1cm,y=0.4cm]
\def\tabx{{0,0.06,0.13,0.19,0.25,0.32,0.38,0.44,0.51,0.57,0.63,0.7,0.76,0.83,0.89,0.95,1.02,1.08,1.14,1.21,1.27,1.33,1.4,1.46,1.52,1.59,1.65,1.71,1.78,1.84,1.9,1.97,2.03,2.09,2.16,2.22,2.28,2.35,2.41,2.48,2.54,2.6,2.67,2.73,2.79,2.86,2.92,2.98,3.05,3.11,3.17,3.24,3.3,3.36,3.43,3.49,3.55,3.62,3.68,3.74,3.81,3.87,3.93,4,4.06,4.13,4.19,4.25,4.32,4.38,4.44,4.51,4.57,4.63,4.7,4.76,4.82,4.89,4.95,5.01,5.08,5.14,5.2,5.27,5.33,5.39,5.46,5.52,5.59,5.65,5.71,5.78,5.84,5.9,5.97,6.03,6.09,6.16,6.22,6.28}};
\def\taby{{0,0.95,1.86,2.7,3.45,4.07,4.55,4.86,4.99,4.95,4.73,4.33,3.78,3.09,2.29,1.41,0.48,-0.48,-1.41,-2.29,-3.09,-3.78,-4.33,-4.73,-4.95,-4.99,-4.86,-4.55,-4.07,-3.45,-2.7,-1.86,-0.95,0,0.95,1.86,2.7,3.45,4.07,4.55,4.86,4.99,4.95,4.73,4.33,3.78,3.09,2.29,1.41,0.48,-0.48,-1.41,-2.29,-3.09,-3.78,-4.33,-4.73,-4.95,-4.99,-4.86,-4.55,-4.07,-3.45,-2.7,-1.86,-0.95,0,0.95,1.86,2.7,3.45,4.07,4.55,4.86,4.99,4.95,4.73,4.33,3.78,3.09,2.29,1.41,0.48,-0.48,-1.41,-2.29,-3.09,-3.78,-4.33,-4.73,-4.95,-4.99,-4.86,-4.55,-4.07,-3.45,-2.7,-1.86,-0.95,0}};
\coordinate (current point) at (0,0);
\foreach[count=\i, evaluate=\i as \y using \taby(\i), evaluate=\i as \lw using \i/10] \x in \tabx
{
\draw[red!\i,line width=\lw] (current point) -- ++ (\x,\y) coordinate (current point);
}
\end{tikzpicture}
\end{document}
我倾向于将数组变量保存在 tex 文件中,而不是单独的文本文件中,但如果这样做有明显的优势,那么它也可能是一种选择。
注意:我知道 pgfplots 和其他高级库,但在这里我宁愿避免使用它们,因为我想保留一个较低级别的控制来添加任意元素,而不是依赖于预构建的绘图命令、符号等的设计。
很高兴能够针对现有问题或手册中的特定部分找到答案;我找到了不少相关参考资料,但无法将所有部分拼凑在一起。
答案1
您需要更多类似以下内容的内容。
您需要将一个简单的列表传递给
\foreach
循环进行解析。您不能在此处传递数组,因为列表只包含一个项目:数组。相反,只需传递一个整数列表,用作访问数组的索引。您需要使用方括号(而不是圆括号)来访问数组的索引元素。
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[x=1cm,y=0.4cm]
\def\tabx{%
{%
0,0.01057775,0.02115551,0.03173326,0.04231101,0.05288877,0.06346652,0.07404427,%
0.08462202,0.09519978,0.1057775,0.1163553,0.126933,0.1375108,0.1480885,0.1586663,0.169244,%
0.1798218,0.1903996,0.2009773,0.2115551,0.2221328,0.2327106,0.2432883,0.2538661,0.2644438,%
0.2750216,0.2855993,0.2961771,0.3067548,0.3173326,0.3279103,0.3384881,0.3490659,0.3596436,%
0.3702214,0.3807991,0.3913769,0.4019546,0.4125324,0.4231101,0.4336879,0.4442656,0.4548434,%
0.4654211,0.4759989,%
0.4865766,0.4971544,0.5077321,0.5183099,0.5288877,0.5394654,0.5500432,0.5606209,0.5711987,0.5817764,%
0.5923542,0.6029319,0.6135097,0.6240874,0.6346652,0.6452429,0.6558207,0.6663984,0.6769762,%
0.6875539,0.6981317,0.7087095,0.7192872,0.729865,0.7404427,0.7510205,0.7615982,0.772176,%
0.7827537,0.7933315,0.8039092,0.814487,0.8250647,0.8356425,0.8462202,0.856798,0.8673757,0.8779535,%
0.8885313,0.899109,0.9096868,0.9202645,0.9308423,0.94142,0.9519978,0.9625755,0.9731533,0.983731,%
0.9943088,1.004887,1.015464,1.026042,1.03662,1.047198%
}%
}
\def\taby{%
{%
0,0.06342392,0.1265925,0.1892512,0.251148,0.3120334,0.3716625,0.4297949,0.4861967,%
0.5406408,0.5929079,0.6427876,0.690079,0.7345917,0.7761465,0.814576,0.8497254,0.8814534,0.909632,%
0.9341479,0.9549022,0.9718116,0.9848078,0.9938385,0.9988673,0.9998741,0.9968548,0.9898214,0.9788024,%
0.9638422,0.9450008,0.9223543,0.8959938,0.8660254,0.8325699,0.7957618,0.7557496,0.7126942,0.666769,%
0.618159,0.5670599,0.5136774,0.4582265,0.4009305,0.3420201,0.2817326,0.2203105,0.1580014,0.09505604,%
0.03172793,-0.03172793,-0.09505604,-0.1580014,-0.2203105,-0.2817326,-0.3420201,-0.4009305,-0.4582265,%
-0.5136774,-0.5670599,-0.618159,-0.666769,-0.7126942,-0.7557496,-0.7957618,-0.8325699,-0.8660254,%
-0.8959938,-0.9223543,-0.9450008,-0.9638422,-0.9788024,-0.9898214,-0.9968548,-0.9998741,-0.9988673,%
-0.9938385,-0.9848078,-0.9718116,-0.9549022,-0.9341479,-0.909632,-0.8814534,-0.8497254,-0.814576,%
-0.7761465,-0.7345917,-0.690079,-0.6427876,-0.5929079,-0.5406408,-0.4861967,-0.4297949,-0.3716625,%
-0.3120334,-0.251148,-0.1892512,-0.1265925,-0.06342392,-0.000000027563173%
}%
}
\coordinate (p O) at (0,0);
\foreach \i [remember=\i as \ilast (initially O), evaluate=\i as \y using {\taby[\i]}, evaluate=\i as \x using {\tabx[\i]}, evaluate=\i as \lw using \i/10] in {0,...,99}
{
\draw [red!\i, line width=\lw] (p \ilast) -- ++(\x,\y) coordinate (p \i);
}
\end{tikzpicture}
\end{document}
但请注意,给予 Ti 是没有意义的钾Z 数据保留到小数点后几位。这只会增加解析时间,而不会提高准确性。
编辑
这是响应有关如何在路径中使用循环的请求的一个扩展示例。
\pdfminorversion=7
% ateb: http://tex.stackexchange.com/a/354419/ addaswyd o gwestiwn baptiste: http://tex.stackexchange.com/q/354406/
\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}[x=1cm,y=0.4cm]
\def\tabx{%
{%
0,0.01057775,0.02115551,0.03173326,0.04231101,0.05288877,0.06346652,0.07404427,%
0.08462202,0.09519978,0.1057775,0.1163553,0.126933,0.1375108,0.1480885,0.1586663,0.169244,%
0.1798218,0.1903996,0.2009773,0.2115551,0.2221328,0.2327106,0.2432883,0.2538661,0.2644438,%
0.2750216,0.2855993,0.2961771,0.3067548,0.3173326,0.3279103,0.3384881,0.3490659,0.3596436,%
0.3702214,0.3807991,0.3913769,0.4019546,0.4125324,0.4231101,0.4336879,0.4442656,0.4548434,%
0.4654211,0.4759989,%
0.4865766,0.4971544,0.5077321,0.5183099,0.5288877,0.5394654,0.5500432,0.5606209,0.5711987,0.5817764,%
0.5923542,0.6029319,0.6135097,0.6240874,0.6346652,0.6452429,0.6558207,0.6663984,0.6769762,%
0.6875539,0.6981317,0.7087095,0.7192872,0.729865,0.7404427,0.7510205,0.7615982,0.772176,%
0.7827537,0.7933315,0.8039092,0.814487,0.8250647,0.8356425,0.8462202,0.856798,0.8673757,0.8779535,%
0.8885313,0.899109,0.9096868,0.9202645,0.9308423,0.94142,0.9519978,0.9625755,0.9731533,0.983731,%
0.9943088,1.004887,1.015464,1.026042,1.03662,1.047198%
}%
}
\def\taby{%
{%
0,0.06342392,0.1265925,0.1892512,0.251148,0.3120334,0.3716625,0.4297949,0.4861967,%
0.5406408,0.5929079,0.6427876,0.690079,0.7345917,0.7761465,0.814576,0.8497254,0.8814534,0.909632,%
0.9341479,0.9549022,0.9718116,0.9848078,0.9938385,0.9988673,0.9998741,0.9968548,0.9898214,0.9788024,%
0.9638422,0.9450008,0.9223543,0.8959938,0.8660254,0.8325699,0.7957618,0.7557496,0.7126942,0.666769,%
0.618159,0.5670599,0.5136774,0.4582265,0.4009305,0.3420201,0.2817326,0.2203105,0.1580014,0.09505604,%
0.03172793,-0.03172793,-0.09505604,-0.1580014,-0.2203105,-0.2817326,-0.3420201,-0.4009305,-0.4582265,%
-0.5136774,-0.5670599,-0.618159,-0.666769,-0.7126942,-0.7557496,-0.7957618,-0.8325699,-0.8660254,%
-0.8959938,-0.9223543,-0.9450008,-0.9638422,-0.9788024,-0.9898214,-0.9968548,-0.9998741,-0.9988673,%
-0.9938385,-0.9848078,-0.9718116,-0.9549022,-0.9341479,-0.909632,-0.8814534,-0.8497254,-0.814576,%
-0.7761465,-0.7345917,-0.690079,-0.6427876,-0.5929079,-0.5406408,-0.4861967,-0.4297949,-0.3716625,%
-0.3120334,-0.251148,-0.1892512,-0.1265925,-0.06342392,-0.000000027563173%
}%
}
\coordinate (p O) at (0,0);
\foreach \i [remember=\i as \ilast (initially O), evaluate=\i as \y using {\taby[\i]}, evaluate=\i as \x using {\tabx[\i]}, evaluate=\i as \lw using \i/10] in {0,...,99}
{
\draw [red!\i, line width=\lw] (p \ilast) -- ++(\x,\y) coordinate (p \i);
}
\draw [green!75!black] (0,-1) \foreach \i [evaluate=\i as \y using {\taby[\i]}, evaluate=\i as \x using {\tabx[\i]}] in {0,...,99} { -- ++(\x,\y) };
\draw (0,-2) coordinate (p O) \foreach \i [remember=\i as \ilast (initially O), evaluate=\i as \y using {\taby[\i]}, evaluate=\i as \x using {\tabx[\i]}, evaluate=\i as \lw using \i/10] in {0,...,99}
{
(p \ilast) edge [blue!\i, line width=\lw] coordinate [pos=1] (p \i) ++(\x,\y)
};
\end{tikzpicture}
\end{document}
红色曲线使用路径循环,每次迭代循环一次,与之前相同。下方的绿色和蓝色曲线使用路径内的循环,每个坐标循环一次。
绿色曲线必须对路径的所有部分使用相同的选项,因此颜色和宽度是静态的。在这种情况下,我们不需要通过命名来记住坐标,因为我们绘制的是连续的路径,并且位于我们要去的任何地方。
蓝色曲线通过使用操作绕过了相同选项限制edge
,该操作会中断当前路径以进行转移。但是,在这种情况下,我们又需要通过命名来记住坐标,因为当edge
完成时,路径会从开始的地方恢复。