[Tex/LaTex] Placing labels at the end of lines

pgfplotstikz-pgf

I have a display depicting two triangles that are reflections of each other across the line y=x. At the end of the line y=x – in the same direction as the line and 7.5pt away from the arrow tip – I would like to place the label $\ell$. I tried using the following command.

\node at ($(90,90) + (45:7.5pt)$){$\ell$};

Nothing was printed – no display. I am trying to use the calc package to position the label. I guess there is some confusion because I am using the coordinate system when I issue (90,90) and I am using the pt unit when I issue (7.5pt,7.5pt).

Also, I want to have k and $k^{\prime}$ typeset at the end of the two other lines drawn in the display. The slopes of the lines are 1/3 and 3. So, I would like to put these labels off the right arrow tips of these lines – in the same direction as the line and 7.5pt away from the arrow tip. (I think that the "added space" would be issued using {atan(1/3):7.5pt} or {atan(3):7.5pt}.)

\documentclass{amsart}
\usepackage{amsmath}

\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections}


\usepackage{pgfplots}
\pgfplotsset{compat=1.11}


\begin{document}


\begin{tikzpicture}
\begin{axis}[width=5in,axis equal image,clip=false,
axis lines=middle,
xmin=-54,xmax=90,
xlabel=$x$,ylabel=$y$,
ymin=-54,ymax=90,
restrict y to domain=-54:90,
enlargelimits={abs=0.25cm},
axis line style={latex-latex},
ticklabel style={font=\tiny,fill=white},
xtick={\empty},ytick={\empty},
extra x ticks={15},
extra x tick labels={15},
xticklabel style={anchor=south},
xticklabel shift=-4pt,
extra y ticks={15},
extra y tick labels={15},
yticklabel style={anchor=west},
yticklabel shift=-4pt,
xlabel style={at={(ticklabel* cs:1)},anchor=north west},
ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]

\addplot[dashed,latex-latex,samples=2,domain=-54:90]{x};

\draw (axis cs:-30,-30) coordinate(A) node[left]{$A$};
\draw (axis cs:-21,29) coordinate(B) node[above left]{$B$};
\draw (axis cs:18,42) coordinate(C) node[above]{$C$};
\draw (axis cs:0,15) coordinate(P);

\addplot[dashed,latex-,samples=2,domain=-54:-21,blue]{(1/3) * x + 36};
\addplot[dashed,-latex,samples=2,domain=18:90,blue]{(1/3) * x + 36};

\draw (axis cs:29,-21) coordinate(b) node[below right]{$B^{\prime}$};
\draw (axis cs:42,18) coordinate(c) node[right]{$C^{\prime}$};
\draw (axis cs:15,0) coordinate(p);

\addplot[latex-,samples=2,loosely dash dot,domain=18:29,green]{3 * x - 108};
\addplot[-latex,samples=2,loosely dash dot,domain=42:66,green]{3 * x - 108};

\draw [fill] (54,54) circle [radius=1.5pt];
\end{axis}

\draw[blue] (A) -- (B) -- (C) -- cycle;
\draw[dashed,blue] (B) -- (P);


\draw[dashed,green] (A) -- (b) -- (c) -- cycle;
\draw[densely dashed,green] (b) -- (p);

\end{tikzpicture}

\end{document}

Best Answer

Using the calc library you can say

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

to locate the point that is <length> units from (A) on the segment joining (A) and (B). For example

\node at ( $ (axis cs:90,90)!7.5pt!(axis cs:100,100) $ ) {$\ell$};

will place $ell$ at a distance 7.5pt away from (axis cs:90,90) along the segment joining (axis cs:90,90) and (axis cs:100,100).

The code:

\documentclass{amsart}
\usepackage{amsmath}

\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections}


\usepackage{pgfplots}
\pgfplotsset{compat=1.11}


\begin{document}


\begin{tikzpicture}
\begin{axis}[width=5in,axis equal image,clip=false,
axis lines=middle,
xmin=-54,xmax=90,
xlabel=$x$,ylabel=$y$,
ymin=-54,ymax=90,
restrict y to domain=-54:90,
enlargelimits={abs=0.25cm},
axis line style={latex-latex},
ticklabel style={font=\tiny,fill=white},
xtick={\empty},ytick={\empty},
extra x ticks={15},
extra x tick labels={15},
xticklabel style={anchor=south},
xticklabel shift=-4pt,
extra y ticks={15},
extra y tick labels={15},
yticklabel style={anchor=west},
yticklabel shift=-4pt,
xlabel style={at={(ticklabel* cs:1)},anchor=north west},
ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]

\addplot[dashed,latex-latex,samples=2,domain=-54:90]{x};
\node at ( $ (axis cs:90,90)!7.5pt!(axis cs:100,100) $ ) {$\ell$};

\draw (axis cs:-30,-30) coordinate(A) node[left]{$A$};
\draw (axis cs:-21,29) coordinate(B) node[above left]{$B$};
\draw (axis cs:18,42) coordinate(C) node[above]{$C$};
\draw (axis cs:0,15) coordinate(P);

\addplot[dashed,latex-,samples=2,domain=-54:-21,blue]{(1/3) * x + 36};
\addplot[dashed,-latex,samples=2,domain=18:90,blue]{(1/3) * x + 36};

\node at ( $ (axis cs:90,0.3333*90+36)!7.5pt!(axis cs:100,0.3333*100+36) $ ) {$k$};

\draw (axis cs:29,-21) coordinate(b) node[below right]{$B^{\prime}$};
\draw (axis cs:42,18) coordinate(c) node[right]{$C^{\prime}$};
\draw (axis cs:15,0) coordinate(p);

\addplot[latex-,samples=2,loosely dash dot,domain=18:29,green]{3 * x - 108};
\addplot[-latex,samples=2,loosely dash dot,domain=42:66,green]{3 * x - 108};

\node at ( $ (axis cs:66,3*66-108)!7.5pt!(axis cs:100,3*100-108) $ ) {$k'$};

\draw [fill] (54,54) circle [radius=1.5pt];
\end{axis}

\draw[blue] (A) -- (B) -- (C) -- cycle;
\draw[dashed,blue] (B) -- (P);


\draw[dashed,green] (A) -- (b) -- (c) -- cycle;
\draw[densely dashed,green] (b) -- (p);

\end{tikzpicture}

\end{document}

The result:

enter image description here

For consistency's sake and also to facilitate code maintenance, you could define some functions:

\documentclass{amsart}
\usepackage{amsmath}

\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections}


\usepackage{pgfplots}
\pgfplotsset{compat=1.11}


\begin{document}


\begin{tikzpicture}[
declare function={ 
  line1(\x) = (1/3) * \x + 36;
  line2(\x) = 3*\x - 108;
  }
]
\begin{axis}[width=5in,axis equal image,clip=false,
axis lines=middle,
xmin=-54,xmax=90,
xlabel=$x$,ylabel=$y$,
ymin=-54,ymax=90,
restrict y to domain=-54:90,
enlargelimits={abs=0.25cm},
axis line style={latex-latex},
ticklabel style={font=\tiny,fill=white},
xtick={\empty},ytick={\empty},
extra x ticks={15},
extra x tick labels={15},
xticklabel style={anchor=south},
xticklabel shift=-4pt,
extra y ticks={15},
extra y tick labels={15},
yticklabel style={anchor=west},
yticklabel shift=-4pt,
xlabel style={at={(ticklabel* cs:1)},anchor=north west},
ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]

\addplot[dashed,latex-latex,samples=2,domain=-54:90]{x};
\node at ( $ (axis cs:90,90)!7.5pt!(axis cs:100,100) $ ) {$\ell$};

\draw (axis cs:-30,-30) coordinate(A) node[left]{$A$};
\draw (axis cs:-21,29) coordinate(B) node[above left]{$B$};
\draw (axis cs:18,42) coordinate(C) node[above]{$C$};
\draw (axis cs:0,15) coordinate(P);

\addplot[dashed,latex-,samples=2,domain=-54:-21,blue]{line1(\x)};
\addplot[dashed,-latex,samples=2,domain=18:90,blue]{line1(\x)};

\node at ( $ (axis cs:90,{line1(90)})!7.5pt!(axis cs:91,{line1(91)}) $ ) {$k$};

\draw (axis cs:29,-21) coordinate(b) node[below right]{$B^{\prime}$};
\draw (axis cs:42,18) coordinate(c) node[right]{$C^{\prime}$};
\draw (axis cs:15,0) coordinate(p);

\addplot[latex-,samples=2,loosely dash dot,domain=18:29,green]{line2(\x)};
\addplot[-latex,samples=2,loosely dash dot,domain=42:66,green]{line2(\x)};

\node at ( $ (axis cs:66,{line2(66)})!7.5pt!(axis cs:67,{line2(67)}) $ ) {$k'$};

\draw [fill] (54,54) circle [radius=1.5pt];
\end{axis}

\draw[blue] (A) -- (B) -- (C) -- cycle;
\draw[dashed,blue] (B) -- (P);


\draw[dashed,green] (A) -- (b) -- (c) -- cycle;
\draw[densely dashed,green] (b) -- (p);

\end{tikzpicture}

\end{document}
Related Question