4

I've the following MWE of a gear :

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,shadows,decorations.markings}

% #1 number of teeths
% #2 radius intern
% #3 radius extern
% #4 angle from start to end of the first arc
% #5 angle to decale the second arc from the first
% #6 internal key of the gear node
% #7 text to put inside the gear
\newcommand{\gear}[7]{
  node (#6) {#7}
  (0:#2)
  \foreach \i [evaluate=\i as \n using {\i-1)*360/#1}] in {1,...,#1}{
    arc (\n:\n+#4:#2) {[rounded corners=1.5pt] -- (\n+#4+#5:#3)
    arc (\n+#4+#5:\n+360/#1-#5:#3)} --  (\n+360/#1:#2)
  }
}

\begin{document}

% Define the layers to draw the diagram
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

% Define block styles used later
\tikzstyle{engine} = [fill=red!20, text centered,drop shadow]

\begin{tikzpicture}

\draw[engine] \gear{8}{2}{2.4}{10}{2}{a}{Gear A};
%\draw[engine] (a.east)+(8,0) \gear{8}{2}{2.4}{10}{2}{b}{Gear B};
\begin{pgfonlayer}{background}
  \path (a.north west)+(-4,4) node (x) {};
  \path (a.south east)+(10,-4) node (y) {};
  \path[fill=yellow!10, rounded corners, draw=black!70, dashed] (x) rectangle (y);                       
\end{pgfonlayer}

\end{tikzpicture}
\end{document}

It produces the following output as expected.

output

Now, I want one more gear, say Gear B, to the right of Gear A. For which I've tried uncommenting the following line in the above example:

\draw[engine] (a.east)+(8,0) \gear{8}{2}{2.4}{10}{2}{b}{Gear B};

But unfortunately It's producing something like this:

wrong output

I've tried with several other ways to get it done, but nothing worked. The gear items are superimposed over each other. Is there any way to position them absolutely ?

Here is an online version of the above example which you can edit and play with.

  • The problem is that the \gear command uses absolute coordinates. Try \draw[engine, blue] ... to see it. – Rmano Apr 16 '16 at 9:07
  • 1
    look at this answer: tex.stackexchange.com/a/58791/5355 – rpapa Apr 16 '16 at 9:31
4

Instead of:

\draw[engine] (a.east)+(8,0) \gear{8}{2}{2.4}{10}{2}{b}{Gear B};

see if this modification of above code is acceptable to you:

\draw[engine,xshift=60mm] \gear{8}{2}{2.4}{10}{2}{b}{Gear B};

Edit: Alternative for above is use of scope (as suggested Peter Grill):

\begin{scope}[shift={($(a.east)+(5,0)$)}]
\draw[engine]  \gear{8}{2}{2.4}{10}{2}{b}{Gear B};
\end{scope}

In this case you need to add library calc for calculation of shift macro

enter image description here

I think that instead of \newcommand for drawing gear the small picture pic would be better choice.

  • 1
    Can you add the scope option so I can delete my answer? – Peter Grill Apr 16 '16 at 4:48
0

Another solution (if you do not have pics because you are still with tikzpgf 2.10) you can change the definition of \gear to use relative coordinates (still need calc added to tikz packages):

\newcommand{\gear}[7]{
  node (#6) {#7} 
  ($(#6)+(0:#2)$)
  \foreach \i [evaluate=\i as \n using {\i-1)*360/#1}] in {1,...,#1}{
    arc (\n:\n+#4:#2) {[rounded corners=1.5pt] -- ($(#6)+ (\n+#4+#5:#3)$)
    arc (\n+#4+#5:\n+360/#1-#5:#3)} --  ($(#6)+(\n+360/#1:#2)$)
  }
}

(the code in overleaf looks funny because it assumes that ($...$) is math mode...)

Result in overleaf with just changing \gear

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.