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?

  • @percusse thanks for fixing the author reference, completely missed that! – Nico Boni Apr 3 '13 at 12:14
  • I could see a solution with the turtle library (or a fork of it) where you specify the radii and the macros/libraries/keys calculate the needed distance ((r_1 + r_2)/2). Inner gears need more calculations and stuff or can be added later. What you draw at these points is another exercise (can be simple circles or any funny gear stuff). With a \matrix you hardly can access positions arranged in such an un-matrix-like pattern. Could you add the input one wants to specify? – Qrrbrbirlbel Apr 9 '13 at 3:57

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

  • Next step: Either transfer the \gear macro into an insert path path (or a node) or distance ourselves (or myself?) from the the one-path approach. – Qrrbrbirlbel Apr 9 '13 at 20:30
  • very nice, I'm now messing up with your code :) Could you specify what you meant in your previous comment about adding the input? do you mean the positions of the other gears? – Nico Boni Apr 9 '13 at 21:50
  • @dcmst I mean, how would you like to input the date where the gears lie? Do you already have a list of positions and radii? Do you have radii and angles, are those angles absolute or relative? Do you have more than one branch, more than the one inner gear? Do you want to provide a list, and TikZ builds the picture (like in the current answer) or do you rather want to input the connections verbose (e.g. Gear A with radius r_1 is connected with that already defined gear). — I don’t think this really needs to be one one path. The gears aren't really connected anyway. – Qrrbrbirlbel Apr 9 '13 at 23:21
  • i don't have a list of positions etc. I just copied a picture (not made LaTeX I think) using manual positioning with x-yshift etc. I can post the code fort the first picture, but I don't really care about a specific image, I'd like to draw many different chains whitout having to do all the work I did for the first image. Also I don't care about the specifics of the gears, but it would be nice if the could be different for different gears in the same image. Finally, I'd like the verbose version. – Nico Boni Apr 10 '13 at 6:54
  • As a side question, do you think it's impossibile to reuse the \gear command for building a chain? – Nico Boni Apr 10 '13 at 6:56

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.