1

I used the following code to draw tangent lines from points a and o (on 2 curves starting from a certain point) to x and y axes.

The node at point strt is used to draw other curves and lines; so I need the 2 curves to start from it.

Is there a code that automates this drawing, without trying drawing lines from different points on the x and y axes till they pass through the designated points on the curves.

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\begin{document}
\begin{frame}[t]
\frametitle{tangent lines to x and y axes}
\begin{tikzpicture}[scale=1., transform shape]
\draw [thick] (0,0) -- (7,0);
\draw [thick] (0,0) -- (0,6);

\node at (2.5,2.) (strt){}; \draw [very thick, blue, looseness=.8] (strt.center) to [out=140,in=-80] node [pos=.3] (x){x} +(120.:3.cm) (strt.center) to [out=-40,in=170] node [pos=.25] (o){o} +(-20.:3.cm); \draw [thick, red] (3.8,0) -- (0,5.2); \draw [thick, red] (6.2,0) -- (0,3.2); \end{tikzpicture} \end{frame} \end{document}

enter image description here

Hany
  • 4,709
  • Do you know the tkz-euclide package? I think its perfect for your task. – Unknown May 14 '22 at 14:00
  • 1
    @Unknown no tkz-euclide is for Euclidean geometry but tkz-fct is a possibility. The only problem is the installation of gnuplot. – Alain Matthes May 14 '22 at 14:26
  • I don't really understand your problem (and neither your question). What do you have? What do you want? If you have a curve and want to draw a tangent to it passing through a given point belonging to the curve, you might try markings which comes with an adapted relative frame (i.e. tangent and normal vectors) to the curve at the point. If the point is not on the curve, the problem is difficult, in general, even from a computational point of view. – Daniel N May 14 '22 at 16:32
  • @Daniel N I want to draw tangents from different points on curves to be extended to the x any y axes. – Hany May 15 '22 at 03:54

3 Answers3

2

enter image description here

The answer is updated in accordance with Hany's remark, "I want the tangent at point 1 to end at y axis with a node containing some text".

There are two decorations tanget at and tangent vector at. The main one is the former and is used for the points 1 and 2. The later, used for 3, is simpler and indicates the sens in which TikZ goes along the curve.

Some comments concerning tanget at

  1. It has three arguments: #1 is a sub-unitary float which determines the point on the curve; #2 and #3 are multiplicative constants.
  2. It draws a tangent (segment) line at #1. Think about #1 as being a value of the time coordinate describing the curve with constant speed.
  3. The length of the tangent line is determined by the two multiplicative constants: #2 represents the length (in length units, cm by default) of the tangent semi-line in the negative direction. The same for #3, but in the positive one.
  4. As a side effect, tangent at sets the following names to points involved in the construction:
  • (point-k) the point on the curve defined by #1
  • (tpoint-k) the point on the tangent line such that (tpoint-k) - (point-k) is the unit tangent vector in the positive direction (see also tangent vector at)
  • (A-k) and (B-k) the extremities of the tangent line.

The integer k stands for the index of the point in the sequence of invocations of tangent at. In the above drawing k is 1 or 2.

Remark You can play with the arguemnts #2 and #3 to have the tangent line you want at the point #1. Then use (A-k) and/or (B-k) to insert the text you need. It might be easier to draw the point (A-k) before looking for the perfect value of #2.

The code

\documentclass[11pt, border=.4cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc, math}
\usetikzlibrary{decorations.markings}

\begin{document}

\tikzset{ tangent vector at/.style={ decoration={ markings, % switch on markings mark = at position #1 with { \filldraw[red] (0, 0) circle (1.5pt) ++(0, 1.5ex) node[scale=.8] {\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}}; \draw[red, ->] (0, 0) -- (1, 0); } }, postaction=decorate }, tangent at/.style args={#1;#2|#3}{% decoration={ markings, % switch on markings mark = at position #1 with { \path (0, 0) coordinate (point-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}); \path (1, 0) coordinate (tpoint-% \pgfkeysvalueof{/pgf/decoration/mark info/sequence number}); \filldraw[blue] (0, 0) circle (1.5pt) ++(0, 1.5ex) node[scale=.8] {\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}}; \draw[blue] (-#2, 0) coordinate (A-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) -- (#3, 0) coordinate (B-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}); } }, postaction=decorate }
}

\begin{tikzpicture}[every node/.style={scale=.8}] \begin{scope}[gray, very thin, ->] \draw (-1, 0) -- (5.5, 0) node[right] {$x$}; \draw (0, -1) -- (0, 3.5) node[above] {$y$}; \end{scope}

\draw[tangent at={.4; 1.92|2}, tangent at={.77; 1|1.1}, tangent vector at=.02] (-1, 1) .. controls +(2, 2.3) and +(3, 3) .. ++(4.5, -1.5); \filldraw (A-1) circle (1.5pt) node[above left] {Some text};

% a curve through the first point \draw[red] (point-1) to[out=210, in=90] ++(0, -2); \end{tikzpicture}

\end{document}

Daniel N
  • 5,687
  • @– Daniel N Thank you very much for your answer. But my question is to, also, extend the tangent lines to the x and y axes. – Hany May 16 '22 at 06:23
  • In the example that I'm giving, how do you want to draw the tangent at point 1? The intersection point with the Ox axis exceeds TikZ capacities (the x coordinate is too big). So want do you want at the end? – Daniel N May 16 '22 at 07:13
  • @– Daniel N I want the tangent at point1 to end at y axis with a node containing some text; and the tangent at point2 to end at x axis with a node containing some text. Thank you. – Hany May 16 '22 at 12:40
  • I modified the previous answer; the intersection point or points with the coordinate axes have names now and can be used afterwards. – Daniel N May 16 '22 at 19:57
  • Well, can you use the answer in its new form? – Daniel N May 17 '22 at 17:23
2

One trick (after searching, I think this is the origin of the trick) for drawing tangent lines is to draw a rectangular node with inner sep=0 and minimum height=0, so the rectangle is really a line segment. Using the sloped option will draw the line as a tangent line.

enter image description here

I set the minimum width to 8cm to ensure the tangent lines will cross the axes. To prevent the tangent lines from crossing the axes I clipped a rectangle in a scope.

If you want the x and o marks you can add them as additional nodes: \node at (x){x}; or \node[blue, above right] at (x){x};

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture} \begin{scope} \clip(0,0)rectangle(7,6); \node at (2.5,2.) (strt){}; \draw [very thick, blue, looseness=.8] (strt.center) to [out=140,in=-80] node [pos=.3, sloped, inner sep=0, minimum height=0, minimum width=8cm, draw=red] (x){} +(120.:3.cm) (strt.center) to [out=-40,in=170] node [pos=.25, sloped, inner sep=0, minimum height=0, minimum width=8cm, draw=red] (o){} +(-20.:3.cm); \end{scope} \draw [thick] (0,0) -- (7,0); \draw [thick] (0,0) -- (0,6); \end{tikzpicture}

\end{document}

Sandy G
  • 42,558
  • Thank you for your solution. Unfortunately, as I mentioned, this curve is a part of a drawing containing other lines and curves, so the clip solution will not work for the other components of the drawing. – Hany May 16 '22 at 06:21
0

With tzplot:

Draw tangent lines to a given curve:

enter image description here

\documentclass{standalone}

\usepackage{tzplot}

\begin{document}

\begin{tikzpicture} \tzhelplines(8,7) \tzaxes(8,7){$x$}{$y$} \tzto[thick,blue,bend right=35]"curve"(1,5)(6,1){$u_0$}[r]

% clip: tangent lines extended to the axes \begin{scope} \settztangentlayer{main} \clip (0,0) rectangle (8,7); \tztangentat[semithick,red]{curve}{2}[0:5] \tztangentat[semithick,red]{curve}{3}[0:6] \tztangentat[dashed]{curve}{4}[0:8] \end{scope}

\tzvXpointat*{curve}{2}(x) \tzvXpointat{curve}{3}(o) \tzdot(o) \end{tikzpicture}

\end{document}

I. Cho
  • 2,985
  • Thank you for your solution. Unfortunately, as I mentioned, this curve is a part of a drawing containing other lines and curves, so the clip solution will not work for the other components of the drawing. – Hany May 16 '22 at 06:21
  • @Hany https://tex.stackexchange.com/questions/433317/extending-a-tangent-line-until-x-and-y-axes/636946#636946 – I. Cho May 16 '22 at 09:40