1

I tried to recreate a multi meter and have created the following code:

\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,decorations.markings,backgrounds,decorations.pathreplacing}
\begin{document}

\begin{tikzpicture} \pgfmathsetmacro{\R}{2}
\draw[thick,fill=black] (0,0) circle (0.1); \draw[thick] ($(0,0)+(45:\R cm)$) arc (45:135:\R); \foreach \x/\y [count=\xi] in {45/3,50/2.5,60/2,72/1.5,90/1,115.5/0.5,135/0} { \draw[thick] ($(0,0)+(\x:\R cm)$) -- ($(0,0)+(\x:\R+0.2+0.28Mod(\xi-1,2)$); \node[rotate=\x-90] at ($(0,0)+(\x:\R+0.4+0.3Mod(\xi-1,2)$) {\y}; } \end{tikzpicture} \end{document}

As far as I understand, this must be the correct code, with an extra bracket after Mod.

\node[rotate=\x-90] at ($(0,0)+(\x:\R+0.4+0.3*Mod(\xi-1,2))$) {\y};

But this gives the following error: ! Package tikz Error: + or - expected.

Ingmar
  • 6,690
  • 5
  • 26
  • 47
jochen
  • 11
  • 2
  • 1
    Welcome. What's \R? You've got to hide the ) of the Mod function from the TikZ parser: ($(0,0)+(\x:{\R+0.2+0.28*Mod(\xi-1,2)})$). (Of course, the (0,0)+ is pointless here but that's another topic. You're also mixing coordinates from the canvas (with units) and from the xyz (without units) coordinate systems which I advise against unless it is necessary.) – Qrrbrbirlbel Sep 28 '23 at 21:19
  • 1
    I've just noticed that (\x:{\R+0.2+0.28*Mod(\xi-1,2) actually works because of a quirk in PGFMath so I don't know what your issue is besides not having \R not defined. Once I define \R, your code works for me. Maybe you have an older installation of your TeX distribution or of PGF/TikZ. – Qrrbrbirlbel Sep 28 '23 at 23:50
  • I had problems, to get '(\x:\R+0.4+0.3*Mod(\xi-1,2))' running, but when I accidentally deleted the last bracket, the code was running. I can not understand this and want to know, if this is a bug or a misunderstanding on my side. – jochen Sep 29 '23 at 04:48
  • See the linked answer. When TikZ finds a coordinate it grabs everything until the next ) and then tries to parse this as a coordinate. If you use *Mod(\xi-1,2)) it will find the first ) and the radius of the polar coordinate will be \R+0.2+0.28*Mod(\xi-1,2. PGFMath is forgiving and doesn't care about the missing ) but then TikZ finds the second ) and that doesn't the path syntax and it raises an error. If you use only one ), PGFMath gets the same formula, still forgiving but TikZ can go on. (That's the equivalent to the “code golf” example (0, sqrt(7) in the link.) – Qrrbrbirlbel Sep 29 '23 at 09:25
  • The proper way (and if the function doesn't occur at the end the only way) is to put the whole radius inside {} to protect the occuring ) from the TikZ parser which isn't as smart as when PGFMath parses a formula (and matches the () correctly, of course). – Qrrbrbirlbel Sep 29 '23 at 09:30
  • Thank you Qrrbrbirlbel for this explanation. Especially the hint with the {} brackets. Now I can write my code in a way, that I know it will work as intended. – jochen Oct 04 '23 at 18:49

0 Answers0