2

Dears,

I am facing the following problem. Given a set of points, I have to draw the geometric shape of these points, however, I have to do this programmatically. Take a look at this MWE.

\documentclass{beamer}

\usepackage{tikz}

\begin{document}
    \begin{frame}
        \begin{figure}
            \begin{tikzpicture}
                % nodes
                \coordinate (a) at (0, 0);
                \coordinate (b) at (1, 0);
                \coordinate (c) at (1, 1);
                \coordinate (d) at (0, 1);

                % block
                \def\blockA{a, b, c, d}

                % draw cycle
                % - static way
                \draw[fill=gray!50, draw = white] (a) -- (b) -- (c) -- (d) -- cycle;

                % - desired way
                \foreach \v [count=\i] in \blockA {
                    % ...
                }
            \end{tikzpicture}
        \end{figure}
    \end{frame}
\end{document}

I need to find proper ways to implement the % - desired way. I would like to know if anyone has ever worked with this. If anyone has any questions or suggestions, please feel free to let me know.

Thank you and best regards.

2
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Commented Jan 21, 2022 at 19:03
  • 1
    Here you don't need the \blockA macro, since you can just write \foreach \v [count=\i] in {a, b, c, d} then use \v inside parentheses to create node names. But it needs a bit more of work to draw the line.
    – SebGlav
    Commented Jan 21, 2022 at 19:03

1 Answer 1

2

The simplest way I can think of is

\draw [fill=red] (a)
\foreach \v [count=\i] in \blockA {
    \ifnum\i>1
        -- (\v)
    \fi
}
-- cycle;

which yields:

enter image description here

since the red fill was done on top of the gray!50.

Code:

\documentclass{beamer}
\usepackage{tikz}

\begin{document}
    \begin{frame}
        \begin{figure}
            \begin{tikzpicture}
                % nodes
                \coordinate (a) at (0, 0);
                \coordinate (b) at (1, 0);
                \coordinate (c) at (1, 1);
                \coordinate (d) at (0, 1);

                % block
                \def\blockA{a, b, c, d}

                % draw cycle
                % - static way
                \draw[fill=gray!50, draw = white] (a) -- (b) -- (c) -- (d) -- cycle;

                % - desired way
                \draw [fill=red] (a)
                \foreach \v [count=\i] in \blockA {
                    \ifnum\i>1
                        -- (\v)
                    \fi
                }
                -- cycle;
            \end{tikzpicture}
        \end{figure}
    \end{frame}
\end{document}
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .