10

I had created some picture which I plan to use it in different places of the coordinate plane. But I made it in a form of TeX macro \cone{...}.

How can I create such picture in a form of tikz \node[cone]?

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{calc}
\begin{document}
    \begin{tikzpicture}
            \def\cone(#1){
            \draw (#1) +(135:0.5) -- +(-45:0.5);
            \draw (#1) +(45:0.5) -- +(-135:0.5);
            \fill (#1) circle (0.03);
            \draw let \p1=(#1) in (\x1,{\y1+.35cm}) ellipse (0.352 and 0.05);
            \draw let \p1=(#1) in (\x1,{\y1-0.35cm}) ellipse (0.352 and 0.05);
            }

            \coordinate (A) at (0,0);
            \coordinate (B) at (1,1);

            \cone(A)
            \cone(B)

    \end{tikzpicture}
\end{document}
sergiokapone
  • 5,578
  • 1
  • 16
  • 39
  • 1
    your cones remain me on "small picture" \pic. it can be used as \pic at (x,y) {cone}. – Zarko Oct 11 '17 at 23:16
  • @Zarko Can you write your answer? It, seems, also good solution. – sergiokapone Oct 11 '17 at 23:31
  • Related questions: https://tex.stackexchange.com/questions/126161/how-can-i-draw-a-tikz-element-multiple-times-against-a-shaded-background/151772#151772, https://tex.stackexchange.com/questions/18400/can-a-shape-be-composed-out-of-subshapes-in-tikz/289625#289625, https://tex.stackexchange.com/questions/172484/how-to-create-something-like-a-function-procedure-macros-etc-in-tikz/172821#172821 – Ignasi Oct 18 '17 at 07:06

1 Answers1

15

enter image description here

above images is drawn with \pic

\documentclass[tikz, border=1cm]{standalone}

\begin{document}
    \begin{tikzpicture}[
cone/.pic = {\draw (-0.5,-0.5) -- ++ (1, 1);
             \draw (-0.5, 0.5) -- ++ (1,-1);
             \fill (0,0) circle (0.03);
             \draw (0, 0.5) ellipse (0.5 and 0.05);
             \draw (0,-0.5) ellipse (0.5 and 0.05);
            }
                    ]
\pic at (0,0) {cone};
\pic at (2,1) {cone};
    \end{tikzpicture}
\end{document}

since context of use "cone" is unknown, i didn't define any anchor of cones.

addendum:

in case when you like to draw lines between cones, than you need to define anchors of cones:

\documentclass[tikz, border=1cm]{standalone}

\begin{document}
    \begin{tikzpicture}[
cone/.pic = {\draw (-0.5, 0.5) coordinate (-nw) -- ++ (1,-1) coordinate (-se);
             \draw (-0.5,-0.5) coordinate (-sw) -- ++ (1, 1) coordinate (-ne);
             \fill (0,0)  coordinate (-center) circle (0.03);
             \draw (0, 0.5) ellipse (0.5 and 0.05);
             \draw (0,-0.5) ellipse (0.5 and 0.05);
             \coordinate (-top)     at (0, 0.55);
             \coordinate (-north)   at (0, 0.5);
             \coordinate (-south)   at (0,-0.5);
             \coordinate (-bottom)  at (0,-0.55);
            }
                    ]
\pic (A) at (0,0) {cone};
\pic (B) at (2,1) {cone};
\draw[red] (A-center) -- (B-bottom);
\draw[blue] (A-north) -- + (0,2) -| (B-north);
\draw[green] (A-ne) |- (B-nw);
\pic[rotate=90] (B) at (4,0) {cone};
    \end{tikzpicture}
\end{document}

enter image description here

Andrew Swann
  • 95,762
Zarko
  • 296,517