# TikZ - How can I draw a line through a point on a line, with the new line perpendicular to the old line?

I have a line A-B between the two points A and B and a point X on A-B.

Now I'd like to draw a line through X which then is perpendicular to the line A-B. How can I do this?

Also:

No other (directly related) points around the line are given, I only have X which is the product of the usage of \path and creating two intersecting paths. Hence I say I can't use code in replies to other related questions (shown on the right), at least I do not see how.

## MWE

\documentclass{standalone}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}

\usepackage{
tikz,
xspace
}

\usetikzlibrary{
intersections,
calc
}

\begin{document}
\begin{tikzpicture}[font=\small]
\draw
(0,0) coordinate (A)
(1,1.435) coordinate (B)
(3,3) coordinate (C)
;
\draw[thick] (A) rectangle (C);
\draw[thick] (A)--(B) node[pos=0.6] (x) {x};
\end{tikzpicture}
\end{document}

• None of the solutions to the linked question show an example proving that it works even if the point is on the line. But I believe that (at least) the tkz-euclide one should work. Also, the fact that the point is on the line opens the door for different solutions, so if the current question is closed as duplicate, it may be worth adding an answer for this particular case to the linked question. – T. Verron Sep 12 '14 at 12:50
• @t.verron: ah yes, my bad – Jake Sep 12 '14 at 12:53
• – Jake Sep 12 '14 at 12:55
• @Jake: But wasn't it still related? Relinking: tex.stackexchange.com/q/19348/9517 – T. Verron Sep 12 '14 at 13:05
• Mathematically, the only conceptual difference is that your X (their P) is on the line. I'm not sure that all solutions in the other question apply here (not without some tweak at least), but some of them (for example the tkz-euclide one) have a fairly good chance of working in your situation. Also, "Related" links (afaik) are only about hinting future readers of the question towards similar problem (by filling the "Linked" section in the right column), so unless there is really nothing in common between the questions, I see no reason for being shy in posting them. – T. Verron Sep 12 '14 at 14:29

If one has content shown at X, playing around with inner and outer sep-values is needed. Or use .center (thanks to Jake).

\documentclass{standalone}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}

\usepackage{
tikz,
}

\usetikzlibrary{
intersections,
calc
}

\begin{document}
\begin{tikzpicture}[font=\small]
\draw
(0,0) coordinate (A)
(1,1.435) coordinate (B)
(3,3) coordinate (C)
;
\draw[thick] (A) rectangle (C);
\draw[thick] (A)--(B) node[pos=0.6, inner sep=0pt, outer sep=0pt](x){x};
\draw[thick] ($(x)!0.5cm!90:(A)$) -- ($(x)!0.5cm!-90:(A)$);
\end{tikzpicture}
\end{document}

• @Jake Oh, right... well, the little bits. If one could just remember all of it. :) – henry Sep 12 '14 at 13:09
• For consistency (and cleaner code), I'd place (x) as a coordinate on the path (a node with no text and no size), and use this (x) as a reference both for the perpendicular line and for the "X" text. For example, if you decide later that you prefer anchoring the "X" text on an edge to improve lisibility, you won't want the line to move. – T. Verron Sep 12 '14 at 13:10

These basic geometry constructions are easy in Metapost once you have got the hang of the declarative equations. Here's my attempt at the canonical plain Metapost approach, explained at the end of Chapter 9 of The Metafont Book.

prologues := 3;
outputtemplate := "%j%c.eps";

beginfig(1);

% some arbitrary points
z1 = (20,80);
z2 = (90,30);
z3 = (60,80);

% a point somewhere on the line through z1 and z2;
z4 = whatever[z1,z2];

% the direction of z3 from z4 is at right angles to that of z2 from z1
z3-z4 = whatever*(z2-z1) rotated 90;

% mark the right angle neatly
draw unitsquare scaled 5 rotated angle (z2-z1) shifted z4 withcolor .7 white;

draw z1--z2;
draw z3--z4 withcolor .67 red;

dotlabel.top("A",z1);
dotlabel.bot("B",z2);
dotlabel.rt("X",z3);
endfig;
end.


The whatever feature provides an arbitrary anonymous numeric variable. These variables can take any real value, and either sign, so in this case the order of the subtractions does not matter, and point X can be on either side of the line AB.

There is a rich source of geometry material for Metapost (in French) on the Syracuse site.

A faster solution with PSTricks just for fun.

\documentclass[pstricks,border=12pt,12pt]{standalone}
\usepackage{pst-eucl}

\begin{document}
\begin{pspicture}(8,8)
\pstGeonode[CurveType=polyline](0,6){A}(6,0){B}([nodesep=4]{B}A){X}
\pcline[linecolor=blue]([offset=1]{A}X)([offset=4]{B}X)
\end{pspicture}
\end{document}


• Interesting. Not "against" PSTricks but I did not ask for it. Also... are you really 13? – henry Sep 12 '14 at 13:05
• @henry: Nobody knows. :-) – kiss my armpit Sep 12 '14 at 13:06

With dirty tikz and a transformation:

\documentclass{standalone}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}

\usepackage{
tikz,
xspace
}

\usetikzlibrary{
intersections,
calc
}

\begin{document}
\begin{tikzpicture}[font=\small]
\draw
(0,0) coordinate (A)
(1,1.435) coordinate (B)
(3,3) coordinate (C)
;
\draw[thick] (A) rectangle (C);
\draw[thick] (A)--(B) node[pos=0.6] (x) {x};

\draw[red,thick,transform canvas={rotate around={90:(x)}}] (A) -- (B) ;
\end{tikzpicture}
\end{document}


You prepare a line from A to B, and then (before drawing it) you rotate it 90° around X. Of course, if you want a different length, you can place coordinates accordingly on your path from A to B.

Apparently, using transform canvas is necessary if the coordinates are given with node names, it is not, however, if the coordinates are given directly. The manual says that transform canvas is evil, but hardcoding coordinates is also evil, so...

• Hm, lots of the coordinates in my big graphic build upon one another. So I suppose it's only half-evil. :) (Seriously though, why is it all "evil"?) – henry Sep 12 '14 at 13:06
• @henry: TikZ loses track of the bounding box if you use transform canvas, so it really is quite evil – Jake Sep 12 '14 at 13:08

This solution slopes the middle node and uses it's north and south anchors as points forming the perpendicular line. Negative shorten <|> make line (red line in figure) as longer as needed. This trick also works with empty and not drawn nodes (green line in figure). It doesn't work with coordinate nodes.

\documentclass[border=2mm,tikz]{standalone}

\begin{document}
\begin{tikzpicture}

\draw (0,0) -- (3,2) node[pos=0.3, sloped, draw] (x) {X} node[pos=0.7,sloped] (y){};
\draw[ shorten >=-1cm, shorten <=-0.5cm, red] (x.south)--(x.north);
\draw[ shorten >=-0.5cm, shorten <=-1cm, green] (y.south)--(y.north);
\end{tikzpicture}
\end{document}


• Oh, that was a really interesting bit! Learned something new once again. If anyone is wondering, sloped is explained on page 238 in the manual (type texdoc tikz in the terminal). – henry Apr 17 '15 at 8:09