# Draw n lines in a loop around a circle

I am trying to draw \nb lines from (0,0). All equally spaced.

I keep getting the error "Missing number treated as zero". I have found a lot of similar errors online; these are solved by enclosing the maths in {} curly brackets.

I have tried this but it still does not work. Does anybody know what my error is?

\begin{figure}[h]
\centering
\begin{tikzpicture}
\newcommand\nb{5}%Number of bars
\newcommand\tta{2*pi/\nb}

\foreach \i in {1,...,\nb} {
\newcommand\ctta{{cos(deg(\i*\tta))}}
\newcommand\stta{{sin(deg(\i*\tta))}}
\newcommand\ttta{{tan(deg(\i*\tta))}}

\newcommand\ltpcx{\ir*\ctta}
\newcommand\ltpcy{\ir*\stta}
\draw[] (0,0) -- (\ltpcx,\ltpcy);
}
\end{tikzpicture}
\caption{General structure of a Data Bank in a MIDAS event.}
\label{fig:BV_structure}
\end{figure}

• Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. Sep 9 '21 at 17:13

Instead of calculating the points using sin and cos how about using the (<ang>:<rad>) syntax of TikZ:

\documentclass[tikz,border=3.14]{standalone}

\begin{document}
\begin{tikzpicture}
\newcommand\nb{5}%Number of bars
\foreach \i in {1,...,\nb}
{
\draw (0,0) -- ({360*\i/\nb}:\ir);
}
\end{tikzpicture}
\end{document}


Adding the small "T"-parts without any other manual calculations:

\documentclass[tikz,border=3.14]{standalone}

\begin{document}
\begin{tikzpicture}
\newcommand\nb{5}%Number of bars
\foreach \i in {1,...,\nb}
{
\pgfmathsetmacro\ang{360*\i/\nb}
% get the centre point of the line (0,0)--(\ang:\ir) as (tmp)
\draw (0,0) -- coordinate(tmp) (\ang:\ir)
% from that centre, draw perpendicular for 0.25 (+(<coord>) syntax)
(tmp) -- +(\ang + 90:.25);
}
\end{tikzpicture}
\end{document}


• The problem in my question was just a MWE. Other than this I need to draw a line perpendicular to each of this lines (centered at the top of the line, and of arbitrary length). It is difficult to explain, it will look like a set of rotating T's. That is why I need the coordinates of the points (I think). Sep 9 '21 at 17:25
• @DanielDuque see the edit. With TikZ you don't need to calculate much if you don't want to... Sep 9 '21 at 18:00
• @DanielDuque For future reference, feel free to ask a complete question instead of just a small part and then ask the rest in the answer comments. A sketch of the expected output is also very appreciated to fully understand your needs. Sep 9 '21 at 18:10

You can use \pgfmathsetmacro to define your variables:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\ir}{2}
\pgfmathsetmacro{\nb}{5}
\pgfmathsetmacro{\tta}{2*pi/\nb}

\foreach \i in {1,...,\nb} {
\pgfmathsetmacro{\ctta}{cos(deg(\i*\tta))}
\pgfmathsetmacro{\stta}{sin(deg(\i*\tta))}
\pgfmathsetmacro{\ltpcx}{\ir*\ctta}
\pgfmathsetmacro{\ltpcy}{\ir*\stta}
\draw (0,0) -- (\ltpcx,\ltpcy);
}
\end{tikzpicture}
\end{document}


EDIT For your reference, there are a couple of questions and answers that explain quite nicely the difference between pgfmathsetmacro and newcommand for use as variable in tikz:

Difference between \newcommand and \pgfmathsetmacro used in TikZ \coordinate

What is the preferred way of defining a TikZ constant?

They are not directly related to your question though.

• The result of \pgfmathsetmacro\<macro>{<number-not-formula>} is pretty much the same as \newcommand\<macro>{<number-not-formula>} except that \pgfmathsetmacro takes much longer for this simple task (however for 2*pi/\nb this is better). Sep 9 '21 at 17:55
• Meaning your comment applies only to the initial definition of \ir and \nb in this example? Sep 9 '21 at 17:57
• Yes, only for \ir and \nb. But the \ltpcx and \ltpcy are not really necessary, you could as well just use \draw (0,0) -- (\ir*\ctta, \ir*\stta); if those points aren't used elsewhere. Sep 9 '21 at 18:03

A short code with pstricks and multido:

\documentclass{article}
\usepackage[svgnames]{xcolor}%
\usepackage{pst-eucl, multido}

\begin{document}

\psset{unit=1.5cm, linewidth=1pt, linecolor=SteelBlue, tbarsize=1}
\begin{pspicture}(-3,-3)(3,3)
\multido{\I = 0+72}{5}{\psline{-|}(0,0)(3;\I)}
\end{pspicture}

\end{document}


• \SpecialCoor is no longer needed. It is already enabled since version x.x.x.x (I forgot). Sep 9 '21 at 22:42
• @MakeMeSmarterEveryDay: I wasn't sure, and didn't have time to check. It makes the code still shorter. Thanks for the confirmation! Sep 10 '21 at 8:10