0

is there a syntax that allows using a line as a node in tikz? In the following example:

\documentclass{standalone}  
\usepackage{tikz}

\begin{document} \begin{tikzpicture} %dummy nodes \node (A) at (0,0) {}; \node (B) at (5,0) {}; \node (C) at (2,-5) {}; \draw (A)--(B); \draw (C)--(2,0); \end{tikzpicture} \end{document}

enter image description here

I'd like to write something such as \draw (C)--[(A)--(B)] instead of \draw (C)--(2,0). Of course in this example I know the nodes' coordinates explicitly, but in my actual diagram I don't, and whenever I add something that changes the size of it, I have to trial-and-error adjust the coordinate of the explicit node.

Thanks

Mogu
  • 321
  • Could you please explain better what is your problem? – vi pa Sep 02 '20 at 11:58
  • Yes: I would like to draw a line from the node C to its orthogonal projection on the line (A)--(B), and I was hoping that there were a straight forward synthax to accomplish that. – Mogu Sep 02 '20 at 12:06
  • 1
    Yes the syntax can be found on this site by searching for "tikz orthogonal line" e.g. here: https://tex.stackexchange.com/a/19349/8650 – hpekristiansen Sep 02 '20 at 12:08
  • @hpekristiansen : Yes, that works as well. I prefer muzimuzhi's solution in the present case, but this will come in handy. (didn't search deeply enough apparently) – Mogu Sep 02 '20 at 12:26
  • tkz euclide has built in ortho support -- see here -- https://tex.stackexchange.com/a/559266/197451 – js bibra Sep 02 '20 at 13:43

1 Answers1

2

Option local bounding box=<node name> is what you need here.

\documentclass{standalone}  
\usepackage{tikz}

\begin{document} \begin{tikzpicture} %dummy nodes \node (A) at (0,0) {}; \node (B) at (5,0) {}; \node (C) at (2,-5) {}; % this creates a rectangle node named AB \draw[local bounding box=AB] (A)--(B); \draw (C) |- (AB); \end{tikzpicture} \end{document}

Update: And a projection modifier version:

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

\begin{document} \begin{tikzpicture} %dummy nodes \node (A) at (0,0) {}; \node (B) at (5,0) {}; \node (C) at (2,-5) {}; \draw (A)--(B); % draw a line from node C to the projection of C on line A to B % see pgfmanual, sec. 13.5.5 The Syntax of Projection Modifiers \draw (C) |- ($(A)!(C)!(B)$); \end{tikzpicture} \end{document}

muzimuzhi Z
  • 26,474
  • Thanks a lot, works very well. I might add that this solution produces a path between A,B and C, which is fine given the question that I asked. But this will look messy with dashed or dotted lines (which was in fact my purpose), and so - I as sensed - hpekristiansen's link ended up being useful here (solution that I went for in the end). Hope that helps future readers. – Mogu Sep 02 '20 at 13:10
  • I meant "an L-shaped path between A, C's projection on AB, and C."
  • – Mogu Sep 02 '20 at 13:31
  • @Mogu To make my answer more helpful for future readers, I've added another example using projection as mentioned by hpekristiansen. Thanks for your comments. – muzimuzhi Z Sep 02 '20 at 15:51