2

Edit:

Solution has been found using the syntax I borrowed. It just involves changing the code pertaining to the horizontal line to \draw[dashed, line width=0.5mm] (0,0|-A) -- (A);. It's a simple syntax, but I'm not sure it's the most effective. Thus, I think it's appropriate to redefine the question: What's the "correct way" to align lines from the x- and y-axis to an intersection point?

I've produced the following output: enter image description here

I successfully managed to make the horizontal dashed line be placed exactly below the intersection point thanks to a line of code from this post: https://tex.stackexchange.com/a/572190/318574

That answer didn't provide any insight as to how you can make a horizontally aligned line to the intersection point from the y-axis, however, and I tried to modify his code but only managed to get a diagonally dashed line with 2 error codes. Here's the MWE:

\documentclass[12pt]{article}

\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}

\begin{tikzpicture}
    % Grid and axis setup
    \draw[help lines] (-0.9,-0.9) grid (9.9,9.9);
    \draw[->, line width=0.5mm] (-1,0) -- (10,0) node[below]{x-axis};
    \draw[->, line width=0.5mm] (0,-1) -- (0,10) node[below left]{y-axis};
    % Graphs and intersection point
    \draw[line width=0.5mm, red, name path = red line] (0,2) -- (8,7);
    \draw[line width=0.5mm, blue, name path = blue line] (0,5) -- (9,3); 
    \fill[name intersections={of= red line and blue line}]
        (intersection-1) circle (3pt) node(A) {}; 
    % Vertical dashed line
    \draw[dashed, line width=0.5mm]
        (A|-0,0) -- (A);
    % Failed horizontal dashed line
    \draw[dashed, line width=0.5mm]
        (0,A) -- (A);
\end{tikzpicture}

\end{document}

Solutions for how to align the dashed lines horizontally as well as explaining how the syntax works (I didn't understand all of the complex code in the referred question, just copy and pasted what I suspected would work) is greatly appreciated!

4
  • 1
    With your logic (there are other ways of doing this), just write \draw[dashed, line width=0.5mm](0,0|-A) -- (A);
    – AndréC
    Commented May 24 at 20:05
  • I see, thank you very much. This isn't my logic, however, because I copied that syntax from the answer which I referenced (Hence why I couldn't figure out this simple adjustment for the horizontal syntax). I could imagine this syntax is one of the easier ways of doing it, however, since there's minimal code involved, but is it optimal? Is there a "right way" of doing it which is more authentic?
    – Atex
    Commented May 24 at 20:15
  • -| and |- are probably the "correct way" to align lines from the x- and y-axis, see the manual, section 14.2.2 Horizontal and Vertical Lines.
    – jlab
    Commented May 24 at 20:36
  • Are you sure those definitions of |- are applicable in this context? Based on that, I interpret the command for the vertical dashed line in my example as the following: "Start from A, then travel vertically and then horizontally from A to (0,0) and then go to the position of A from there". Based on that, I understand it as if we manipulate the starting coordinates such that we start in A (so we can get the value for the x-axis correct) and then pin the x-coordinate to that value and then set the y-coordinate to zero, which is also what I intended, but it seems like a funny way to get it
    – Atex
    Commented May 24 at 21:26

1 Answer 1

5

The intersection of straight lines has another syntax, made explicit in version 1.18 of Tikz. This syntax is no longer documented in the latest manuals, but remains accessible. It has the advantage of calculating coordinates even if the intersection is not constructed on the figure, which is not the case for line intersection in later versions of Tikz!

See here Tikz manual 1.18

\documentclass[12pt]{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
    % Grid and axis setup
    \draw[help lines] (-0.9,-0.9) grid (9.9,9.9);
    \draw[->, line width=0.5mm] (-1,0) -- (10,0) node[below]{x-axis};
    \draw[->, line width=0.5mm] (0,-1) -- (0,10) node[below left]{y-axis};
    % Graphs and intersection point
    \draw[line width=0.5mm, red] (0,2) -- (8,7);
    \draw[line width=0.5mm, blue] (0,5) -- (9,3); 
    \fill  (intersection of 0,2--8,7 and 0,5--9,3) circle (3pt) node(A) {}; 
    % Dashed line
    \draw[dashed, line width=0.5mm] (A|-0,0) -- (A)--(0,0|-A);
\end{tikzpicture}

\end{document}

enter image description here

4
  • Wow, you even managed to make it work without the additional usage of the intersections library! Is it still possible to name the paths and call them by their names instead of entering their coordinates, though? Also, it seems like the syntax for intersection of straight lines you provided isn't just an alternative, but a better solution, too. Any idea why TikZ wouldn't document this solution in their newer manuals?
    – Atex
    Commented May 24 at 21:16
  • No, paths cannot be named as in later versions of Tikz. However, if points are named (A), (B), etc, you can write (intersection of A--B and C--D)
    – AndréC
    Commented May 24 at 21:32
  • I see, then that'd involve having to create 4 separate coordinates, which is not so convenient. So, naming paths and referencing them by it is only possible with the intersections library? Are there any potential downsides using this apart from loading an additional library?
    – Atex
    Commented May 25 at 7:36
  • 1
    To name paths, it's necessary to use the intersections library, but it's only really useful for intersections of curved lines. For the intersection of straight lines, the original syntax is simpler and more practical: for example, it will calculate (intersection of 0,0--5,0 and 1,1--1,2) even though the paths of these two paths do not intersect on the figure. In this case, the intersections library will say that these two paths do not intersect. On the contrary, it's very practical to name points with their coordinate or node. This simplifies the coding considerably and makes it easy to read
    – AndréC
    Commented May 25 at 8:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .