[Tex/LaTex] how to draw a chain of gears


I'd like to draw a chain of gears, like this:
enter image description here

I'm using the macro provided by Alain Matthes in Creating gears in TikZ for drawing a single gear with some modification to obtain the "double gear" effect (i.e. an inside/outside gear).

My actual code is this:


\foreach \i in {1,...,#1} {%
  [rotate=(\i-1)*360/#1]  (0:#2)  arc (0:#4:#2) {[rounded corners=1.5pt]
             -- (#4+#5:#3)  arc (#4+#5:360/#1-#5:#3)} --  (360/#1:#2)

    \matrix[column sep=-0.1cm,draw=none,fill=none,ampersand replacement=\&] {
    % First gear %
    \node[circle,minimum size=40mm,fill=gray,shade] at (0,0){};
    \node[circle,minimum size=30mm,fill=white] at (0,0){};
    \draw[thick,rotate=10,fill=gray,shade] \gear{18}{2}{2.2}{10}{2};
    \draw[thick,rotate=22,fill=white] \gear{18}{1.5}{1.7}{6}{1};\&
    % Second gear %
    \node[circle,minimum size=40mm,fill=gray,shade] at (0,0){};
    \node[circle,minimum size=30mm,fill=white] at (0,0){};
    \draw[thick,rotate=10,fill=gray,shade] \gear{18}{2}{2.2}{10}{2};
    \draw[thick,rotate=22,fill=white] \gear{18}{1.5}{1.7}{6}{1};\\

which produces:enter image description here

to move the second gear left/right i've put the gears in a \matrix so that I just need to copy the code from the first gear in the second cell of the matrix and specify a column sep such that the gears are at the desired distance. This method however doesn't work anymore when you add a third gear that needs a different column sep.

to move the second gear up/down so to obtain the effect of the first image I tried with xshift and yshift options in \draw (that is how i got the first image) but it's a lot of work and requires many recompilations.

So my question is:
Is there a better way to move a gear (where a gear consist in two \draw and two \node commands, as shown in the code) to the left-right-up-down with respect of the other without having to manually positioning it with xshiftand yshift?

A more general approach

To make the question more general, let's just consider the case where every gear is only made by one gear, without the inner gear, background, etc. the tikzpicture in the above code become:

    \matrix[column sep=-0.1cm,draw=none,fill=none,ampersand replacement=\&] {
    % First gear %
    \draw[thick] \gear{18}{2}{2.2}{10}{2};\&
    % Second gear %
    \draw[thick] \gear{18}{2}{2.2}{10}{2};\\

enter image description here
How to chain two or more gears shifting their position like in the first image?

Best Answer

Here is a starting point and just one of many ideas how to input the data.

The next gear style takes its arguments in the form of

[<opt>:<text>]<abs angle>:<next gear’s radius>


  • :<text> and
  • [<opt>:<text>] are optional,
  • <abs angle> denotes the absolute angle/direction of the next gear and
  • <next gear’s radius> specifies the next gear’s radius.

The turtle library is only used for its forward style and the \tikz@lib@turtle@dir macro. To be honest, this could have been coded without the library, but oh well …


Well, hear is a new gear (\ngear[<options>](<point>)) and a try on an output.

The combination of gears involves a little bit of math as number of teeth, the width of these teeth and the rotation are dependent of the previous gear and its rotation.

You can hijack the \qrr@tikz@do@nextgear@ and insert needed calculations for the next gear on your own.

(You can even re-define \ngear to act as an front-end to one of the already defined \gear macros on TeX.sx.)


    outer radius/.initial=1cm,
    outer height/.initial=5cm,
    outer width/.initial=.5cm,
    outer number/.initial=10,
    outer rotate/.initial=0,
    inner radius/.initial=,
    inner rotate/.initial=0,
    inner height/.initial=.2cm,
    inner width/.initial=.2cm,
    inner number/.initial=18,
    height/.style={/gear/outer height={#1},/gear/inner height={#1}},
    width/.style={outer width={#1},inner width={#1}},
%   outer outer radius=.4pt, rounded corners?
%   inner outer radius=.2pt,
%   outer inner radius=.4pt,
%   inner inner radius=.2pt,
    /tikz/every gear/.style={draw,fill=none,even odd rule,rounded corners=.5pt},
%   /tikz/every outer gear/.style={top color=blue, bottom color=green},
%   /tikz/every inner gear/.style={draw=blue,fill=white},
    \geargetvalue{outer radius}\qrr@gear@outerR
    \geargetvalue{inner radius}\qrr@gear@innerR
    \geargetvalue{outer number}\qrr@gear@outerN
    \geargetvalue{inner number}\qrr@gear@innerN
    \geargetvalue{outer height}\qrr@gear@outerH
    \geargetvalue{inner height}\qrr@gear@innerH
    \geargetvalue{outer width}\qrr@gear@outerW
    \geargetvalue{inner width}\qrr@gear@innerW
    \geargetvalue{outer rotate}\qrr@gear@outerRot
    \geargetvalue{inner rotate}\qrr@gear@innerRot
    \pgfmathsetmacro\qrr@gear@ioArc{180/(\qrr@gear@outerN) - \qrr@gear@ooArc}%
    \pgfmathsetmacro\qrr@gear@iiArc{180/(\qrr@gear@innerN) - \qrr@gear@oiArc}%
    \path[every gear/.try, every outer gear/.try] ({\qrr@gear@outerRot}:{\qrr@gear@outerR+\qrr@gear@outerH})
        \foreach \@tooth in {0,...,\qrr@gear@outerN} {
            arc[radius={\qrr@gear@outerR+\qrr@gear@outerH}, start angle=\qrr@gear@sa, delta angle=\qrr@gear@ooArc]
            -- (\qrr@gear@sa+\qrr@gear@ooArc:{\qrr@gear@outerR})
            arc[radius={\qrr@gear@outerR}, start angle=\qrr@gear@sa+\qrr@gear@ooArc, delta angle=2*\qrr@gear@ioArc]
            -- (\qrr@gear@sa+\qrr@gear@ooArc+2*\qrr@gear@ioArc:{\qrr@gear@outerR+\qrr@gear@outerH})
            arc[radius={\qrr@gear@outerR+\qrr@gear@outerH}, start angle=\qrr@gear@sa+\qrr@gear@ooArc+2*\qrr@gear@ioArc, delta angle=\qrr@gear@ooArc]
%       ; % un-comment this and the next line to have two paths
%   \path[every gear/.try, every inner gear]
    \foreach \@tooth in {0,...,\qrr@gear@innerN} {
        arc[radius={\qrr@gear@innerR-\qrr@gear@innerH}, start angle=\qrr@gear@sa, delta angle=\qrr@gear@oiArc]
        -- (\qrr@gear@sa+\qrr@gear@oiArc:{\qrr@gear@innerR})
        arc[radius={\qrr@gear@innerR}, start angle=\qrr@gear@sa+\qrr@gear@oiArc, delta angle=2*\qrr@gear@iiArc]
        -- (\qrr@gear@sa+\qrr@gear@oiArc+2*\qrr@gear@iiArc:{\qrr@gear@innerR-\qrr@gear@innerH})
        arc[radius={\qrr@gear@innerR-\qrr@gear@innerH}, start angle=\qrr@gear@sa+\qrr@gear@oiArc+2*\qrr@gear@iiArc, delta angle=\qrr@gear@oiArc]
    \pgfmathsetmacro\qrr@tikz@gear@distance{\qrr@tikz@gear@current@radius+(#3)+\pgfkeysvalueof{/gear/outer height}}%
    \pgfkeysalso{/tikz/turtle/forward/.expanded=\qrr@tikz@gear@distance pt}
    \pgfkeysalso{/tikz/gear={#1, outer radius=\qrr@tikz@gear@current@radius pt}{\pgf@gear@@x,\pgf@gear@@y}}
    gear/.style 2 args={/tikz/insert path={\pgfextra{\ngear[#1](#2)}}},
    turtle/next gear/.code=\qrr@tikz@do@nextgear#1\@qrr@tikz@do@nextgear

\begin{tikzpicture}[show background grid,/gear/height=.3cm,every gear/.append style=thick]
    \draw[dash pattern={on \pgflinewidth off 2\pgflinewidth},thin] (0,0) [turtle={
        next gear/.list={
            {[outer rotate=-90,outer width=.18cm,outer number=10]90:2cm},
            {[outer width=.49cm,outer number=14,outer rotate=0]30:3cm},
            {[outer width=.2cm,outer rotate=18]120:2cm}


enter image description here