Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

While attempting to draw some geometry diagrams in TikZ, I was having problems drawing an arc to mark off an angle. The arc kept falling too short and would not intersect both rays of the angle. (It wasn't a PDF zooming issue, the arc falls short at all zoom levels). I also noticed that slightly perturbing one of the coordinates fixes the arc. Here is some code and output showing what I mean:

\documentclass[draft]{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

% A = (-4,1.999)
\begin{tikzpicture}
    \coordinate [label=below:{$B$}] (B) at (-6,0);
    \coordinate [label=below:{$C$}] (C) at (0,0);
    \coordinate [label=above:{$A$}] (A) at (-4,1.999);

    \draw let \p1=($(B)$),\p2=($(A)$) in
        (-1,0) arc (atan2(\x1,\y1):atan2(\x2,\y2):1);

    \draw (B) -- (C) -- (A);
\end{tikzpicture}

% A = (-4,2)
\begin{tikzpicture}
    \coordinate [label=below:{$B$}] (B) at (-6,0);
    \coordinate [label=below:{$C$}] (C) at (0,0);
    \coordinate [label=above:{$A$}] (A) at (-4,2.0);

    \draw let \p1=($(B)$),\p2=($(A)$) in
        (-1,0) arc (atan2(\x1,\y1):atan2(\x2,\y2):1);

    \draw (B) -- (C) -- (A);
\end{tikzpicture}

% A = (-4,2.001)
\begin{tikzpicture}
    \coordinate [label=below:{$B$}] (B) at (-6,0);
    \coordinate [label=below:{$C$}] (C) at (0,0);
    \coordinate [label=above:{$A$}] (A) at (-4,2.001);

    \draw let \p1=($(B)$),\p2=($(A)$) in
        (-1,0) arc (atan2(\x1,\y1):atan2(\x2,\y2):1);

    \draw (B) -- (C) -- (A);
\end{tikzpicture}
\end{document}

enter image description here

I was wondering why this is happening only for A=(-4,2). Perhaps I am misusing atan2 somehow?

share|improve this question
    
Welcome to LaTeX! Welcome to TeX.sx! All look the same on my computer. Can it be a PDF viewer problem? –  percusse Oct 31 '12 at 10:44
    
@percusse: I can reproduce the problem. Are you using the CVS version of PGF, by any chance? –  Jake Oct 31 '12 at 10:44
    
@Jake Ah might be, I forgot to revert back to the stable version. –  percusse Oct 31 '12 at 10:46

1 Answer 1

up vote 5 down vote accepted

There is no problem with your usage but the developers of TikZ updated the CVS version of the math library. The culprit seems to be in the division algorithm so if I copy the relevant part of the CVS version into the stable release (v2.10) it works as expected.

Since a small perturbation is enough for the workaround, this code below is only for demo purposes.

\documentclass[draft]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\makeatletter

\def\pgfmathdivide@@{%
    \let\pgfmath@next=\relax%
    \ifdim\pgfmath@small@number pt<\pgfmath@x%
        \ifdim\pgfmath@small@number pt<\pgfmath@y%
            \ifdim\pgfmath@y>\pgfmath@x%
                \ifpgfmath@divide@period%
                    \expandafter\def\expandafter\pgfmathresult\expandafter{\pgfmathresult.}%
                    \pgfmath@divide@periodfalse%
                \fi%
                \pgfmathdivide@dimenbyten\pgfmath@y%
                \ifdim\pgfmath@y>\pgfmath@x%
                    \expandafter\def\expandafter\pgfmathresult\expandafter{\pgfmathresult0}%
                \fi%
            \else%
                \c@pgfmath@counta=\pgfmath@x%
                \c@pgfmath@countb=\pgfmath@y%
                \divide\c@pgfmath@counta by\c@pgfmath@countb%
                \pgfmath@ya=\c@pgfmath@counta\pgfmath@y%
                \advance\pgfmath@x by-\pgfmath@ya%
                \def\pgfmath@next{%
                    \toks0=\expandafter{\pgfmathresult}%
                    \edef\pgfmathresult{\the\toks0 \the\c@pgfmath@counta}%
                }%
                \ifpgfmath@divide@period
                \else
                            \ifnum\c@pgfmath@counta>9 %
                        \expandafter\pgfmathdivide@advance@last@digit\pgfmathresult CCCCC\@@
                        \advance\c@pgfmath@counta by-10 %
                        \ifnum\c@pgfmath@counta=0
                            \let\pgfmath@next=\relax
                        \fi
                    \fi
                \fi
                \pgfmath@next
            \fi%
            \let\pgfmath@next=\pgfmathdivide@@%
        \fi%
    \fi%
    \pgfmath@next%
}

% advances the last digit found in the number. Any missing digits are
% supposed to be filled with 'C'.
\def\pgfmathdivide@advance@last@digit#1.#2#3#4#5#6#7\@@{%
    \pgfmath@ya=\pgfmathresult pt %
    \if#2C%
        \pgfmath@xa=1pt %
    \else
        \if#3C%
            \pgfmath@xa=0.1pt %
        \else
            \if#4C%
                \pgfmath@xa=0.01pt %
            \else
                \if#5C%
                    \pgfmath@xa=0.001pt %
                \else
                    \if#6C%
                        \pgfmath@xa=0.0001pt %
                    \else
                        \pgfmath@xa=0.00001pt %
                    \fi
                \fi
            \fi
        \fi
    \fi
    \advance\pgfmath@ya by\pgfmath@xa
    \edef\pgfmathresult{\pgfmath@tonumber@notrailingzero\pgfmath@ya}%
}%
{
\catcode`\p=12
\catcode`\t=12
\gdef\Pgf@geT@NO@TRAILING@ZERO#1.#2pt{%
    #1.%
    \ifnum#2=0 \else #2\fi
}
}
\def\pgfmath@tonumber@notrailingzero#1{\expandafter\Pgf@geT@NO@TRAILING@ZERO\the#1}


\makeatother
\begin{document}


\begin{tikzpicture}
\foreach \x[count=\xi] in {1.999,2.0,2.001}{
\coordinate (a\xi) at (-4,\x);
\node at (0,\xi) {\pgfmathparse{-4/\x}\pgfmathresult};
\node at (2,\xi) {\pgfmathparse{atan(-4/\x)}\pgfmathresult};
\node at (4,\xi) {\pgfmathparse{atan2(-4,\x)}\pgfmathresult};
}
\end{tikzpicture}

\begin{tikzpicture}
    \coordinate [label=below:{$B$}] (B) at (-6,0);
    \coordinate [label=below:{$C$}] (C) at (0,0);
    \coordinate [label=above:{$A$}] (A) at (-4,1.999);

    \draw let \p1=($(B)$),\p2=($(A)$) in
        (-1,0) arc (atan2(\x1,\y1):atan2(\x2,\y2):1);

    \draw (B) -- (C) -- (A);
\end{tikzpicture}

% A = (-4,2)
\begin{tikzpicture}
    \coordinate [label=below:{$B$}] (B) at (-6,0);
    \coordinate [label=below:{$C$}] (C) at (0,0);
    \coordinate [label=above:{$A$}] (A) at (-4,2.0);

    \draw let \p1=($(B)$),\p2=($(A)$) in
        (-1,0) arc (atan2(\x1,\y1):atan2(\x2,\y2):1);

    \draw (B) -- (C) -- (A);
\end{tikzpicture}

% A = (-4,2.001)
\begin{tikzpicture}
    \coordinate [label=below:{$B$}] (B) at (-6,0);
    \coordinate [label=below:{$C$}] (C) at (0,0);
    \coordinate [label=above:{$A$}] (A) at (-4,2.001);

    \draw let \p1=($(B)$),\p2=($(A)$) in
        (-1,0) arc (atan2(\x1,\y1):atan2(\x2,\y2):1);

    \draw (B) -- (C) -- (A);
\end{tikzpicture}
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.