使 tikzpicture 环境中定义的命令(在示例中)在所有文档中可用的正确方法是什么?

使 tikzpicture 环境中定义的命令(在示例中)在所有文档中可用的正确方法是什么?

精彩答案针对这个问题TikZ 中的简单曲线共享一些在环境内部定义的代码tikzpicture。我在这里分享答案的相关部分:


这里是 plothandler 的一个稍微修改过的版本,它允许你使用 TikZ 键first support={<point>}和指定第一个和最后一个支撑点last support={<point>},其中可以是<point>任何 TikZ 坐标表达式,例如(1,2),,,(感谢 Andrew Stacey 对(1cm,2pt)(A.south west)([xshift=1cm] A.south west)提取 TikZ 中任意点的 x,y 坐标)。

absolute first support默认情况下,这些点被认为是相对于路径的第一个/最后一个点的坐标。您可以使用、absolute last support或键指定支撑点为绝对坐标absolute supports

 \documentclass{article}

\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,shapes.misc}

\begin{document}
\begin{tikzpicture}
\tikzset{
    show curve controls/.style={
        decoration={
            show path construction,
            curveto code={
                \draw [blue, dashed]
                    (\tikzinputsegmentfirst) -- (\tikzinputsegmentsupporta)
                    node [at end, cross out, draw, solid, red, inner sep=2pt]{};
                \draw [blue, dashed]
                    (\tikzinputsegmentsupportb) -- (\tikzinputsegmentlast)
                    node [at start, cross out, draw, solid, red, inner sep=2pt]{};
            }
        }, decorate
    }
}

\makeatletter
\newcommand{\gettikzxy}[3]{%
  \tikz@scan@one@point\pgfutil@firstofone#1\relax
  \edef#2{\the\pgf@x}%
  \edef#3{\the\pgf@y}%
}

\newif\iffirstsupportabsolute
\newif\iflastsupportabsolute

\tikzset{
    absolute first support/.is if=firstsupportabsolute,
    absolute first support=false,
    absolute last support/.is if=lastsupportabsolute,
    absolute last support=false,
    absolute supports/.style={
        absolute first support=#1,
        absolute last support=#1
    },
    first support/.code={
        \gettikzxy{#1}{\pgf@plot@firstsupportrelx}{\pgf@plot@firstsupportrely}
    },
    first support={(0pt,0pt)},
    last support/.code={
        \gettikzxy{#1}{\pgf@plot@lastsupportrelx}{\pgf@plot@lastsupportrely}
    },
    last support={(0pt,0pt)}
}

\def\pgf@plot@curveto@handler@initial#1{%
  \pgf@process{#1}%
  \pgf@xa=\pgf@x%
  \pgf@ya=\pgf@y%
  \pgf@plot@first@action{\pgfqpoint{\pgf@xa}{\pgf@ya}}%
  \xdef\pgf@plot@curveto@first{\noexpand\pgfqpoint{\the\pgf@xa}{\the\pgf@ya}}%
  \iffirstsupportabsolute
    \pgf@xa=\pgf@plot@firstsupportrelx%
    \pgf@ya=\pgf@plot@firstsupportrely%
  \else
    \advance\pgf@xa by\pgf@plot@firstsupportrelx%
    \advance\pgf@ya by\pgf@plot@firstsupportrely%
  \fi
  \xdef\pgf@plot@curveto@firstsupport{\noexpand\pgfqpoint{\the\pgf@xa}{\the\pgf@ya}}%
  \global\let\pgf@plot@curveto@first@support=\pgf@plot@curveto@firstsupport%
  \global\let\pgf@plotstreampoint=\pgf@plot@curveto@handler@second%
}

\def\pgf@plot@curveto@handler@finish{%
  \ifpgf@plot@started%
    \pgf@process{\pgf@plot@curveto@second}
    \pgf@xa=\pgf@x%
    \pgf@ya=\pgf@y%
    \iflastsupportabsolute
      \pgf@xa=\pgf@plot@lastsupportrelx%
      \pgf@ya=\pgf@plot@lastsupportrely%
    \else
      \advance\pgf@xa by\pgf@plot@lastsupportrelx%
      \advance\pgf@ya by\pgf@plot@lastsupportrely%
    \fi
    \pgfpathcurveto{\pgf@plot@curveto@first@support}{\pgfqpoint{\the\pgf@xa}{\the\pgf@ya}}{\pgf@plot@curveto@second}%
  \fi%
}
\makeatother

\coordinate (A) at (2,-1);

\draw [gray!50]  (-1,-0.5) -- (1.5,1) -- (3,0);
\draw [
    cyan,
    postaction=show curve controls
] plot [
    smooth, tension=2,
    absolute supports,
    first support={(A)},
    last support={(A)}] coordinates { (-1,-0.5) (1.5,1) (3,0)};

\draw [
    yshift=-3cm,
    magenta,
    postaction=show curve controls
] plot [
    smooth, tension=2,
    first support={(-0.5cm,1cm)},
    last support={(0.5cm,1cm)}] coordinates { (-1,-0.5) (1.5,1) (3,0)};

\end{tikzpicture}
\end{document}

我想使\makeatletter和之间定义的命令\makeatother可用于我的所有文档。正确的做法是什么?

相关内容