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?

  • 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 1 '11 at 8:54
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;
}
}
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: Could you please accept one of these answers, so that this post is removed from the "unanswered question" section. Thanks! – Martin Scharrer Mar 8 '11 at 11:29
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} 
  • Ops i made a mistake, see the following answer... – Azoun Feb 1 '11 at 0:38
  • I fixed the indentation in your code. OK? – Hendrik Vogt Feb 1 '11 at 8:56
  • @Hendrik OK !. I don't know exactly the rules for the indentation, perhaps there is something special and important to read about "how write answer with TeX code". – Alain Matthes Feb 1 '11 at 12:47
  • If you indent lines by 4 spaces, then they're marked as a code sample. You can also highlight the code and click the "code" button (with "101010" on it). – Hendrik Vogt Feb 1 '11 at 12:49

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.