1

I'd like to define the point C on line AB such that AC=k, using the calc library and the let... in... command. This does not work, why? Is there a unit problem (C seems to be pretty close to A)? I tried the scalar() function (pgfmanual 3.0.1A, p925) but it didn't help.

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
    \begin{tikzpicture}
        \coordinate[label=above left:$A$](A) at(-2,-1);
        \coordinate[label=above right:$B$](B) at (2.5,1);
        \pgfmathsetmacro\k{3.0}
        \path
            let \p1=($(B)-(A)$),
                 \n1={veclen(\x1,\y1)}
            in coordinate[label=below:$C$] (C) at ($(A)!{\k/\n1}!(B)$); 
        \foreach \p in {A,B,C}
            \fill (\p) circle (1.5pt);
    \end{tikzpicture}
\end{document}

Thank you!

3
  • Welcome to TeX:SE! Where you expect that coordinate C should be? Ar you sure, that calculation of coordinate C position is correct?
    – Zarko
    Commented Jan 30, 2022 at 12:13
  • Welcome to TeX.SE!!! If I understand what you need, you can define directly \coordinate[label=below:$C$] (C) at ($(A)!3cm!(B)$); or \coordinate[label=below:$C$] (C) at ($(A)!\k cm!(B)$);. Commented Jan 30, 2022 at 12:20
  • you should be aware, that \n1 is given in points (in your case it is 140.1146pt. So you first need to make both variable to be in the same length units.
    – Zarko
    Commented Jan 30, 2022 at 12:31

1 Answer 1

1

I suspect that you after the following:

enter image description here

( dashed line is added for better seeing that coordinates are in line)

For above result you should be aware, that \n1 is given in points (in your case it is 140.1146pt). So you first need to make both variable to be in the same length units and than in calculation of C position consider that number had to be scalar:

($(A)!<scalar>!(B)$)

Above image you can get with:

\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
    \begin{tikzpicture}
\coordinate[label=above  left:$A$](A) at (-2.5,-1);
\coordinate[label=above right:$B$](B) at ( 2.0, 1);
\pgfmathsetmacro\k{3*28.346456693} % <--- observe conversion of units
\path
    let \p1=($(B)-(A)$),
         \n1={veclen(\x1,\y1)}
    in coordinate[label=below:$C$] (C) at ($(A)!scalar(\k/\n1)!(B)$); % <--- observe use of "scalar" option
\foreach \p in {A,B,C}
    \fill (\p) circle (1.5pt);
\draw[densely dashed, ultra thin]   (A) -- (B); % added
    \end{tikzpicture}
\end{document}

or with

\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

    \begin{tikzpicture}
\coordinate[label=above  left:$A$](A) at (-2.5,-1);
\coordinate[label=above right:$B$](B) at ( 2.0, 1);
\pgfmathsetmacro\k{3} 
\path
    let \p1=($(B)-(A)$),
         \n1={veclen(\x1,\y1)}
    in coordinate[label=below:$C$] (C) at ($(A)!scalar(28.346456693*\k/\n1)!(B)$); % <--- observe conversion of units and scalar option
\foreach \p in {A,B,C}
    \fill (\p) circle (1.5pt);
\draw[densely dashed, ultra thin]   (A) -- (B); 
    \end{tikzpicture}
\end{document}

You must log in to answer this question.

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