Drawing and labeling the sides and angles of a right triangle

The following code instructs TikZ to draw a right triangle AQP with a right angle at Q. I mark the length of line segment AQ by y. How do I rotate by 90 degrees the letter y so that it is upright? I would like to draw a line segment perpendicular to PA from A to the line containing the leg PQ, and I would like to label that point of intersection R.

\documentclass[10pt]{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections,quotes,decorations.markings}
\usepackage{tkz-euclide}
\usetkzobj{all}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}

\begin{tikzpicture}[dot/.style={fill,circle,inner sep=1.5pt}]
\path
(80:5) node [dot,label=above left:$A$]{} coordinate (A)
(80:7) coordinate (a)
(20:9) coordinate (B)
(20:11) coordinate (b)
(0:0) node[dot,label=below left:$P$]{} coordinate(P)
(-100:1)coordinate (e)
(-160:1) coordinate (f);

\path coordinate (Q)at($(P)!(A)!(B)$) node at (Q)[dot,label=below:$Q$]{} ;
\draw[<->]  (a)--(e);
\draw[<->, name path=kline] (f)--(b) node[below right]{$k$}; % First line for intersection

\draw[purple!70!black,dashed] (A)--(Q);
\tkzMarkRightAngle(A,Q,P);

\draw ($(P)!3mm!90:(A)$)--($(A)!3mm!-90:(P)$)coordinate(u); % Note here the invisible point u, where uA is normal to pA at point A
\draw[|<->|] ($(P)!-7mm!90:(Q)$)--node[fill=white,sloped] {$x$} ($(Q)!-7mm!-90:(P)$);
\draw[|<->|] ($(Q)!-3mm!90:(A)$)--node[fill=white] {$y$} ($(A)!-3mm!-90:(Q)$);

\tkzMarkAngle[size=0.75cm,color=cyan,mark=||](B,P,A);
\tkzMarkAngle[size=1cm,color=cyan,mark=|](P,A,Q);

\path [name path=ARline] (u)--($(A)!-10cm!(u)$); % Second line for intersection
\path [name intersections={of = ARline and kline, by=R}];
\draw (A)--(R)node[dot,label=below:$R$]{};
\tkzMarkRightAngle(R,A,P);
\end{tikzpicture}

\end{document}


Your code produces errors; I tried to polish it by guessing what you were trying to do:

The code:

\documentclass[10pt]{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections,quotes,decorations.markings}
\usepackage{tkz-euclide}
\usetkzobj{all}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}

\begin{tikzpicture}[dot/.style={fill,circle,inner sep=1.5pt}]
\path
(80:5) node [dot,label=above left:$A$]{} coordinate (A)
(80:7) coordinate (a)
(20:9) node [dot,label=below:$B$]{} coordinate (B)
(20:11) coordinate (b)
(0:0) node[dot,label=below left:$P$]{} coordinate(P)
(-100:1)coordinate (e)
(-160:1) coordinate (f);

\node[dot,label=below:$Q$] (Q) at ($(P)!(A)!(B)$) {};
\node[dot,label=above right:$R$] (R) at ($(P)!(Q)!(A)$) {};

\draw[<->]  (a) -- (e);
\draw[<->]  (b) -- (f);

\draw[purple!70!black,dashed] (A) -- (Q);
\draw[green!70!black,dashed] (Q) -- (R);
\tkzMarkRightAngle(A,Q,P);

\draw[|<->|] ($(P)!3mm!90:(A)$)--node[fill=white,sloped] {$r$} ($(A)!3mm!-90:(P)$);
\draw[|<->|] ($(P)!-7mm!90:(Q)$)--node[fill=white,sloped] {$x$} ($(Q)!-7mm!-90:(P)$);
\draw[|<->|] ($(Q)!-3mm!90:(A)$)--node[fill=white] {$y$} ($(A)!-3mm!-90:(Q)$);

\tkzMarkAngle[size=0.75cm,color=cyan,mark=||](B,P,A);
\tkzMarkAngle[size=1cm,color=cyan,mark=|](P,A,Q);
\end{tikzpicture}

\end{document}

• You have been helpful in the past, Gonzalo. Nice to hear from you again. Why couldn't my code be compiled? To learn coding in TikZ, I wanted to first declare the coordinates of A, a, B, b, P, and use them to have TikZ calculate the point Q. After that, I wanted to declare \path (a) (b) ... (f). What is wrong with this syntax? Please show me the code in this way. I know that this is more coding than necessary. It will help me with the syntax. Nov 29 '14 at 2:36
• I see how you modified the code to get "y" printed the way I wanted. Thanks. You have point R on the wrong line. Point R should be on the line containing PQ. (I may have to extend the line containing P, Q, and B.) I tried to modify the code myself. I made a mess, and my computer is too slow to find in the manual the code for doing this. Thanks for your help. Nov 29 '14 at 2:40
• I have edited the file so that it now compiles. There are several edits that I would like to make. I want to declare coordinates for a, A, b, B, e, f, P, and Q. How do I do that? After that, I want to have a command like \draw (a) (b) ... (Q). I do not want the line that is drawn from the command \draw ($(P)!3mm!90:(A)$)--($(A)!3mm!-90:(P)$) coordinate(u);. (It is not part of the triangle.) I need the coordinate u defined by this command, though, to draw a line perpendicular to PA. How do I do that? What is 3mm!-90:? Nov 29 '14 at 20:41
• Are you there, Gonzalo? Nov 30 '14 at 20:05
• I had my computer look for commands in the pgfplots manual for drawing a line perpendicular to a given line. Nothing appeared. If you are not going to answer my specific questions, direct me to the pages in an appropriate manual. Why aren't you willing to answer my questions? I think that many users of TikZ would benefit from such answers. Dec 1 '14 at 19:56

This is what is required in the OP last comments:

\documentclass[10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections,quotes,decorations.markings}
\usepackage{tkz-euclide}
\usetkzobj{all}
\begin{document}

\begin{tikzpicture}[dot/.style={fill,circle,inner sep=1.5pt},line width=.7pt]
\path
(80:5) node[dot,label=above left:$A$]{} coordinate (A)
(80:7) coordinate (a)
(20:9) coordinate (B)
(20:11) coordinate (b)
(0:0) node[dot,label=below left:$P$]{} coordinate(P)
(-100:1)coordinate (e)
(-160:1)coordinate (f)
($(P)!(A)!(B)$)node[dot,label=below:$Q$]{} coordinate(Q);

\path[name path=kline](f)--(b); % First line for intersection
\path[name path=ARline](A)--($(A)!10cm!90:(P)$); % A line normal to pA at point A in the east direction

\path [name intersections={of = ARline and kline, by=R}];
\draw (A)--(P)--(R)node[dot,label=below:$R$]{}--cycle;
\draw[purple!70!black,dashed] (A)--(Q);

\tkzMarkRightAngle(A,Q,P);
\tkzMarkAngle[size=0.75cm,color=cyan,mark=||](B,P,A);
\tkzMarkAngle[size=1cm,color=cyan,mark=|](P,A,Q);
\tkzMarkRightAngle(R,A,P);
\end{tikzpicture}
\end{document}


Which gives:

The best way to learn TikZ is to study others codes besides reading the comprehensive user manual (latest version).

• Yes, I copied part of the code. I wanted to get a different triangle, and I tried to edit it to do so. To learn to code in TikZ, I would like to declare the coordinates for a bunch of points - a, A, b, B, ... and Q - first. After that, I would like to give a command like \path (a) (b) (P) (e) (f) (A) (B) (Q); to draw the triangle and lines. Why didn't it get compiled? Nov 29 '14 at 2:53
• @user143462 See my updated answer. Nov 29 '14 at 12:41
• I tried to modify the code to get just a right triangle containing two smaller right triangles. I removed the label for point B from the diagram, but I kept the point in the code, and I removed the line indicating the height y of the altitude of the triangle and the line indicating the length of PA. I need to remove line k and the line through points P and A. I tried the command \path[name path=kline] (f)--(b); but it made a mess of the diagram. Nov 29 '14 at 15:31
• @ AboAmmar I also need to remove the line indicating the length of line segment PA, too. I tried the command \path ($(P)!3mm!90:(A)$)--node[fill=white,sloped] {$r$} ($(A)!3mm!-90:(P)$) coordinate(u); but it also made a mess of the diagram. What is $(P)!3mm!90:(A)$ instructing TikZ to draw? Nov 29 '14 at 15:35
• @user143462 A picture is worth thousand words. Please add a picture of what the final output should look like. Besides, avoid just removing and adding nodes and lines randomly or by trial and error. This way you will not learn any thing. Before you remove/add some thing you should first try to know what exactly this makes or how it will affect the remaining code statements. Nov 29 '14 at 17:11

Perhaps this is what you want?

\documentclass[10pt]{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections,quotes,decorations.markings}
\usepackage{tkz-euclide}
\usetkzobj{all}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}

\begin{tikzpicture}[dot/.style={fill,circle,inner sep=1.5pt}]

\coordinate (Q) at (0,0);
\coordinate (A) at ($(Q)+(30:3)$);
\coordinate (P) at ($(Q)+(30+90:4)$);

\draw (A) -- (Q) -- (P) -- cycle;

\path (A) -- node [midway,sloped,above] {hypotenuse} (P);
\path (A) -- node [midway,sloped,below] {shorter leg} (Q);
\path (P) -- node [midway,sloped,below] {longer leg} (Q);

\tkzMarkRightAngle(A,Q,P);
\end{tikzpicture}

\end{document}


You seem to be attempting to assign a lot of different coordinates, but I'm having a hard time seeing what you're trying to accomplish. But you asked about labeling the sides, so that's what I've done.

• What is the "+" in the command \coordinate (A) at ($(Q)+(30:3)$); instructing TikZ to draw? I would like to declare the coordinates for a bunch of points first. After that, I would like to give the command \path (a) (b) (P) (e) (f) (A) (B) (Q); to draw the triangle and lines. Why didn't it get compiled? Nov 29 '14 at 2:49