Graphics, Figures & TablestikZ | Drawing Circles by new Commands

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
kasa
Posts: 5
Joined: Wed Dec 05, 2012 10:25 pm

tikZ | Drawing Circles by new Commands

Post by kasa »

What is the problem with this source? The result is far to be nice.

Code: Select all

\documentclass{article}
\usepackage{tikz}

\newcommand{\Andr}[4]{  
% #1 n
% #2 angle
% #3 node size
% #4 \fill or \draw
\begin{tikzpicture}[rotate=#2,scale=2]
 \pgfmathsetmacro{\n}{3*#1 - 1}
 \pgfmathsetmacro{\m}{2*#1 - 1}
 \foreach \i in {1,...,\n}
 { \path (360/\n*\i:1cm) node (X\i) {};
    #4 (X\i) circle (#3pt);
 }
  \foreach \i in {1,...,\n}
 {
  \foreach \j in {#1,...,\m}
  {   \pgfmathsetmacro{\p}{1+ mod(\i+\j-1,\n)}
      \draw (X\i) -- (X\p);
   }
  }  
\end{tikzpicture}
}
  
\begin{document}
   \Andr{4}{0}{3}{\draw} 
   \Andr{6}{0}{2}{\draw}
   \Andr{2}{0}{3}{\fill}
   \Andr{3}{0}{2}{\fill}
\end{document}
Last edited by localghost on Wed Dec 05, 2012 10:36 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

tikZ | Drawing Circles by new Commands

Post by cgnieder »

Hi kasa,

welcome to the LaTeX community!

Your problem description is not very clear but I guess I know what your problem is. If I'm right it is due to the fact that node(X\i) {} not only stores a coordinate but actually creates a non-empty node, i.e., not a single point! If you change this to coordinate (X\i) the output looks like it could be what you want.

Regards
site moderator & package author
kasa
Posts: 5
Joined: Wed Dec 05, 2012 10:25 pm

tikZ | Drawing Circles by new Commands

Post by kasa »

You are right. Thanks.
But the following example works nice even with node. Why?
(The edges link nodes, and not the centers of the circles, as in the case of coordinates. And in my first example the nodes and edges are distorsioned, the edges link different points on and in the circles. Why?)

Code: Select all

\documentclass{article}
\usepackage{tikz}

\newcommand{\Andr}[4]{  
% #1 n
% #2 angle
% #3 node size
% #4 \fill or \draw
\begin{tikzpicture}[rotate=#2, scale=2]
 \pgfmathsetmacro{\n}{3*#1 - 1}
 \pgfmathsetmacro{\m}{2*#1 - 1}
 \foreach \i in {1,...,\n}
 {
    \path (360/\n*\i:1cm)  node (X\i) {};
    #4 (X\i) circle (#3pt);
 }
  \foreach \i in {1,...,\n}
 {
   \foreach \j in {#1,...,\m}
   { 
          \draw (X\i) -- (X\j);   
   }
}  
\end{tikzpicture}
}
  
\begin{document}
\Andr{2}{90}{2}{\draw}\qquad\Andr{4}{90}{2}{\draw}

\Andr{6}{90}{2}{\draw}\qquad\Andr{3}{90}{2}{\fill}
\end{document}

User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Re: tikZ | Drawing Circles by new Commands

Post by cgnieder »

Huh, that's strange... judging the code I can see no apparent reasons why the two macros give different outcomes. Maybe someone else can help here. (I'll try to investigate anyway when I find the time.)

Regards
site moderator & package author
kasa
Posts: 5
Joined: Wed Dec 05, 2012 10:25 pm

tikZ | Drawing Circles by new Commands

Post by kasa »

Zap! I found the solution!! If I put
\pgfmathtruncatemacro{\p}{1+mod(\i+\j-1,\n)}
instead of
\pgfmathsetmacro{\p}{1+ mod(\i+\j-1,\n)}
the outcome will be nice.
Post Reply