3

I'm trying to create a function that generates blobs with boundaries that have continuous curvature. As long as I pass individual coordinates to the function, everything works out well.

\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}

But if I want to have an arbitrary number of arguments by passing in a list of coordinates, I can't make it work.

\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}

It give me an error: Package tikz Error: Cannot parse this coordinate.

Can anyone show me how to extract the coordinates from the list in such a way that the plot operation can use them?

1 Answers1

2

This can be done with 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}

enter image description here

  • This answer is easier than your first answer -- but your first answer introduced me to the \typeout macro, which will become an important part of my troubleshooting toolkit. Thanks for both answers! – Carl Sorensen Feb 06 '19 at 01:07
  • @CarlSorensen Yes, I keep forgetting to remove the \typeouts before uploading the answer. (Another tool of this type is \show, but this interrupts the compilation.) –  Feb 06 '19 at 01:16