2

This code produces two coil windings. If I specify them both explicitely, it works fine. If I wrap it in a foreach loop, they don't connect as they should. I tried varying the line width and the preaction line width. That does not affect the mis-alignment. Can anybody see the problem?

\documentclass{article}
\usepackage{tikz}
\begin{document}
    \tikzset{coil/.style={preaction={draw,line width=1mm,white},thick}}%
    \newlength{\coilradiusa}
    \newlength{\coilradiusb}
    \newlength{\coilradiusc}
    \newlength{\coilradiusd}
    \begin{tikzpicture}
        \pgfmathsetlength{\coilradiusa}{11.593 mm}
        \pgfmathsetlength{\coilradiusb}{3.42 mm}
        \pgfmathsetlength{\coilradiusc}{8.407 mm}
        \pgfmathsetlength{\coilradiusd}{2.48 mm}
        \newcommand{\iminusone}{0}
        \node[inner sep=0,outer sep=0,anchor=center] (0) at (0,0) {};
        \foreach \x in {1,...,2}{%
            \pgfmathsetmacro{\iminusone}{\x-1}
            \draw[coil] (\iminusone)
                arc[x radius=-\coilradiusb,y radius=\coilradiusa,start angle=0,delta angle=-180]
                arc[x radius=\coilradiusd,y radius=-\coilradiusc,start angle=0,delta angle=-180]
                node[inner sep=0,outer sep=0,anchor=center] (\x) {};
        }
    \end{tikzpicture}
    \begin{tikzpicture}
        \pgfmathsetlength{\coilradiusa}{11.593 mm}
        \pgfmathsetlength{\coilradiusb}{3.42 mm}
        \pgfmathsetlength{\coilradiusc}{8.407 mm}
        \pgfmathsetlength{\coilradiusd}{2.48 mm}
        \node[inner sep=0,outer sep=0,anchor=center] (0) at (0,0) {};
        \draw[coil] (0)
            arc[x radius=-\coilradiusb,y radius=\coilradiusa,start angle=0,delta angle=-180]
            arc[x radius=\coilradiusd,y radius=-\coilradiusc,start angle=0,delta angle=-180]
            node[inner sep=0,outer sep=0,anchor=center] (1) {};
        \draw[coil] (1)
            arc[x radius=-\coilradiusb,y radius=\coilradiusa,start angle=0,delta angle=-180]
            arc[x radius=\coilradiusd,y radius=-\coilradiusc,start angle=0,delta angle=-180]
            node[inner sep=0,outer sep=0,anchor=center] (2) {};
    \end{tikzpicture}
\end{document}

enter image description here

  • Use \pgfmathtruncatemacro instead of \pgfmathsetmacro. – Maarten Dhondt Jan 9 '16 at 16:12
  • Perfect! Still don't know what goes wrong in the for loop, but thanks anyway – mauricev Jan 9 '16 at 16:21
  • @mauricev \pgfmathtruncatemacro rounds the number, or better, it removes the decimals. That's probably why you had that little misalignment, since Maarten's solution helped you. – Alenanno Jan 9 '16 at 16:58
  • 1
    @MaartenDhondt Will you answer? – cfr Jan 10 '16 at 2:00
  • 2
    The expression (1.0) represents a point on the border of the node (1) such that the line between (1.0) and (1.center) is parallel to x-axis. (Similarly (1.north) and (1.90) usually coincide.) – Symbol 1 Jan 10 '16 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.