我正在尝试创建一个函数,该函数可生成具有连续曲率的边界的斑点。只要我将各个坐标传递给该函数,一切就会顺利进行。
\documentclass{article}
\usepackage{tikz}
\newcommand{\smoothplot}[3]{%
% Smooth curve through 3 coordinates
\path [draw=red, line width = 5, smooth cycle]
plot coordinates {(#1) (#2) (#3)};
}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (5,5);
\coordinate (C) at (2,-2);
\smoothplot{A}{B}{C};
\end{tikzpicture}
\end{document}
但是如果我想通过传递坐标列表来获得任意数量的参数,我就无法让它工作。
\documentclass{article}
\usepackage{tikz}
\newcommand{\smoothplot}[1]{%
% Smooth curve through coordinates given in a list
\path [draw=red, line width = 5, smooth cycle]
plot coordinates {\foreach \mycoord in #1 {(\mycord)}};
}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (5,5);
\coordinate (C) at (2,-2);
\smoothplot{A, B, C};
\end{tikzpicture}
\end{document}
它给了我一个错误:Package tikz Error: Cannot parse this coordinate.
有人能告诉我如何从列表中提取坐标以便操作plot
可以使用它们吗?
答案1
这可以通过 来完成samples at
。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\newcommand{\smoothplot}[1]{
\draw[red,line width=5] plot[smooth cycle,variable=\x,samples at={#1}] (\x);}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (5,5);
\coordinate (C) at (2,-2);
%\draw[red,line width=5] plot[smooth cycle,variable=\x,samples at={A,B,C}] (\x);
\smoothplot{A,B,C}
\end{tikzpicture}
\end{document}