6

I want to draw a graph that contains the following elements:

  1. A downward-sloping curve in the northeast quadrant of a 2d Cartesian plane;
  2. a line tangent to an arbitrary point on this curve; and
  3. the tangent line extends to touch (but not cross) the x and y axes.

I managed to get points 1 and 2 done, but 3 cannot be done without a lot of manual adjustments.

Question

Is there a more efficient way to extend the tangent line (the blue line in my MWE) to both axes? I was thinking, e.g. perhaps the vertical intercept could be determined through some version of the (tangent point -| 0,0) syntax?

MWE

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}[
    tangent/.style={ % https://tex.stackexchange.com/a/25940/18228
        decoration={
            markings,% switch on markings
            mark=
                at position #1
                with
                {
                    \coordinate (tangent point-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,0pt);
                    \coordinate (tangent unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (1,0pt);
                    \coordinate (tangent orthogonal unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,1);
                }
        },
        postaction=decorate
    },
    use tangent/.style={
        shift=(tangent point-#1),
        x=(tangent unit vector-#1),
        y=(tangent orthogonal unit vector-#1)
    },
    use tangent/.default=1
]
\draw[very thick,tangent=.4](.5,4)to[bend right=35](5.5,.5);
\draw[use tangent,blue](-2,0)--(2,0); % Better way to draw this line?
\draw[<->](0,5)node[left]{$y$}--(0,0)--(7,0)node[below]{$x$};

\end{tikzpicture}
\end{document}

Output

enter image description here

  • I think some tikz beginner tutorial does something like that. Is tikz-minimal? It shows how to calculate the end points of lines (values of sine and cosine) and has a diagonal that intercepts with the tan of a circle. Both lines stop at the intercept. I don't know if the intercept is calculated or if both lines are clipped at the other one's respective value. Might you know which tutorial I mean? Check that out ;) – thymaro May 25 '18 at 5:22
6

There is an almost trivial possibility: extend the tangent by some decent amount and clip it.

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings,calc}
\begin{document}
\begin{tikzpicture}[
    tangent/.style={ % https://tex.stackexchange.com/a/25940/18228
        decoration={
            markings,% switch on markings
            mark=
                at position #1
                with
                {
                    \coordinate (tangent point-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,0pt);
                    \coordinate (tangent unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (1,0pt);
                    \coordinate (tangent orthogonal unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,1);
                }
        },
        postaction=decorate
    },
    use tangent/.style={
        shift=(tangent point-#1),
        x=(tangent unit vector-#1),
        y=(tangent orthogonal unit vector-#1)
    },
    use tangent/.default=1
]
\draw[very thick,tangent=.4](.5,4)to[bend right=35](5.5,.5);
\begin{scope}[overlay]
\clip(0,0) rectangle (10,10);
\draw[use tangent,blue](-3,0)--(3,0); % Better way to draw this line?
\end{scope}
\draw[<->](0,5)node[left]{$y$}--(0,0)--(7,0)node[below]{$x$};

\end{tikzpicture}
\end{document}

enter image description here

Of course, there will be more elaborate solutions which pretend to be more elegant. ;-)

You do not even need intersections to find the intersections with the axes.

\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings,calc}
\begin{document}
\tikzset{mark tangent intersections with axes/.code={
\path let \p1=(tangent point-#1), 
\p2=($(tangent unit vector-#1)-(tangent point-#1)$)
in
({\x1-\y1*\x2/\y2},0) coordinate (x-intersection-#1) 
(0,{\y1-\x1*\y2/\x2}) coordinate (y-intersection-#1);},
mark tangent intersections with axes/.default=1
}
\begin{tikzpicture}[
    tangent/.style={ % https://tex.stackexchange.com/a/25940/18228
        decoration={
            markings,% switch on markings
            mark=
                at position #1
                with
                {
                    \coordinate (tangent point-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,0pt);
                    \coordinate (tangent unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (1,0pt);
                    \coordinate (tangent orthogonal unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,1);
                }
        },
        postaction=decorate
    },
    use tangent/.style={
        shift=(tangent point-#1),
        x=(tangent unit vector-#1),
        y=(tangent orthogonal unit vector-#1)
    },
    use tangent/.default=1
]
\draw[very thick,tangent=.4](.5,4)to[bend right=35](5.5,.5);

\draw[mark tangent intersections with axes,blue] 
(x-intersection-1) -- (y-intersection-1);
\draw[<->](0,5)node[left]{$y$}--(0,0)--(7,0)node[below]{$x$};

\end{tikzpicture}
\end{document}

enter image description here

If you have more than one tangent, use \draw[mark tangent intersections with axes=2,... and so on, the respective intersection points will be called (x-intersection-2), (y-intersection-2) etc. Notice that this code will fail if the tangent is horizontal or vertical, so would intersections. One could add if statements checking if \y2 or \x2 is zero in case you want to make it fool proof(er).

  • Thanks for the quick response. Yes, clip is one way to go. I preferred calculating the intercepts directly because I wanted to allow for the possibility of adding labels/nodes beside the intercepts. I guess clip can be used together with the intersections library to achieve the same effect. So maybe clip is the most efficient way to go. – Herr K. May 25 '18 at 4:57
  • @HerrK. You do not even need intersections for this. I wrote a quick code. – user121799 May 25 '18 at 5:22
  • @marmot Excellent and....elegant. +1 – Sebastiano May 25 '18 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.