1

I define some variable names in TikZ. When I want to draw lines between them, it can be done. But when I try to fill the drawn shape, it does not happen.

For instance, I use this code:

\begin{figure}
        \centering
        \begin{tikzpicture}
            \tikzstyle{every node}=[draw, fill=white, shape=circle, inner sep=3pt];
            \node (h1) at (1,2) {};
            \node (h2) at (2,1) {};
            \node (h3) at (3,2) {};
            \node (b1) at (2,5) {};
            \node (b2) at (-1,-1) {};
            \node (b3) at (5,-1) {};
            \draw[fill=gray] (1,2)--(2,1)--(3,2)--(1,2);
            \draw[thick, blue] (h1)--(h2)--(h3)--(h1);  
            \draw[thick, blue] (b1)--(b2)--(b3)--(b1);
            \draw[dashed] (b1) -- (h1) -- (b2);
            \draw[dashed] (b2) -- (h2) -- (b3);
            \draw[dashed] (b3) -- (h3) -- (b1);
        \end{tikzpicture}
        \caption{Some caption}
    \end{figure}

which outputs this picture: [1]

But if I use the defined variables h1, h2, h3 instead, then the gray area won't appear.

The code:

\begin{figure}
        \centering
        \begin{tikzpicture}
            \tikzstyle{every node}=[draw, fill=white, shape=circle, inner sep=3pt];
            \node (h1) at (1,2) {};
            \node (h2) at (2,1) {};
            \node (h3) at (3,2) {};
            \node (b1) at (2,5) {};
            \node (b2) at (-1,-1) {};
            \node (b3) at (5,-1) {};
            \draw[fill=gray] (h1)--(h2)--(h3)--(h1);
            \draw[thick, blue] (h1)--(h2)--(h3)--(h1);  
            \draw[thick, blue] (b1)--(b2)--(b3)--(b1);
            \draw[dashed] (b1) -- (h1) -- (b2);
            \draw[dashed] (b2) -- (h2) -- (b3);
            \draw[dashed] (b3) -- (h3) -- (b1);
        \end{tikzpicture}
        \caption{Some caption}
    \end{figure}

The output:

enter image description here

What is the proper way to do this?

2

One more example:

\documentclass[convert={outfile=\jobname.png}]{standalone}
\usepackage{tikz}
\begin{document}
 \begin{tikzpicture}  
   \tikzstyle{every node}=[draw, fill=white, shape=circle, inner sep=3pt];

   \foreach \point [count=\i] in {(1,2), (2,1), (3,2), (2,5),(-1,-1),(5,-1)} {
     \node[coordinate] (h\i) at \point { };
   }
   \draw[fill=gray] (h1) -- (h2) -- (h3) -- cycle;

   \foreach \i in {1,...,6} {
      \node (p\i) at (h\i) {\i};
   } 
   \draw[thick, blue] (p1)--(p2)--(p3)--(p1);
   \draw[thick, blue] (p4)--(p5)--(p6)--(p4);
   \draw[dashed] (p4) -- (p1) -- (p5)
                           (p5) -- (p2) -- (p6)
                           (p6) -- (p3) -- (p4);   
    \end{tikzpicture}
\end{document}

Output:

enter image description here

1
    \begin{tikzpicture}
        \tikzstyle{every node}=[draw, fill=white, shape=circle, inner sep=3pt];
        \node (h1) at (1,2) {};
        \node (h2) at (2,1) {};
        \node (h3) at (3,2) {};
        \node (b1) at (2,5) {};
        \node (b2) at (-1,-1) {};
        \node (b3) at (5,-1) {};
        \draw[fill=gray] (h1.center)--(h2.center)--(h3.center)--cycle; % added .center ...
        \draw[thick, blue] (h1)--(h2)--(h3)--(h1);
        \draw[thick, blue] (b1)--(b2)--(b3)--(b1);
        \draw[dashed] (b1) -- (h1) -- (b2);
        \draw[dashed] (b2) -- (h2) -- (b3);
        \draw[dashed] (b3) -- (h3) -- (b1);
    \end{tikzpicture}

enter image description here

Difference is that in your first case you determine coordinates, in the second nodes where lines are drawn from node border to node border and consequently lines are not connected (what is condition to fill enclosed area).

Addendum: in the case, that you like to have circles on front of the inner triangle, than before it only add \scoped[on background layer], i.e.:

    \scoped[on background layer]
        \draw[fill=gray] (h1.center)--(h2.center)--(h3.center)--cycle;

or

    \scoped[on background layer]
        \fill[gray] (h1.center)--(h2.center)--(h3.center)--cycle;

Of course, in preamble you should load backgrounds library too.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.