2

If I compile the following code I get no error:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\draw [help lines] (-2,-2) grid (2,2);
\draw plot [mark=*, samples at=    {22.5,67.5,112.5,157.5,202.5,247.5,292.5,337.5,22.5}] (\x:1);
\draw[shift={(0 cm, 2*sin(67.5) cm)}] plot [mark=*, samples at={22.5,67.5,112.5,157.5,202.5,247.5,292.5,337.5,22.5}] (\x:1);
\end{tikzpicture}
\end{document}

Notice that I translate the second octagon using the vector (0,2*sin(67.5)) and the compiler seems to interpreter correctly the second component. On the other side, when I try to compile the following:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\draw [help lines] (-2,-2) grid (2,2);
\draw plot [mark=*, samples at={22.5,67.5,112.5,157.5,202.5,247.5,292.5,337.5,22.5}] (\x:1);
\draw[shift={(2*cos(67.5) cm, 0 cm)}] plot [mark=*, samples at={22.5,67.5,112.5,157.5,202.5,247.5,292.5,337.5,22.5}] (\x:1);
\end{tikzpicture}
\end{document}

I get an error message:

! Package pgf Error: No shape named 2*cos(67 is known.
See the pgf package documentation for explanation.
Type H <return> for immediate help.
...
l.11 \draw[shift={(2*cos(67.5) cm, 0 cm)}]
plot [mark=*, samples at={22.5,67...

It seems that it doesn't recognize the vector (2*cos(67.5), 0), in particular the 2*cos(67.5) component... Can someone tell me why?

Thanks in advance.

4

Whenever you have () in a component of a coordinate, you need to put braces around the component, otherwise the parser will see the closing ) as the closing ) of the coordinate. That is, you need ({<x expression with parens>}, {<y expression with parens>})

In addition, it seems it doesn't work to put the cm right after cos(67.5), but you can put it after the 2 instead.

code output

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\draw [help lines] (-2,-2) grid (2,2);
\draw plot [mark=*, samples at={22.5,67.5,112.5,157.5,202.5,247.5,292.5,337.5,22.5}] (\x:1);
\draw[shift={({2cm*cos(67.5)}, 0cm)}] plot [mark=*, samples at={22.5,67.5,112.5,157.5,202.5,247.5,292.5,337.5,22.5}] (\x:1);
\end{tikzpicture}
\end{document}
  • Can I put some labels on the edges of the octagons? Can I color some of the edges using the same \draw plot command? – EM90 Oct 5 '17 at 12:52
  • @EM90 To color individual edges, you need multiple \draw statements I believe. You could use polar coordinates for labels as well, e.g. \node at (35:1.1) {a};, or you do something like \path (22.5:1) -- node[auto] {b} (67.5:1);. (By the way, when asking follow up questions about something that is unrelated to your original question, consider posting a new question instead of using comments like this.) – Torbjørn T. Oct 5 '17 at 14:36

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.