[Tex/LaTex] Macros/encapsulation in TIKZ

tikz-pgftikz-styles

I'm considering starting to use TIKZ, and trying to figure out if it will do what I want, and how.

I have a large document with many pictures of Bezier curves. For each of these, I'd like to draw the curve itself, four dots at its control points, and three lines joining these four points. I'm currently drawing these using Powerpoint macros. They look like this:

enter image description here

I want to "encapsulate" this thing in a function or macro, to make it easy to instantiate, and ensure consistency between different pictures. I imagine that the inputs to the function would be the coordinates of the four points and three colors (one for the curve, one for the points, and one for the lines).

I read about the "tikzstyle" facility. It sounds good, because I can place the "style" definition externally, and re-use it in all my pictures, which gives me consistency and easy global changes. But I'm not sure how the "input arguments" (the coordinates) would work.

Is tikzstyle the right approach, or is there some better way (either using TIKZ or some other similar tool)?

Best Answer

This is one possible solution. Here a macro called mybezier is defined within a tikzpicture environment as displayed below that takes 4 arguments: P_0, P_a, P_b, P_1. Then call \mybezier{p0}{pa}{pb}{p1} to draw.

\newcommand\mybezier[4]{
    \begin{tikzpicture}
    \draw [green]          (#1)--(#2)-- (#3)--(#4);
    \draw[very thick,blue] (#1).. controls (#2) and (#3) .. (#4);
    \draw [fill=red,draw=black]    (#1)node[left] {$P_0$} circle (2pt);
    \draw [fill=red,draw=black]    (#2)node[left] {$P_a$} circle (2pt);
    \draw [fill=red,draw=black]    (#3)node[right]{$P_b$} circle (2pt);
    \draw [fill=red,draw=black]    (#4)node[right]{$P_1$} circle (2pt);
    \end{tikzpicture}
}

enter image description here

Code

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\newcommand\mybezier[4]{
    \begin{tikzpicture}
    \draw [green]          (#1)--(#2)-- (#3)--(#4);
    \draw[very thick,blue] (#1).. controls (#2) and (#3) .. (#4);
    \draw [fill=red,draw=black]    (#1)node[left] {$P_0$} circle (2pt);
    \draw [fill=red,draw=black]    (#2)node[left] {$P_a$} circle (2pt);
    \draw [fill=red,draw=black]    (#3)node[right]{$P_b$} circle (2pt);
    \draw [fill=red,draw=black]    (#4)node[right]{$P_1$} circle (2pt);
    \end{tikzpicture}
}
\begin{document}
\mybezier{1,0}{2,3}{5,4}{6,1}
\end{document}