如何将 Tikz 代码放入序言中

如何将 Tikz 代码放入序言中

我有以下 MWE,基于在此站点上找到的答案来绘制切线,然后将切线延伸至轴:

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

我希望在序言中定义切线的代码部分,这样我写代码时会更简洁、更简短。具体来说,这部分:

    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

我怎样才能做到这一点?

答案1

正如@dalief 在评论中所说,您只需将这些行移到\tikzset{...}序言中的声明中:

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

为了更好的衡量,我还添加了every node/.append style={font=\footnotesize}环境选项tikzpicture以应用于\footnotesize所有节点文本。当然,这也可以添加到语句中——如果您想这样做,请注意,您需要用逗号\tikzset{...}分隔里面的所有不同样式等。\tikzset{...}

相关内容