14

Prior to Tikz 3.0, the use of \usepackage{fp}\usetikzlibrary{fixedpointarithmetic} would increase the accuracy of calculations.

I haven't used these packages since the update. However, I tried them out today and the results were nonsense.

Why is fixed point arithmetic affecting 3 sides of my box of mass M and under estimating the arc?

Also, the vectors off the box are based on the location of the box. How can the vectors remain unchanged when the box has moved?

I used fixed point arithmetic on calculating angles in the \draw let syntax.

  \draw[-stealth, fixed point arithmetic] let
    \p0 = (O),
    \p1 = (P1),
    \p2 = (P2),
    \n1 = {atan2(\y1 - \y0, \x1 - \x0)},
    \n2 = {atan2(\y2 - \y0, \x2 - \x0)},
    \n3 = {1cm},
    \n4 = {(\n1 + \n2)/2}
  in \pgfextra{\xdef\myn{\n2}} (O) +(\n1:\n3) arc[radius = \n3,
  start angle = \n1, end angle = \n2] node[right, font = \tiny] at (\n4:\n3)
  {$\theta$};

Without fixed point arithmetic, the image is:

enter image description here

With:

enter image description here

Code:

\documentclass[tikz, convert = false]{standalone}%

\usepackage{fp}
\usetikzlibrary{fixedpointarithmetic}
\usetikzlibrary{calc}
\usetikzlibrary{intersections}
\usetikzlibrary{backgrounds}

\begin{document}
\begin{tikzpicture}[line join = round, line cap = round]
  \coordinate (O) at (0, 0);

  \draw (O) -- +(3, 0) coordinate (P1);
  \draw[name path = sline] (O) -- (3, 2) coordinate (P2);

  \draw[-stealth, fixed point arithmetic] let % remove fpa for it work
    \p0 = (O),
    \p1 = (P1),
    \p2 = (P2),
    \n1 = {atan2(\y1 - \y0, \x1 - \x0)},
    \n2 = {atan2(\y2 - \y0, \x2 - \x0)},
    \n3 = {1cm},
    \n4 = {(\n1 + \n2)/2}
  in \pgfextra{\xdef\myn{\n2}} (O) +(\n1:\n3) arc[radius = \n3,
  start angle = \n1, end angle = \n2] node[right, font = \tiny] at (\n4:\n3)
  {$\theta$};

  \path[name path = line1] (1.5, 0) -- +(0, 1.25);
  \path[name path = line2] (2, 0) -- +(0, 1.5);
  \path[name intersections = {of = sline and line1, by = P3}];
  \path[name intersections = {of = sline and line2, by = P4}];

  \draw (P3) -- ($(P3)!.25cm!-90:(O)$) coordinate (P5);
  \draw (P4) -- ($(P4)!.25cm!-90:(O)$) coordinate (P6);
  \draw[name path = boxtop] (P5) -- (P6) node[pos = .5, below, font = \tiny,
  rotate = \myn] {$M$};

  \path[name path = grav] ($(P5)!.75!(P6)$) -- +(0, -1.25);
  \path[name intersections = {of = grav and sline, by = P7}];

  \begin{scope}[on background layer]
    \draw[-latex, blue] (P7) -- ($(P7)!.75cm!-270:(O)$) coordinate (P8)
    node[pos = 1.25, font = \tiny, color = black] {$F_2$};

    \path[name path = perl1] (P8) -- ($(P8)!.75cm!-270:(P7)$);
    \path[name intersections = {of = perl1 and grav, by = P9}];

    \draw[-latex, blue] (P7) -- (P9) node[below, font = \tiny, inner sep = .3,
    color = black] {$Mg$};
    \draw[blue] (P9) -- (P8);

    \path[name path = norm] (P7) -- ($(P7)!.75cm!-90:(O)$);
    \path[name intersections = {of = norm and boxtop, by = P10}];

    \draw[-latex,blue] (P10) -- ($(P10)!.5cm!-90:(P5)$) node[pos = 1.15,
    font = \tiny, rotate = {\myn}, color = black] {$N$};

    \coordinate (P11) at ($(P3)!.5!(P5)$);
    \coordinate (P12) at ($(P4)!.5!(P6)$);

    \draw[-latex, blue] (P12) -- ++(\myn:.5) node[pos = 1.25, font = \tiny,
    rotate = {\myn}, color = black] {$F_f$};
    \draw[-latex, blue] (P11) -- ++({\myn + 180}:.5) node[pos = 1.25,
    font = \tiny, rotate = {\myn}, color = black] {$F_1$};
  \end{scope}
\end{tikzpicture}
\end{document}
dustin
  • 18,617
  • 23
  • 99
  • 204
  • Not inherent to the problem, but are you aware of the angles library? – Claudio Fiandrino Apr 25 '14 at 18:07
  • @ClaudioFiandrino I have never used that library. – dustin Apr 25 '14 at 18:15
  • @dustin: it just makes your job easier ;) – Claudio Fiandrino Apr 25 '14 at 18:20
  • @TorbjørnT. here using fp isn't important, but when I want to have pgf calculate and print the angles (in other problems), using fp improves the printed value significantly in some cases. – dustin Apr 25 '14 at 18:20
  • I unable to duplicate your problem. – A.Ellett Apr 25 '14 at 23:25
  • @A.Ellett are you passing fixed point arithmetic in options of draw for the let syntax? Do you Tikz 3.0? – dustin Apr 26 '14 at 00:34
  • For me it works fine with or without the fp package. I use textlive 2013 and I have installed pgf tikz 3.0.0 over it, TDF friendly in texmf/local. – Tarass Apr 26 '14 at 07:14
  • @Tarass did you read @TorbjornT. comment? You can't just load the packages. You have to pass the option fixed point arithmetic into the \draw[fixed point arithmetic] let If you don't, it will work fine since you didn't use the package. – dustin Apr 26 '14 at 14:17
  • Sorry I'm lost, I tried your example "as is", it works fine. What is the alternative that doesn't work ? – Tarass Apr 26 '14 at 15:00
  • The only différence is with "fixed point arithmetic" M and theta are shifted right, without all is correct. All drawings are correct, not as on your picture. – Tarass Apr 26 '14 at 15:20
  • @Tarass you didn't read the full question if you cant figure out why you cant generate the error. I edited the question so all you have to do now is copy and paste to produce the error. The code snippet before the full code had the error highlighted and you could see what the different was between full code and snippet. Now they are the same. – dustin Apr 26 '14 at 16:56
  • @Tarass m and theta shouldn't shift with that package. That is the point of this question. – dustin Apr 26 '14 at 16:57
  • I understand, but i wonder about the point that on dustin example, not only M and theta are shifted ? Why in my case only M and theta are shifted ? All what is shifted depend of \myn. – Tarass Apr 26 '14 at 16:59
  • 4
    This question appears to be off-topic because it is about a bug that has been reported. – Werner Apr 27 '14 at 15:17
  • @Werner Why does that make it off-topic?! The bug was reported after (and in fact as a result of) this question being posed. The question has been asked and answered. Why close it? It is ridiculous to say that "This question does not fall within the scope of TeX, LaTeX or related typesetting systems as defined in the help center." – ShreevatsaR May 03 '17 at 15:56
  • @ShreevatsaR we use off-topic since the possibility of closing as too localized doesn't exist any more (we used to have that…). Bug reports are usually too localized in time so aren't really helpful questions in the long run… – cgnieder May 03 '17 at 16:21
  • @clemens This is a great question to have around; I don't see any reason to close it. This is less "localized" than most questions on the site. – ShreevatsaR May 03 '17 at 16:35
  • 1
    @ShreevatsaR: The reasoning behind "off-topic" is global to the entire network. Sure, this does fall within the realm of (La)TeX, but it is something that will never(!) require revisiting as a result of the bug fix. Very few people, if anyone, should have a TeX distribution that includes this bug. – Werner May 03 '17 at 16:46
  • @ShreevatsaR: If you find other questions on this site that are more localized than this, then act upon it and suggest voting to close them (and let the community decide). Closures do not degrade the site. Equally, if you feel that closures are not warranted, then cast a vote-to-reopen (and let the community decide). – Werner May 03 '17 at 16:50
  • @Werner Closures do degrade the site: they send a message to visitors that good questions like this are not welcome on the site. (And a confusing message that "this question appears to be off-topic".) The value from closing this is minimal (can you say what it is?) and the harm is not. I have voted to reopen. – ShreevatsaR May 03 '17 at 16:59
  • 1
    @ShreevatsaR: It will never be required since the bug has been fixed. The fact that it has answers (and a high voting score) means it will remain on the site (and not be deleted), so it's value in terms of code, etc. is not lost. We get a whole host of new questions from new users every day, some good, some bad. I doubt there's any proof to your claim "they send a message to visitors that good questions like this one are not welcome." Anyone reading this entire thread would understand that. Do duplicate closures also degrade the site? I think not. – Werner May 03 '17 at 17:05

3 Answers3

11

It is a bug in pgflibraryfixedpointarithmetic.code.tex (tested with TikZ 3.0; file size 13595 bytes) caused by a space at line end in macro definition \pgfmathfpabs@:

\def\pgfmathfpabs@#1{%
        \begingroup%
                \FPabs\pgfmathresult{#1}
        \pgfmath@smuggleone\pgfmathresult%
        \endgroup%
}

The space is after the third line. Corrected version:

\def\pgfmathfpabs@#1{%
        \begingroup%
                \FPabs\pgfmathresult{#1}%
        \pgfmath@smuggleone\pgfmathresult%
        \endgroup%
}

The code for atan2 uses function abs twice. The inserted spaces causes the following elements to the right; the origin is no longer in (0,0), but moved to the right.

The definitions of \pgfmathfpsec@ and \pgfmathfpcosec suffer from the same problems.

Workaround:

\endlinechar=-1
\usetikzlibrary{fixedpointarithmetic}
\endlinechar=13

MWE:

\documentclass[tikz, convert = false]{standalone}%

\usepackage{fp}
\usetikzlibrary{fixedpointarithmetic}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}

  \fill[blue] (0,0) circle[radius=1pt];
  \fill(1,0) circle[radius=.5pt];

  \path[
    fixed point arithmetic
  ] let
    \n1 = {atan2(57pt, 85pt)}
  in;

  \fill[red] (0,0) circle[radius=.5pt];

\end{tikzpicture}
\end{document}

Without fix:

Without fix

With fix:

Fixed

Heiko Oberdiek
  • 271,626
5

Using a scope only for the calculation of atan2 works for me. This scope seems to be necessary, but I do not know why.

\documentclass[tikz, convert = false]{standalone}%

\usepackage{fp}
\usetikzlibrary{fixedpointarithmetic}
\usetikzlibrary{calc}
\usetikzlibrary{intersections}
\usetikzlibrary{backgrounds}

\begin{document}
\begin{tikzpicture}[line join = round, line cap = round]
  \coordinate (O) at (0, 0);

  \draw (O) -- +(3, 0) coordinate (P1);
  \draw[name path = sline] (O) -- (3, 2) coordinate (P2);

  \begin{scope}
    \path[fixed point arithmetic] let 
      \p0 = (O),
      \p1 = (P1),
      \p2 = (P2),
      \n1 = {atan2(\y1 - \y0, \x1 - \x0)},
      \n2 = {atan2(\y2 - \y0, \x2 - \x0)}
    in \pgfextra{\xdef\mym{\n1}\xdef\myn{\n2}}; 
  \end{scope}

  \draw[-stealth] let
    \n3 = {1cm},
    \n4 = {(\mym + \myn)/2} in
    (O) +(\mym:\n3) arc[radius = \n3,
  start angle = \mym, end angle = \myn] node[right, font = \tiny] at (\n4:\n3)
  {$\theta$};

  \path[name path = line1] (1.5, 0) -- +(0, 1.25);
  \path[name path = line2] (2, 0) -- +(0, 1.5);
  \path[name intersections = {of = sline and line1, by = P3}];
  \path[name intersections = {of = sline and line2, by = P4}];

  \draw (P3) -- ($(P3)!.25cm!-90:(O)$) coordinate (P5);
  \draw (P4) -- ($(P4)!.25cm!-90:(O)$) coordinate (P6);
  \draw[name path = boxtop] (P5) -- (P6) node[pos = .5, below, font = \tiny,
  rotate = \myn] {$M$};

  \path[name path = grav] ($(P5)!.75!(P6)$) -- +(0, -1.25);
  \path[name intersections = {of = grav and sline, by = P7}];

  \begin{scope}[on background layer]
    \draw[-latex, blue] (P7) -- ($(P7)!.75cm!-270:(O)$) coordinate (P8)
    node[pos = 1.25, font = \tiny, color = black] {$F_2$};

    \path[name path = perl1] (P8) -- ($(P8)!.75cm!-270:(P7)$);
    \path[name intersections = {of = perl1 and grav, by = P9}];

    \draw[-latex, blue] (P7) -- (P9) node[below, font = \tiny, inner sep = .3,
    color = black] {$Mg$};
    \draw[blue] (P9) -- (P8);

    \path[name path = norm] (P7) -- ($(P7)!.75cm!-90:(O)$);
    \path[name intersections = {of = norm and boxtop, by = P10}];

    \draw[-latex,blue] (P10) -- ($(P10)!.5cm!-90:(P5)$) node[pos = 1.15,
    font = \tiny, rotate = {\myn}, color = black] {$N$};

    \coordinate (P11) at ($(P3)!.5!(P5)$);
    \coordinate (P12) at ($(P4)!.5!(P6)$);

    \draw[-latex, blue] (P12) -- ++(\myn:.5) node[pos = 1.25, font = \tiny,
    rotate = {\myn}, color = black] {$F_f$};
    \draw[-latex, blue] (P11) -- ++({\myn + 180}:.5) node[pos = 1.25,
    font = \tiny, rotate = {\myn}, color = black] {$F_1$};
  \end{scope}
\end{tikzpicture}
\end{document}

enter image description here

In this example the fixed point arithmetic option could also be an argument of the scope or the picture, but in every case I have to use this extra scope for the calculation of atan2.

\begin{tikzpicture}[line join = round, line cap = round,
  fixed point arithmetic
]
...
  \begin{scope}
    \path let 
      \p0 = (O),
      \p1 = (P1),
      \p2 = (P2),
      \n1 = {atan2(\y1 - \y0, \x1 - \x0)},
      \n2 = {atan2(\y2 - \y0, \x2 - \x0)}
    in \pgfextra{\xdef\mym{\n1}\xdef\myn{\n2}}; 
  \end{scope}

  \draw[-stealth] let
    \n3 = {1cm},
    \n4 = {(\mym + \myn)/2} in
    (O) +(\mym:\n3) arc[radius = \n3,
  start angle = \mym, end angle = \myn] node[right, font = \tiny] at (\n4:\n3)
  {$\theta$};
...

The result is the same as above.

esdd
  • 85,675
  • In a post maybe 6 months or more ago, I learned not to put fixed point as the picture option. It can cause issue in some cases with nodes. – dustin Apr 27 '14 at 02:48
  • I reread yesterday the pgftikz manual and page 360 one can read "The best way to use this key is as an argument to a scope or picture." I remember it at sleeping time but this morning esdd had the same idea. – Tarass Apr 27 '14 at 06:41
3

Not the answer but I don't think you need fp here. The result is pretty accurate with PGF itself too.

\documentclass[tikz]{standalone}
\usetikzlibrary{angles,quotes,calc}
\begin{document}
\begin{tikzpicture}
% Angle label
\draw (0,0) coordinate (O) -- +(3, 0) coordinate (P1) (O)-- +(3, 2) coordinate (P2)
pic ["$\theta$",draw,->,angle eccentricity=1.2,angle radius=1cm] {angle = P1--O--P2};
% Angle value
\path let \p1=($(P2)-(O)$),\n1={atan2(\y1,\x1)} in \pgfextra{\xdef\myn{\n1}};
% Mass placement
\node[minimum width=1cm,draw,anchor=south,rotate=\myn] (m) at ($(O)!0.7!(P2)$) {M};
% Ortho-Forces
\foreach \x[count=\xi from 0] in {F_f,N,F_1,F_2}{
\draw[-latex,draw=blue] (m.\xi*90) --++(\xi*90+\myn:5mm) 
node[,inner sep=2pt,anchor=90*(\xi+3),font=\tiny] {$\x$};
}
% Gravity
\draw[-latex,draw=blue] (m.south) --++(0,{-1/cos(\myn)*5mm}) node[font=\tiny,left] (mg) {$mg$};
% Tangential component
\draw[blue] (mg.east) -- ++(\myn:{tan(\myn)*5mm});
\end{tikzpicture}
\end{document}

enter image description here

percusse
  • 157,807
  • I know fp isn't needed here. I general only use it when I print an angle. I decided to try it though since TikZ was updated. – dustin Apr 27 '14 at 15:13