# Can anyone help me mark a right-angle in Tikz?

I've been stuck here playing with numbers using \draw for ages now. There must be a better way. I want to add this right-angle symbol: and I don't know how I could do it neatly. Can someone help me out? Thanks. MWE:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[domain=0:4]
\draw (0.1,-0.2) node[left]{\textcolor{gray}{O}};
\draw[thick, color=gray,->] (-4,0) -- (8,0) node[right] {\textcolor{black}{$x$}};
\draw[thick, color=gray, ->] (0,-1) -- (0,5) node[above] {\textcolor{black}{$y$}};
\draw (3+2,5)--(-4+2,-1) node[above]{$l_1$};
\draw (-0.5,5)--(36/7-0.5,-1) node[above]{$l_2$};
\draw [color=gray, dashed] (1.9, 2.28) node[circle,fill=black,inner sep=0pt,minimum size=3pt, label=left:{\textcolor{black}{$P~~$}}]{}--(4.5,2.28)node[circle,fill=black,inner sep=0pt,minimum size=3pt, label=below right:{\textcolor{black}{$Q$}}]{}--(4.5,4.6) node[circle,fill=black,inner sep=0pt,minimum size=3pt, label= right: {\textcolor{black}{$~R$}}]{};
\draw (4.5,2.58)--(4.2,2.58)--(4.2,2.28);
\draw [color=gray, dashed] (1.9, 2.28) --(1.9,-0.3)node[circle,fill=black,inner sep=0pt,minimum size=3pt, label= below left:{\textcolor{black}{$S$}}]{}--(4,-0.3) node[circle,fill=black,inner sep=0pt,minimum size=3pt, label= below:{\textcolor{black}{$T~~~$}}]{};
\draw (1.9,0)--(2.2,0)--(2.2,-0.3);
\draw (2.9,2.28) arc (0:45:1cm);
\node at (2.5,2.5) {$\theta$};
\draw (3.4,2.28) arc (0:-51:1.5cm);
\node[rotate=-25] at (2.7,1.9) {$\theta-90^{\circ}$};
\draw (2.6,1.38) arc (-51:-93:1cm);
\node at (2.1,1.5) {$\theta$};
\end{tikzpicture}
\end{document}

• If you can read Freanch, you can use \TkzMarkRightAngle from the tikz-euclide package. – John Kormylo May 30 '15 at 16:34
• @JohnKormylo Why not post that as an answer (including output and code)? :D – Alenanno May 30 '15 at 16:43
• @JohnKormylo I think you can still use \tkzMarkRightAngle even if you can't read French. Though, in that case, understanding the manual might be rather difficult. :-P – A.Ellett May 30 '15 at 16:43
• @Alenanno - I could, but (A) he never named any of his coordinates and (B) I don't read French. – John Kormylo May 30 '15 at 16:50
• @JohnKormylo I'd be most indebted to you if you would :P – Luke Collins May 30 '15 at 17:35

Here's a solution (almost) entirely using the tkz-euclide package, almost, excluding the first four nodes. That's because I don't know how to place custom labels at certain points (yet).

## Code

\documentclass{article}
\usepackage{tikz, tkz-euclide}
\usetkzobj{all}

\usetikzlibrary{arrows.meta,intersections,calc}

\tikzset{
mynode/.style={circle,fill=black,inner sep=0pt,minimum size=3pt},
}

\begin{document}
\begin{tikzpicture}[domain=0:4]

\node[anchor=north west] at (8,0) {$x$};
\node[anchor=south east] at (0,5) {$y$};
\node[anchor=west] at (-2.5,-2) {$l_1$};
\node[anchor=east] at (5,-2) {$l_2$};

%%% Axis coordinates and axis lines (X axis, Y axis)
\tkzDefPoints{-4/0/Z, 8/0/X, 0/-1/W, 0/5/Y}

%%% Our main lines
\tkzDefPoints{5/5/A, -2/-1/B, -.5/5/C, 4.642857143/-1/D}

%Getting points
\tkzInterLL(A,B)(C,D) \tkzGetPoint{P}
\tkzDefPointBy[projection=onto B--D](P) \tkzGetPoint{S}

\tkzDefLine[parallel=through S](Z,X) \tkzGetPoint{parax}
\tkzInterLL(S,parax)(C,D) \tkzGetPoint{T}

\tkzDefPointBy[rotation=center P angle 90](S) \tkzGetPoint{Q}
\tkzDefLine[parallel=through Q](W,Y) \tkzGetPoint{paray}
\tkzInterLL(Q,paray)(A,B) \tkzGetPoint{R}

% Draw lines and draw/place label points
\tkzDrawLines(A,B C,D)
\tkzDrawLines[dashed,color=gray,add=0 and 0](P,S S,T P,Q Q,R)
\tkzLabelPoints[left](P,S)
\tkzLabelPoints[right](T,Q,R)
\tkzDrawPoints[color=black,fill=black](P,S,T,Q,R)

%%% Angles
% Right angles
\tkzFindAngle(C,P,A)
\tkzGetAngle{angleCPA};
\tkzMarkRightAngle[draw=black,size=.5](C,P,A)
\tkzFindAngle(T,S,P)
\tkzGetAngle{angleTSP};
\tkzMarkRightAngle[draw=black](T,S,P)
\tkzFindAngle(P,Q,R)
\tkzGetAngle{anglePQR};
\tkzMarkRightAngle[draw=black](P,Q,R)

% other angles
\tkzFindAngle(S,P,T)
\tkzGetAngle{angleSPT};
%\tkzMarkAngle[draw=black](S,P,T) % This one cannot be used here because dashed lines. See \tkzDrawArc below
\tkzDrawArc[R with nodes, color=blue](P,1cm)(S,T)
\tkzLabelAngle[dist=.6](S,P,T){$\theta$}

\tkzFindAngle(Q,P,R)
\tkzGetAngle{angleQPR};
\tkzDrawArc[R with nodes, color=blue](P,1cm)(Q,R)
\tkzLabelAngle[dist=.6](Q,P,R){$\theta$}

\tkzFindAngle(T,P,Q)
\tkzGetAngle{angleTPQ};
\tkzDrawArc[R with nodes, color=red](P,1.8cm)(T,Q)
\tkzLabelAngle[dist=1.1,rotate=-25](T,P,Q){$\theta - 90^\circ$}

\end{tikzpicture}
\end{document}


I couldn't get tkz-euclide to work, so I wrote my own \MarkRightAngle macro. I also corrected some of your coordinates: l_1 is given by y=.857 x +.714 and l_2 is given by y = -1.167 x +4.4167.

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

\newcommand{\MarkRightAngle}[4][.3cm]% #1=size (optional), #2-#4 three points: \angle #2#3#4
{\coordinate (tempa) at ($(#3)!#1!(#2)$);
\coordinate (tempb) at ($(#3)!#1!(#4)$);
\coordinate (tempc) at ($(tempa)!0.5!(tempb)$);%midpoint
\draw (tempa) -- ($(#3)!2!(tempc)$) -- (tempb);
}

\begin{document}
\begin{tikzpicture}[domain=0:4]
\coordinate (P) at (1.83,2.28);
\coordinate (Q) at (4.5,2.28);
\coordinate (R) at (4.5,4.57);
\coordinate (S) at (1.83,-0.367);
\coordinate (T) at (4.1,-0.367);
\coordinate (U) at (0,4.417);
\draw (0.1,-0.2) node[left]{\textcolor{gray}{O}};
\draw[thick, color=gray,->] (-4,0) -- (8,0) node[right] {\textcolor{black}{$x$}};
\draw[thick, color=gray, ->] (0,-1) -- (0,5) node[above] {\textcolor{black}{$y$}};
\draw (3+2,5)--(-4+2,-1) node[above]{$l_1$};
\draw (-0.5,5)--(36/7-0.5,-1) node[above]{$l_2$};
\draw [color=gray, dashed] (P) node[circle,fill=black,inner sep=0pt,minimum size=3pt, label=left:{\textcolor{black}{$P~~$}}]{}--(Q) node[circle,fill=black,inner sep=0pt,minimum size=3pt, label=below right:{\textcolor{black}{$Q$}}]{}--(R) node[circle,fill=black,inner sep=0pt,minimum size=3pt, label= right: {\textcolor{black}{$~R$}}]{};
\draw [color=gray, dashed] (P) --(S)node[circle,fill=black,inner sep=0pt,minimum size=3pt, label= below left:{\textcolor{black}{$S$}}]{}--(T) node[circle,fill=black,inner sep=0pt,minimum size=3pt, label= below:{\textcolor{black}{$T~~~$}}]{};
\draw (2.9,2.28) arc (0:45:1cm);
\node at (2.5,2.5) {$\theta$};
\draw (3.4,2.28) arc (0:-51:1.5cm);
\node[rotate=-25] at (2.7,1.9) {$\theta-90^{\circ}$};
\draw (2.6,1.38) arc (-51:-93:1cm);
\node at (2.1,1.5) {$\theta$};
\MarkRightAngle{P}{S}{T}
\MarkRightAngle{P}{Q}{R}
\MarkRightAngle{U}{P}{R}
\end{tikzpicture}
\end{document}


• I love your \MarkRightAngle macro, but if the triangle is not approximately isosceles, the right-angle mark can be an elongated rectangle. I fixed this by multiplying one of the #1 by an adjustment factor, but ideally you'd want to have two optional arguments for size (and two defaults), which is not super easy to write, except perhaps with the xparse package. – PatrickT Jul 1 at 10:11
• @PatrickT - The size parameter is supposed to use units, like 5mm instead of 0.5. Anyway, TikZ now supports angles (angle library). – John Kormylo Jul 1 at 22:19
• Oh I didn't realize. Let me look into it. Thanks! – PatrickT Jul 2 at 1:49

I don't have time to rewrite using tkz-euclide but here is how you can work the repair suggested above into your code:

\documentclass{article}
\usepackage{tikz}
\usepackage{tkz-euclide}
\usetkzobj{all}%this is important!
\begin{document}
\begin{tikzpicture}[domain=0:4]
\tkzDefPoint (4.5,4.6){A}
\tkzDefPoint (1.840.5,2.3){B}
\tkzDefPoint (-0.5,5){C}
\tkzMarkRightAngle(C,B,A)
\draw (0.1,-0.2) node[left]{\textcolor{gray}{O}};
\draw[thick, color=gray,->] (-4,0) -- (8,0) node[right] {\textcolor{black}{$x$}};
\draw[thick, color=gray, ->] (0,-1) -- (0,5) node[above] {\textcolor{black}{$y$}};
\draw (3+2,5)--(-4+2,-1) node[above]{$l_1$};
\draw (-0.5,5)--(36/7-0.5,-1) node[above]{$l_2$};
\draw [color=gray, dashed] (1.9, 2.28) node[circle,fill=black,inner sep=0pt,minimum size=3pt, label=left:{\textcolor{black}{$P~~$}}]{}--(4.5,2.28)node[circle,fill=black,inner sep=0pt,minimum size=3pt, label=below right:{\textcolor{black}{$Q$}}]{}--(4.5,4.6) node[circle,fill=black,inner sep=0pt,minimum size=3pt, label= right: {\textcolor{black}{$~R$}}]{};
\draw (4.5,2.58)--(4.2,2.58)--(4.2,2.28);
\draw [color=gray, dashed] (1.9, 2.28) --(1.9,-0.3)node[circle,fill=black,inner sep=0pt,minimum size=3pt, label= below left:{\textcolor{black}{$S$}}]{}--(4,-0.3) node[circle,fill=black,inner sep=0pt,minimum size=3pt, label= below:{\textcolor{black}{$T~~~$}}]{};
\draw (1.9,0)--(2.2,0)--(2.2,-0.3);
\draw (2.9,2.28) arc (0:45:1cm);
\node at (2.5,2.5) {$\theta$};
\draw (3.4,2.28) arc (0:-51:1.5cm);
\node[rotate=-25] at (2.7,1.9) {$\theta-90^{\circ}$};
\draw (2.6,1.38) arc (-51:-93:1cm);
\node at (2.1,1.5) {$\theta$};
\end{tikzpicture}
\end{document}


The line \usetkzobj{all} is important when using tkz-euclide. You need the 3 points forming the angle in the correct order as they trace out the angle. I just played around with numbers to get the right angle in the proper place. There is some explanation of the basics of tkz-euclide posted here: see the construction of the geometrical diagram at the bottom of the page. Most tkz-euclide content not addressed but it has much/all of what you are working with.

Here's yet another approach

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
%% \usetikzlibrary{calc}
\usepackage{tkz-euclide}
\usetkzobj{all}

\tikzset{my node dot/.style={circle,fill=black,inner sep=0pt,minimum size=3pt}}

\begin{document}

\begin{tikzpicture}[domain=0:4]

%% origin and axes including axis names:
\coordinate (O) at (0,0);
\draw[thick, color=gray,->]  (-4,0) -- (8,0) node[right] {$x$};
\draw[thick, color=gray, ->] (0,-1) -- (0,5) node[above] {$y$};

%% line with positive slope
\coordinate (l1a) at (3+2,5);
\coordinate (l1b) at (-4+2,-1);
\path (l1a) -- (l1b) coordinate[pos=0.07] (R);

%% line with negative slope
\coordinate (l2a) at (-0.5,5);
\coordinate (l2b) at (36/7-0.5,-1);
\path (l2a) -- (l2b) coordinate[pos=0.88] (T);

%%
\tkzInterLL(l1a,l1b)(l2a,l2b)\tkzGetPoint{P}
\coordinate (Q) at (P-|R);
\coordinate (S) at (P|-T);

%% labels for points
\node[my node dot,label=below left:{$O$}]      at (O) {};
\node[my node dot,label=right:{$R$}]           at (R) {};
\node[my node dot,label=below left:{$T$}]      at (T) {};
\node[my node dot,label=left:{$P$}]            at (P) {};
\node[my node dot,label=below right:{$Q$}]     at (Q) {};
\node[my node dot,label=below left:{$S$}]      at (S) {};

%% labels for lines
\draw (l1a) -- (l1b) node[pos=1,above] {$l_1$} ;
\draw (l2a) -- (l2b) node[pos=1,above] {$l_2$};

%% marking angles
%% marking the right angles
%%\tkzMarkRightAngle(P,Q,R) %% appears as a square
\draw (Q) ++ (-6pt,0) -- ++ (0,6pt) -- ++ (6pt,0);
\draw [color=gray, dashed] (P) -- (Q) -- (R) ;

%%\tkzMarkRightAngle(P,S,T) %% appears as a square
\draw (S) ++ (6pt,0) -- ++ (0,6pt) -- ++ (-6pt,0);
\draw [color=gray,dashed] (P) -- (S) -- (T);

\tkzMarkRightAngle(R,P,l2a)
%% if you use the calc library then the following will also draw a right angle
%% \draw[red] ($(P)!6pt!(R)$) --  ($($(P)!6pt!(R)$)!6pt!90:(R)$) -- ($(P)!6pt!(l2a)$);

%% marking other angles
\tkzMarkAngle(S,P,T)
\tkzLabelAngle[pos=0.75](S,P,T) {$\theta$}

\tkzMarkAngle(Q,P,R)
\tkzLabelAngle[pos=0.75](Q,P,R) {$\theta$}

\tkzMarkAngle[size=1.65](T,P,Q)
\tkzLabelAngle[rotate=-25](T,P,Q){$\theta-90^\circ$}

\end{tikzpicture}

\end{document}


which results in

The difference here with the other examples is that I use the tkz-euclide macro for finding:

• the intersection of two lines: \tkzInterLL(<point>,<point>)(<point>,<point>)
• you retrieve the point and name it via: \tkzGetPoint{<point name>}
• I use the tkz-euclide macros for marking and labeling angles: \tkzMarkAngle and \tkzLabelAngle.

I sometimes find the approach taken by tkz-euclide a bit suboptimal. For example, \tkzMarkRightAngle does a relatively nice job, but not against dashed lines. So, I improvised. I also show you how to improvise that right angle at P. My approach for that is a bit different from @JohnKormylo 's approach. I use the TikZ library calc. If I were to write it as a macro I would do it as follows:

\def\aeMarkRightAngle[size=#1](#2,#3,#4){%%
\draw ($(#3)!#1!(#2)$) --
($($(#3)!#1!(#2)$)!#1!90:(#2)$) --
($(#3)!#1!(#4)$);}


which can then be called as

  \aeMarkRightAngle[size=6pt](R,P,l2a)