3

I have the following MWE, building on answers found on this site for drawing a tangent line and then extending the tangent line till the axes:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{calc,intersections,through,backgrounds,matrix,patterns}
\usepackage[normalem]{ulem}
\usetikzlibrary{decorations.pathreplacing}


\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{document}

    \begin{tikzpicture}[scale = 1.2, 
    tangent/.style={
        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
    ]
    \footnotesize   

    \draw[<->] (0,5) node[above]{$y$}--(0,0)--(5,0) node[right]{$x$};

    \draw[name path = I1, tangent=0.5] (0.2,4) to [out=-85,in=180] (4.5,0.5) node[right] {IC$_3$};


    \draw[mark tangent intersections with axes, red] 
    (x-intersection-1) -- (y-intersection-1);


    \end{tikzpicture}


\end{document}

I'd like to have the section of code defining a tangent line in the preamble so the code is neater and shorter when I'm writing. Specifically, this bit:

    tangent/.style={
            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

How can I do this?

2
  • 2
    You are already doing it, add a new style to \tikzset and give it that code as it contents and when you need this code, just give it the name of the style you made
    – daleif
    Sep 12, 2018 at 7:36
  • I tried doing that but couldn't get it to work. I assumed I was going about it wrong.
    – Thev
    Sep 12, 2018 at 7:39

1 Answer 1

5

As @dalief days in the comments, you just need to move the lines into the\tikzset{...} statement in the preamble:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{calc,intersections,through,backgrounds,matrix,patterns}
\usepackage[normalem]{ulem}
\usetikzlibrary{decorations.pathreplacing}


\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,
    tangent/.style={
        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,
}

\begin{document}

    \begin{tikzpicture}[scale = 1.2, every node/.append style={font=\footnotesize}]
      \draw[<->] (0,5) node[above]{$y$}--(0,0)--(5,0) node[right]{$x$};
      \draw[name path = I1, tangent=0.5] (0.2,4) to [out=-85,in=180] (4.5,0.5) node[right] {IC$_3$};
      \draw[mark tangent intersections with axes, red] (x-intersection-1) -- (y-intersection-1);
    \end{tikzpicture}

\end{document}

For good measure I have also added every node/.append style={font=\footnotesize} to the options for the tikzpicture environment to apply \footnotesize to all node text. This could, of course, also be added to the \tikzset{...} statement -- if you want to do this then note that you need to separate all of the different styles etc inside \tikzset{...} with commas.

You must log in to answer this question.

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