4

I was typesetting a triangle with , trying to automate the process as much as possible, and noticed that there is some calculation mistakes when using \tkzCalcLength and \tkzGetLength AND different values for \tkzInit[xmin=_,xmax=_,ymin=_,ymax=_] it's easier if I just show the issue:

\documentclass[letterpaper,12pt]{book}

\usepackage{tkz-euclide}
    \usetikzlibrary{shapes,backgrounds,calc,arrows.meta,fit,positioning,intersections}
    \usetkzobj{all}

\tikzset{ptgrs/.style={thick,transform shape,scale=\tikzscale}}

\usepackage{environ}

\makeatletter
\newsavebox{\measure@tikzpicture}
\NewEnviron{scaletikzpicturetowidth}[1]{%
  \def\tikz@width{#1}%
  \def\tikzscale{1}\begin{lrbox}{\measure@tikzpicture}%
  \BODY
  \end{lrbox}%
  \pgfmathparse{#1/\wd\measure@tikzpicture}%
  \edef\tikzscale{\pgfmathresult}%
  \BODY
}
\makeatother

\begin{document}

\begin{center}\begin{scaletikzpicturetowidth}{0.66\textwidth}\begin{tikzpicture}[ptgrs]
\tkzInit[xmin=-0.5,xmax=35,ymin=-2,ymax=15] \tkzClip %\tkzGrid
\def\AX{12}
\def\angXAC{25.5}
\def\angBXC{38.25}
\tkzDefPoint(0,0){A}
\tkzDefPoint(\AX,0){X}
\path[name path=b] (A) -- (\angXAC:40cm); 
\path[name path=l] (A) -- (X) --+ (\angBXC:30cm);
\draw [name intersections={of=b and l, by={F,C}}];
\coordinate (B) at ($(A)!(C)!(X)$);
\tkzDrawPolygon(A,B,C)
\draw (C) -- (X);
%\tkzMarkAngle[size=2.7,](X,A,C)
%   \tkzLabelAngle[pos=2,transform shape=false](X,A,C){$\alpha$}
%\tkzMarkAngle[size=6,](A,C,X)
%   \tkzLabelAngle[pos=5,transform shape=false](A,C,X){$\beta$}
%\tkzMarkAngle[size=1.4,](C,X,A)
%   \tkzLabelAngle[pos=0.8,transform shape=false](C,X,A){$\delta$}
%\tkzMarkAngle[size=2.7,](B,X,C)
%   \tkzLabelAngle[pos=2,transform shape=false](B,X,C){$\gamma$}
%\tkzMarkRightAngle[size=1](C,B,X)
\draw[transform shape=false,thin,decorate,decoration={brace,amplitude=7pt}](X)--(A)
    node [midway,yshift=-12pt] {$\AX$};
\tkzCalcLength[cm](B,C)\tkzGetLength{BC} \FPround\BC\BC{3}
\draw[transform shape=false,thin,decorate,decoration={brace,amplitude=7pt}](C)--(B)
    node [midway,xshift=32pt] {$\BC$};
\draw[transform shape=false,thin,decorate,decoration={brace,amplitude=7pt}](B)--(X)
    node [midway,yshift=-12pt] {$A$};
\draw[blue](current bounding box.south west)rectangle(current bounding box.north east);
\end{tikzpicture}\end{scaletikzpicturetowidth}\end{center}

\begin{center}\begin{scaletikzpicturetowidth}{0.66\textwidth}\begin{tikzpicture}[ptgrs]
\tkzInit[xmin=-0.5,xmax=40,ymin=-2,ymax=15] \tkzClip %\tkzGrid
\def\AX{12}
\def\angXAC{25.5}
\def\angBXC{38.25}
\tkzDefPoint(0,0){A}
\tkzDefPoint(\AX,0){X}
\path[name path=b] (A) -- (\angXAC:40cm); 
\path[name path=l] (A) -- (X) --+ (\angBXC:30cm);
\draw [name intersections={of=b and l, by={F,C}}];
\coordinate (B) at ($(A)!(C)!(X)$);
\tkzDrawPolygon(A,B,C)
\draw (C) -- (X);
%\tkzMarkAngle[size=2.7,](X,A,C)
%   \tkzLabelAngle[pos=2,transform shape=false](X,A,C){$\alpha$}
%\tkzMarkAngle[size=6,](A,C,X)
%   \tkzLabelAngle[pos=5,transform shape=false](A,C,X){$\beta$}
%\tkzMarkAngle[size=1.4,](C,X,A)
%   \tkzLabelAngle[pos=0.8,transform shape=false](C,X,A){$\delta$}
%\tkzMarkAngle[size=2.7,](B,X,C)
%   \tkzLabelAngle[pos=2,transform shape=false](B,X,C){$\gamma$}
%\tkzMarkRightAngle[size=1](C,B,X)
\draw[transform shape=false,thin,decorate,decoration={brace,amplitude=7pt}](X)--(A)
    node [midway,yshift=-12pt] {$\AX$};
\tkzCalcLength[cm](B,C)\tkzGetLength{BC} \FPround\BC\BC{3}
\draw[transform shape=false,thin,decorate,decoration={brace,amplitude=7pt}](C)--(B)
    node [midway,xshift=32pt] {$\BC$};
\draw[transform shape=false,thin,decorate,decoration={brace,amplitude=7pt}](B)--(X)
    node [midway,yshift=-12pt] {$A$};
\draw[blue](current bounding box.south west)rectangle(current bounding box.north east);
\end{tikzpicture}\end{scaletikzpicturetowidth}\end{center}

\begin{center}\begin{scaletikzpicturetowidth}{0.66\textwidth}\begin{tikzpicture}[ptgrs]
\tkzInit[xmin=-0.5,xmax=45,ymin=-2,ymax=15] \tkzClip %\tkzGrid
\def\AX{12}
\def\angXAC{25.5}
\def\angBXC{38.25}
\tkzDefPoint(0,0){A}
\tkzDefPoint(\AX,0){X}
\path[name path=b] (A) -- (\angXAC:40cm); 
\path[name path=l] (A) -- (X) --+ (\angBXC:30cm);
\draw [name intersections={of=b and l, by={F,C}}];
\coordinate (B) at ($(A)!(C)!(X)$);
\tkzDrawPolygon(A,B,C)
\draw (C) -- (X);
%\tkzMarkAngle[size=2.7,](X,A,C)
%   \tkzLabelAngle[pos=2,transform shape=false](X,A,C){$\alpha$}
%\tkzMarkAngle[size=6,](A,C,X)
%   \tkzLabelAngle[pos=5,transform shape=false](A,C,X){$\beta$}
%\tkzMarkAngle[size=1.4,](C,X,A)
%   \tkzLabelAngle[pos=0.8,transform shape=false](C,X,A){$\delta$}
%\tkzMarkAngle[size=2.7,](B,X,C)
%   \tkzLabelAngle[pos=2,transform shape=false](B,X,C){$\gamma$}
%\tkzMarkRightAngle[size=1](C,B,X)
\draw[transform shape=false,thin,decorate,decoration={brace,amplitude=7pt}](X)--(A)
    node [midway,yshift=-12pt] {$\AX$};
\tkzCalcLength[cm](B,C)\tkzGetLength{BC} \FPround\BC\BC{3}
\draw[transform shape=false,thin,decorate,decoration={brace,amplitude=7pt}](C)--(B)
    node [midway,xshift=32pt] {$\BC$};
\draw[transform shape=false,thin,decorate,decoration={brace,amplitude=7pt}](B)--(X)
    node [midway,yshift=-12pt] {$A$};
\draw[blue](current bounding box.south west)rectangle(current bounding box.north east);
\end{tikzpicture}\end{scaletikzpicturetowidth}\end{center}

\begin{center}\begin{scaletikzpicturetowidth}{0.66\textwidth}\begin{tikzpicture}[ptgrs]
\tkzInit[xmin=-0.5,xmax=50,ymin=-2,ymax=15] \tkzClip %\tkzGrid
\def\AX{12}
\def\angXAC{25.5}
\def\angBXC{38.25}
\tkzDefPoint(0,0){A}
\tkzDefPoint(\AX,0){X}
\path[name path=b] (A) -- (\angXAC:40cm); 
\path[name path=l] (A) -- (X) --+ (\angBXC:30cm);
\draw [name intersections={of=b and l, by={F,C}}];
\coordinate (B) at ($(A)!(C)!(X)$);
\tkzDrawPolygon(A,B,C)
\draw (C) -- (X);
%\tkzMarkAngle[size=2.7,](X,A,C)
%   \tkzLabelAngle[pos=2,transform shape=false](X,A,C){$\alpha$}
%\tkzMarkAngle[size=6,](A,C,X)
%   \tkzLabelAngle[pos=5,transform shape=false](A,C,X){$\beta$}
%\tkzMarkAngle[size=1.4,](C,X,A)
%   \tkzLabelAngle[pos=0.8,transform shape=false](C,X,A){$\delta$}
%\tkzMarkAngle[size=2.7,](B,X,C)
%   \tkzLabelAngle[pos=2,transform shape=false](B,X,C){$\gamma$}
%\tkzMarkRightAngle[size=1](C,B,X)
\draw[transform shape=false,thin,decorate,decoration={brace,amplitude=7pt}](X)--(A)
    node [midway,yshift=-12pt] {$\AX$};
\tkzCalcLength[cm](B,C)\tkzGetLength{BC} \FPround\BC\BC{3}
\draw[transform shape=false,thin,decorate,decoration={brace,amplitude=7pt}](C)--(B)
    node [midway,xshift=32pt] {$\BC$};
\draw[transform shape=false,thin,decorate,decoration={brace,amplitude=7pt}](B)--(X)
    node [midway,yshift=-12pt] {$A$};
\draw[blue](current bounding box.south west)rectangle(current bounding box.north east);
\end{tikzpicture}\end{scaletikzpicturetowidth}\end{center}

\begin{center}\begin{scaletikzpicturetowidth}{0.66\textwidth}\begin{tikzpicture}[ptgrs]
\tkzInit[xmin=-0.5,xmax=55,ymin=-2,ymax=15] \tkzClip %\tkzGrid
\def\AX{12}
\def\angXAC{25.5}
\def\angBXC{38.25}
\tkzDefPoint(0,0){A}
\tkzDefPoint(\AX,0){X}
\path[name path=b] (A) -- (\angXAC:40cm); 
\path[name path=l] (A) -- (X) --+ (\angBXC:30cm);
\draw [name intersections={of=b and l, by={F,C}}];
\coordinate (B) at ($(A)!(C)!(X)$);
\tkzDrawPolygon(A,B,C)
\draw (C) -- (X);
%\tkzMarkAngle[size=2.7,](X,A,C)
%   \tkzLabelAngle[pos=2,transform shape=false](X,A,C){$\alpha$}
%\tkzMarkAngle[size=6,](A,C,X)
%   \tkzLabelAngle[pos=5,transform shape=false](A,C,X){$\beta$}
%\tkzMarkAngle[size=1.4,](C,X,A)
%   \tkzLabelAngle[pos=0.8,transform shape=false](C,X,A){$\delta$}
%\tkzMarkAngle[size=2.7,](B,X,C)
%   \tkzLabelAngle[pos=2,transform shape=false](B,X,C){$\gamma$}
%\tkzMarkRightAngle[size=1](C,B,X)
\draw[transform shape=false,thin,decorate,decoration={brace,amplitude=7pt}](X)--(A)
    node [midway,yshift=-12pt] {$\AX$};
\tkzCalcLength[cm](B,C)\tkzGetLength{BC} \FPround\BC\BC{3}
\draw[transform shape=false,thin,decorate,decoration={brace,amplitude=7pt}](C)--(B)
    node [midway,xshift=32pt] {$\BC$};
\draw[transform shape=false,thin,decorate,decoration={brace,amplitude=7pt}](B)--(X)
    node [midway,yshift=-12pt] {$A$};
\draw[blue](current bounding box.south west)rectangle(current bounding box.north east);
\end{tikzpicture}\end{scaletikzpicturetowidth}\end{center}

\end{document}

The output is as follows:

(don't mind the wrongly displayed labels)

The imprecision gets worse, or different, if I use for example \FPround\BC\BC{4} or if I use different margins with geometry.

To be clear I don't think it's a terrible bug or even if it's a bug since as you can see there's a lot of stuff going on in the code but I would like to know what causes this and if there's a way to get more accurate lengths.

4
  • 1
    TeX has limited precision - there are quite a lot of questions about this - and I am guessing that's the issue here. It is not really a bug. More a known limitation of the software, I think. You can use ... is it floating point ... but there is no perfect solution, as I understand it.
    – cfr
    Commented Jan 19, 2016 at 12:54
  • "More a known limitation of the software, I think." Oh, that's good to know, I thought as much but it never hurts to ask, maybe I was doing something wrong. Commented Jan 20, 2016 at 1:27
  • I don't know for sure that's responsible. But this has the familiar feel of the many TikZ questions about why the calculations come out just a little bit off. And the answer always seems to be: TeX features a certain degree of precision and that's a hard limit. At least, so I understand it. I don't understand the system at that level directly, but it seems non-controversial among the experts who do. I doubt very much you are doing anything wrong.
    – cfr
    Commented Jan 20, 2016 at 1:44
  • 1
    tkzCalcLength uses the package fp and the precision is correct. When tikz uses TeX to calculate the results can be inaccurate. Commented Feb 28, 2016 at 20:42

1 Answer 1

2

I have a more precise answer but not yet entirely satisfactory. Firstly the problem is not with the macro \tkzCalcLength[cm](B,C) but I think with the scaleoption

If you remove the tkzClip you get the same result

enter image description here

Then like in my question/answer same problem here if you change

{0.66\textwidth} with {0.5\textwidth}.

Now the problem is to find the link between some calculations and the option scale

\documentclass{article}
\usepackage{tkz-euclide}
\usepackage{environ}

\makeatletter
\newsavebox{\measure@tikzpicture}
\NewEnviron{scaletikzpicturetowidth}[1]{%
  \def\tikz@width{#1}%
  \def\tikzscale{1}%
  \begin{lrbox}{\measure@tikzpicture}%
  \BODY
  \end{lrbox}%
  \pgfmathparse{#1/\wd\measure@tikzpicture}%
  \edef\tikzscale{\pgfmathresult}%
  \BODY
}
\makeatother

\tikzset{ptgrs/.style={thick,scale=\tikzscale}}

\begin{document}

\begin{center}
\begin{scaletikzpicturetowidth}{0.75\textwidth}
\begin{tikzpicture}[ptgrs]
\tkzInit[xmin=-1,xmax=35,ymin=-2,ymax=15]% \tkzClip 
\def\AX{12}
\def\angXAC{25.5}
\def\angBXC{38.25}
\tkzDefPoint(0,0){A}
\tkzDefPoint(\AX,0){X}
\path[name path=b] (A) -- (\angXAC:40cm); 
\path[name path=l] (A) -- (X) --+ (\angBXC:30cm);
\draw [name intersections={of=b and l, by={F,C}}];
\coordinate (B) at ($(A)!(C)!(X)$);
\tkzDrawPolygon(A,B,C)
\draw (C) -- (X);
\draw[transform shape=false,thin,decorate,decoration={brace,amplitude=7pt}](X)--(A)
    node [midway,yshift=-12pt] {$\AX$};
\tkzCalcLength[cm](B,C)\tkzGetLength{BC} \FPround\BC\BC{3}
\draw[transform shape=false,thin,decorate,decoration={brace,amplitude=7pt}](C)--(B)
    node [midway,xshift=32pt] {$\BC$};
\draw[transform shape=false,thin,decorate,decoration={brace,amplitude=7pt}](B)--(X)
    node [midway,yshift=-12pt] {$A$};
\draw[blue](current bounding box.south west)rectangle(current bounding box.north east);
\end{tikzpicture}
\end{scaletikzpicturetowidth}
\end{center}

\begin{center}
  \begin{scaletikzpicturetowidth}{0.75\textwidth}
    \begin{tikzpicture}[ptgrs]
\tkzInit[xmin=-1,xmax=40,ymin=-2,ymax=15] \tkzClip 
\def\AX{12}
\def\angXAC{25.5}
\def\angBXC{38.25}
\tkzDefPoint(0,0){A}
\tkzDefPoint(\AX,0){X}
\path[name path=b] (A) -- (\angXAC:40cm); 
\path[name path=l] (A) -- (X) --+ (\angBXC:30cm);
\draw [name intersections={of=b and l, by={F,C}}];
\coordinate (B) at ($(A)!(C)!(X)$);
\tkzDrawPolygon(A,B,C)
\draw (C) -- (X);
\draw[transform shape=false,thin,decorate,decoration={brace,amplitude=7pt}](X)--(A)
    node [midway,yshift=-12pt] {$\AX$};
\tkzCalcLength[cm](B,C)\tkzGetLength{BC} \FPround\BC\BC{3}
\draw[transform shape=false,thin,decorate,decoration={brace,amplitude=7pt}](C)--(B)
    node [midway,xshift=32pt] {$\BC$};
\draw[transform shape=false,thin,decorate,decoration={brace,amplitude=7pt}](B)--(X)
    node [midway,yshift=-12pt] {$A$};
\draw[blue](current bounding box.south west)rectangle(current bounding box.north east);
\end{tikzpicture}
\end{scaletikzpicturetowidth}
\end{center}

\begin{center}
\begin{scaletikzpicturetowidth}{0.75\textwidth}
\begin{tikzpicture}[ptgrs]
\def\AX{12}
\def\angXAC{25.5}
\def\angBXC{38.25}
\tkzDefPoint(0,0){A}
\tkzDefPoint(\AX,0){X}
\path[name path=b] (A) -- (\angXAC:40cm); 
\path[name path=l] (A) -- (X) --+ (\angBXC:30cm);
\draw [name intersections={of=b and l, by={F,C}}];
\coordinate (B) at ($(A)!(C)!(X)$);
\tkzDrawPolygon(A,B,C)
\draw (C) -- (X);
\draw[transform shape=false,thin,decorate,decoration={brace,amplitude=7pt}](X)--(A)
    node [midway,yshift=-12pt] {$\AX$};
\tkzCalcLength[cm](B,C)\tkzGetLength{BC} \FPround\BC\BC{3}
\draw[transform shape=false,thin,decorate,decoration={brace,amplitude=7pt}](C)--(B)
    node [midway,xshift=32pt] {$\BC$};
\draw[transform shape=false,thin,decorate,decoration={brace,amplitude=7pt}](B)--(X)
    node [midway,yshift=-12pt] {$A$};
\draw[blue](current bounding box.south west)rectangle(current bounding box.north east);
\end{tikzpicture}
\end{scaletikzpicturetowidth}
\end{center}
\end{document}

I remove the \tkzClip in the first and the last figure, so the width of the picture is the same but with \tkzClip the with is different and scale different. I need to verify step by step the different coordinates with/without scale.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .