2

I have a regular polygon using TikZ like this:

\begin{tikzpicture}
    \node[regular polygon, regular polygon sides=5, shape border rotate=-18, fill=blue!20, minimum size=5cm, thick, draw] (Funfeck) {};
    \coordinate (P1) at (Funfeck.corner 1);
    \coordinate (P2) at (Funfeck.corner 2);
    \coordinate (P3) at (Funfeck.corner 3);
    \coordinate (P4) at (Funfeck.corner 4);
    \coordinate (P5) at (Funfeck.corner 5);
    ...
\end{tikzpicture}

It looks like this:Pentagon

In another place, I want to have a triangle with the points P5, P1 and P2.

It should look like this:The Triangle, so the part in the document in which these polygons are, should look like this:The document The Code would be something like this:

\draw (P5) -- (P1) -- (P2) -- (P5) -- cycle;

But, as this code is at an entirely different place, this code is in a different tikzpicture and I can't access the coordinates.

Now, I have four solution proposals:

First: (My favourite) I get these coordinates without drawing the pentagon. The advantage would be, that I needn't to draw the figure somewhere else if I have the same problem another time.
Something like this:

\coordinate (P1) at (\node[regular polygon, regular polygon sides=5, shape border rotate=-18, minimum size=5cm].corner 1);
\coordinate (P2) at (\node[regular polygon, regular polygon sides=5, shape border rotate=-18, minimum size=5cm].corner 1);
\coordinate (P5) at (\node[regular polygon, regular polygon sides=5, shape border rotate=-18, minimum size=5cm].corner 1);
\draw (P5) -- (P1) -- (P2) -- (P5) -- cycle;

Second: Another possibility would be to get the coordinates out of the other tikzpicture.
I mean something like this:

\coordinate (P1) at (tikzpicture[0].coordinate.P1);
\coordinate (P2) at (tikzpicture[0].coordinate.P2);
\coordinate (P5) at (tikzpicture[0].coordinate.P5);
\draw (P5) -- (P1) -- (P2) -- (P5) -- cycle;

Third: Another solution, I have fought about was to draw the polygon, get the coordinates and then I remove the polygon. In fact, I can make the node without draw and without fill=blue!20, but then, there is still a whitespace.
I mean something like this:

\node[regular polygon, regular polygon sides=5, shape border rotate=-18, minimum size=5cm, draw] (Funfeck) {};
\coordinate (P1) at (Funfeck.corner 1);
\coordinate (P2) at (Funfeck.corner 2);
\coordinate (P3) at (Funfeck.corner 3);
\coordinate (P4) at (Funfeck.corner 4);
\coordinate (P5) at (Funfeck.corner 5);
\remove {Funfeck}
\draw (P5) -- (P1) -- (P2) -- (P5) -- cycle;

Fourth: If nothing of the other methods is possible, the worst solution is to print the Coordinates and insert them3 manually. But I haven't even found a way to do this:
My Pseudo-Code would be:

\begin{tikzpicture}
    \node[regular polygon, regular polygon sides=5, shape border rotate=-18, fill=blue!20, minimum size=5cm, thick, draw] (Funfeck) {};
    \coordinate (P1) at (Funfeck.corner 1);
    \coordinate (P2) at (Funfeck.corner 2);
    \coordinate (P3) at (Funfeck.corner 3);
    \print("P1, x: "+P1.X+" .P1, y: "+P1.Y);
    \print("P2, x: "+P2.X+" .P2, y: "+P2.Y);
    \print("P3, x: "+P3.X+" .P3, y: "+P3.Y);
    ...
\end{tikzpicture}

...

\coordinate (P1) at (manually inserted, manually inserted);
\coordinate (P2) at (manually inserted, manually inserted);
\coordinate (P3) at (manually inserted, manually inserted);

Can anybody give me a solution how to solve this problem?
Thank you very much :).

  • Welcome to TeX.SX! Please help us (and also you) and add a minimal working example (MWE), that illustrates your problem... compilable code, starting with \documentclass and ending with \end{document}. - Does access a coordinate in a nested \tikzpicture help? – Bobyandbob Oct 30 '17 at 11:48
  • It is very unclear what you are asking. Can you write a document or draw a picture with the expected result or some approximation of it? It is especially unclear what you mean by referring to P1,2,3 from another picture. Do you want to overlay the triangle on the original picture? – Bordaigorl Oct 30 '17 at 11:52
  • @Bordaigorl I have added three pictures to show how the pentagon looks, the triangle should look, and the full document should look :) – Korne127 Oct 30 '17 at 11:59
  • Do you want to draw on the same picture or on a different picture that has the points "in the same place"? If the former see section 17.13.1 of the manual -- particular, ` remember picture` and overlay. If the latter, just draw the same points -- for regular polygons just use polar coordinates. – Andrew Oct 30 '17 at 12:01
2

You can use the overlay option for the node, then it will not be considered for the calculation of the bounding box.

Alternatively, use polar coordinates to calculate the coordinates.

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}
\node[
   regular polygon,
   regular polygon sides=5,
   shape border rotate=-18,
   minimum size=5cm, 
   overlay % overlay option means the node isn't included in the calculation of the bounding box
] (P) {};

\draw (P.corner 5) -- (P.corner 1) -- (P.corner 2) -- cycle;

% or do as you did, and make new coordinates
%\coordinate (P1) at (Funfeck.corner 1);
%\coordinate (P2) at (Funfeck.corner 2);
%\coordinate (P3) at (Funfeck.corner 3);
%\coordinate (P4) at (Funfeck.corner 4);
%\coordinate (P5) at (Funfeck.corner 5);
%\draw (P5) -- (P1) -- (P2) -- cycle;
\end{tikzpicture}


% example using polar coordinates
\begin{tikzpicture}
\foreach \N in {1,...,5}
  \coordinate (P\N) at ({360/5*(\N-1)+90-18}:2.5cm);

\draw (P1) -- (P2) -- (P5) -- cycle;
\end{tikzpicture}
\end{document}

The second version can be generalized by defining a macro. With inspiration from Ryan Reich's answer to How to create a command with key values?, one possible implementation is shown in the code below.

Used alone, \myregpolygon will define coordinates P1 through P5, for a regular five sided polygon with "radius" of 1cm, centered on the point (0,0), and the first point at an angle of zero. Each of those parameters can modified with key-value pairs in an optional argument to the macro:

\myregpolygon[% 
  radius=3cm, % radius of circumscribed circle
  origin={(3,2)}, % center point
  start angle=90, % angle of first coordinate
  name=Q, % prefix for coordinate names
  sides=4 % sides for the polygon
]

One doesn't have to redefine all at once, of course.

\documentclass[border=5mm]{standalone}
\usepackage{tikz}

\pgfkeys{
  regpoly/.is family,
  /regpoly,
  default/.style={
     radius=1cm,
     start angle=0,
     origin={(0,0)},
     sides={5},
     name={P}
  },
  radius/.estore in=\RPradius,
  start angle/.estore in=\RPstartangle,
  origin/.estore in=\RPorigin,
  sides/.estore in=\RPsides,
  name/.estore in=\RPname,
}

\newcommand\myregpolygon[1][]{
\pgfkeys{/regpoly,default, #1}
\foreach \N in {1,...,\RPsides}
   \coordinate [shift=\RPorigin] (\RPname\N) at ({360/\RPsides*(\N-1)+\RPstartangle}:\RPradius);
}
\begin{document}
\begin{tikzpicture}

\myregpolygon[start angle=72,radius=2.5cm]

\draw (P1) -- (P2) -- (P5) -- cycle;
\end{tikzpicture}
\end{document}
  • @Korne127 I added a second option as well. – Torbjørn T. Oct 30 '17 at 12:04
  • I have seen it. Which solution would you prefer? – Korne127 Oct 30 '17 at 12:06
  • I think, I'll use the second solution as it's 'cleaner' and it doesn't contain a hidden node, but calculates the coordinates. – Korne127 Oct 30 '17 at 12:11
  • @TorbjørnT. I was preparing a similar solution with your second (but would give options on a newcommand named \myregpolygon for radius, starting angle, number of corners etc). Since it is just an extension of yours, I will not upload... I just suggesting to edit and do it this way because it solves more problems and we can keep this question as a guide for future similar requests. Korne127 the second is better for me too... (+1 by the way) – koleygr Oct 30 '17 at 12:19
  • 1
    @koleygr There has probably been similar questions before as well, but anyway, I added one possible implementation. – Torbjørn T. Oct 30 '17 at 12:50

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.