2

I use this for draw tangents to an arbitrary plot. For example:

\documentclass[border=5mm]{standalone}  
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usetikzlibrary{intersections}

\makeatletter
\def\parsenode[#1]#2\pgf@nil{%
    \tikzset{label node/.style={#1}}
    \def\nodetext{#2}
}

\tikzset{
    add node at x/.style 2 args={
        name path global=plot line,
        /pgfplots/execute at end plot visualization/.append={
                \begingroup
                \@ifnextchar[{\parsenode}{\parsenode[]}#2\pgf@nil
            \path [name path global = position line #1-1]
                ({axis cs:#1,0}|-{rel axis cs:0,0}) --
                ({axis cs:#1,0}|-{rel axis cs:0,1});
            \path [xshift=1pt, name path global = position line #1-2]
                ({axis cs:#1,0}|-{rel axis cs:0,0}) --
                ({axis cs:#1,0}|-{rel axis cs:0,1});
            \path [
                name intersections={
                    of={plot line and position line #1-1},
                    name=left intersection
                },
                name intersections={
                    of={plot line and position line #1-2},
                    name=right intersection
                },
                label node/.append style={pos=1}
            ] (left intersection-1) -- (right intersection-1)
            node [label node]{\nodetext};
            \endgroup
        }
    }
}
\makeatother

\begin{document}

    \begin{tikzpicture}[>=latex]
    \begin{axis}[
    grid,
    axis x line=center,
    axis y line=center,
    xtick={-5,-4,...,5},
    ytick={-5,-4,...,5},
    xlabel={$x$},
    ylabel={$y$},
    xlabel style={below right},
    ylabel style={above left},
    xmin=-5.5,
    xmax=5.5,
    ymin=-5.5,
    ymax=5.5,
    tangent/.style={
        add node at x={2}{
            [
            sloped, 
            append after command={(\tikzlastnode.west) edge [thick,black] (\tikzlastnode.east)},
            minimum width=0.2\textwidth
            ]
        }      
    }]
        \addplot[color=black,smooth, tangent] coordinates {
        (-5,-5)
        (-3,-4)
        (-2,-1)
        (0,0)
        (1,2)
        (2,3)
        (3,3)
        (4,1)
    };
    \end{axis}
    \end{tikzpicture}

\end{document}

enter image description here

The problem is that I want more tangents, not only one. For example one more at the point (-3,f(-3)). Is it possible?

I tried some solutions, but I got only errors. Maybe I don't understand Jake's code...

Thank's in advanced!!!

Henri Menke
  • 109,596

1 Answers1

3

Like this? I slightly modified tangent/.style such that it makes the position its argument and then you can use the list key handler to do

 tangent/.list={2,3}

MWE:

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}

\makeatletter
\def\parsenode[#1]#2\pgf@nil{%
    \tikzset{label node/.style={#1}}
    \def\nodetext{#2}
}

\tikzset{
    add node at x/.style 2 args={
        name path global=plot line,
        /pgfplots/execute at end plot visualization/.append={
                \begingroup
                \@ifnextchar[{\parsenode}{\parsenode[]}#2\pgf@nil
            \path [name path global = position line #1-1]
                ({axis cs:#1,0}|-{rel axis cs:0,0}) --
                ({axis cs:#1,0}|-{rel axis cs:0,1});
            \path [xshift=1pt, name path global = position line #1-2]
                ({axis cs:#1,0}|-{rel axis cs:0,0}) --
                ({axis cs:#1,0}|-{rel axis cs:0,1});
            \path [
                name intersections={
                    of={plot line and position line #1-1},
                    name=left intersection
                },
                name intersections={
                    of={plot line and position line #1-2},
                    name=right intersection
                },
                label node/.append style={pos=1}
            ] (left intersection-1) -- (right intersection-1)
            node [label node]{\nodetext};
            \endgroup
        }
    }
}
\makeatother

\begin{document}

    \begin{tikzpicture}[>=latex]
    \begin{axis}[
    grid,
    axis x line=center,
    axis y line=center,
    xtick={-5,-4,...,5},
    ytick={-5,-4,...,5},
    xlabel={$x$},
    ylabel={$y$},
    xlabel style={below right},
    ylabel style={above left},
    xmin=-5.5,
    xmax=5.5,
    ymin=-5.5,
    ymax=5.5,
    tangent/.style={
        add node at x={#1}{
            [
            sloped, 
            append after command={(\tikzlastnode.west) edge [thick,black] (\tikzlastnode.east)},
            minimum width=0.2\textwidth
            ]
        }      
    }]
        \addplot[color=black,smooth, tangent/.list={2,3}] coordinates {
        (-5,-5)
        (-3,-4)
        (-2,-1)
        (0,0)
        (1,2)
        (2,3)
        (3,3)
        (4,1)
    };
    \end{axis}
    \end{tikzpicture}

\end{document}

enter image description here