8

After reading this post, I was playing around to make a command to create a TikZ command with two inputs like this:

\documentclass[10pt,a4paper]{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\centering

\newcommand{\mitsubishi}[2]{
% #1 = radius
% #2 = position x,y
\pgfmathparse{2.966/7}
\pgfmathparse{#1*\pgfmathresult}

\filldraw (#2) circle(#1 cm); % plate

\foreach \angle in {0,120,240}{
\filldraw[white,rotate=\angle] (#2)--++(60:\pgfmathresult)--++(120:\pgfmathresult)--++(-120:\pgfmathresult)--cycle;
}
}

\begin{tikzpicture}
\mitsubishi{2}{0,0}
\end{tikzpicture}
\end{document}

but I have problems with the command \pgfmathresult inside the \filldraw command.

Why?

Azoun
  • 2,317
  • Welcome to tex.sx! A tip: you can use backticks ``` to mark your inline code as I did in my edit. Moreover, note that you don't have to sign with your name since it automatically appears in the lower right corner of your post. – Hendrik Vogt Feb 01 '11 at 08:54

3 Answers3

8

The \pgfmathresult is overwritten by the drawing commands. You have to save it away:

\pgfmathparse{2.966/7}
\let\myresult\pgfmathresult
\pgfmathparse{#1*\myresult}
\let\myresult\pgfmathresult

\filldraw (#2) circle(#1 cm); % plate

\foreach \angle in {0,120,240}{
\filldraw[white,rotate=\angle] (#2)--++(60:\myresult)--++(120:\myresult)--++(-120:\myresult)--cycle;
}
}
Martin Scharrer
  • 262,582
1

You also use \pgfmathsetmacro:

\documentclass[10pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\newcommand{\mitsubishi}[2]{
% #1 = radius
% #2 = position x,y
\pgfmathsetmacro{\mymathresult}{#1*2.966/7}   

\filldraw (#2) circle(#1 cm); 
\foreach \angle in {0,120,240}{
\filldraw[white,rotate=\angle] (#2)--++(60:\mymathresult)--++(120:\mymathresult)--++(-120:\mymathresult)--cycle;
}
}

\begin{tikzpicture}
\mitsubishi{4}{0,0}
\end{tikzpicture}

\end{document} 
Hendrik Vogt
  • 37,935
Alain Matthes
  • 95,075
1

There was a problem if I put a different center point, let say (1,1), because the rotation is not done around that point but around (0,0) so i suggest this ultimate solution:

\documentclass[10pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\newcommand{\mitsubishi}[2]{
% #1 = radius
% #2 = position x,y
\pgfmathsetmacro{\mymathresult}{#1*2.966/7}   

\filldraw (#2) circle(#1 cm); 
\foreach \angle in {0,120,240}{
\filldraw[white] (#2)--++(60+\angle:\mymathresult)
                     --++(120+\angle:\mymathresult)
                     --++(240+\angle:\mymathresult)
                     --cycle;
}
}

\begin{tikzpicture}
\draw[help lines,step=.5cm](0,0)grid(4,4);
\draw[<->] (5,0)--(0,0)--(0,5);

\foreach \x in {1,2,3,4}{
         \foreach \y in {1,2,3,4}{
         \mitsubishi{.25}{\x,\y}
         }
}

\end{tikzpicture}

\end{document}

Thanks to all for let me achiving this result!!!

Bye

Azoun
  • 2,317
  • Azoun: Could you please accept one of these answers, so that this post is removed from the "unanswered question" section. Thanks! – Martin Scharrer Mar 08 '11 at 11:29