“描边”预定义路径

“描边”预定义路径

我想使用之前定义的路径的名称,以便稍后在代码中绘制它。下面我提供了一个示例,其中我定义了两个路径“D1”和“S1”,然后稍后绘制这些完全相同的路径。在示例中,我使用基本相同的代码绘制路径,这是多余的。有没有办法记住路径的名称并使用之前定义的路径绘制一条线?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}

\begin{tikzpicture}[yscale=3,xscale=3]

%Pre-Define Paths

\path[name path=D1] (0,1.8) -- (1.8,0);
\path[name path=S1] (0,0) -- (1.8,1.8);
\path[name intersections={of=D1 and S1,by=EQ}];

%axis

\draw[->,thick] (0,0) -- (3,0) node[right,fontscale=0] {Quantity};
\draw[->,thick] (0,0) -- (0,2) node[left,fontscale=0] {Price};

%Initial Equilibrium

\fill[black] (EQ) circle (.5pt) node[right] {$EQ$};
\draw[blue] (0,1.8) -- (1.8,0);
\draw[red] (0,0) -- (1.8,1.8);


\end{tikzpicture}

\end{document}

答案1

您可以使用未记录的save path键,该键在宏中以非常低的级别保存整个路径。您可以再次使用此路径,并将use path键定义为

\makeatletter
\tikzset{use path/.code=\tikz@addmode{\pgfsyssoftpath@setcurrentpath#1}}
\makeatother

这里不应该使用任何路径运算符。

如果您想要保存路径的一部分并希望稍后使用它们(也可能仅作为一部分),则可以使用saveuse path以下键我的另一个答案定义为

\tikzset{
  saveuse path/.code 2 args={
    \pgfkeysalso{#1/.estyle={insert path={#2}}}%
    \global\expandafter\let\csname pgfk@\pgfkeyscurrentpath/.@cmd\expandafter\endcsname % not optimal as it is now global through out the document
                           \csname pgfk@\pgfkeyscurrentpath/.@cmd\endcsname
    \pgfkeysalso{#1}%
  }
}

saveuse path键将路径保存为insert path键(并使用它),以后可以用于相同的路径(只要您不更改转换矩阵)。

代码A

\documentclass[tikz,convert=false]{standalone}
\usetikzlibrary{intersections}
\makeatletter
\tikzset{use path/.code=\tikz@addmode{\pgfsyssoftpath@setcurrentpath#1}}
\makeatother
\begin{document}
\begin{tikzpicture}[yscale=3,xscale=3]

%Pre-Define Paths
\path[save path=\pathA,name path=D1] (0,1.8) -- (1.8,0);
\path[save path=\pathB,name path=S1] (0,0) -- (1.8,1.8);
\path[name intersections={of=D1 and S1,by=EQ}];

%axis
\draw[->,thick] (0,0) -- (3,0) node[right] {Quantity};
\draw[->,thick] (0,0) -- (0,2) node[left] {Price};

%Initial Equilibrium
\fill[black] (EQ) circle (.5pt) node[right] {$EQ$};
\draw[blue][use path=\pathA];
\draw[red] [use path=\pathB];
\end{tikzpicture}
\end{document}

代码 B

\documentclass[tikz,convert=false]{standalone}
\usetikzlibrary{intersections}
\tikzset{
  saveuse path/.code 2 args={
    \pgfkeysalso{#1/.estyle={insert path={#2}}}%
    \global\expandafter\let\csname pgfk@\pgfkeyscurrentpath/.@cmd\expandafter\endcsname % not optimal as it is now global through out the document
                           \csname pgfk@\pgfkeyscurrentpath/.@cmd\endcsname
    \pgfkeysalso{#1}%
  }
}
\begin{document}
\begin{tikzpicture}[yscale=3,xscale=3]

%Pre-Define Paths
\path[name path=D1] [saveuse path={pathA}{(0,1.8) -- (1.8,0)}];
\path[name path=S1] [saveuse path={pathB}{(0,0) -- (1.8,1.8)}];
\path[name intersections={of=D1 and S1,by=EQ}];

%axis
\draw[->,thick] (0,0) -- (3,0) node[right] {Quantity};
\draw[->,thick] (0,0) -- (0,2) node[left] {Price};

%Initial Equilibrium
\fill[black] (EQ) circle (.5pt) node[right] {$EQ$};
\draw[blue][pathA];
\draw[red] [pathB];
\end{tikzpicture}
\end{document}

答案2

一个简单的解决方案是将路径 D1 存储在变量 \D1 中,S1 也是如此。然后根据需要多次使用此变量。以下是示例:

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{enumerate}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{tikz-3dplot}
\usepackage{hyperref}
\usepackage{ifthen}
\usepackage{pgfplots}

\usetikzlibrary{calc,3d,shapes, pgfplots.external, intersections}
\pgfplotsset{compat=1.11} 

\begin{document}
\begin{tikzpicture}[yscale=3,xscale=3]

%Pre-Define Paths

  \def\D1{(0,1.8) -- (1.8,0)}
  \def\S1{(0,0) -- (1.8,1.8)}

\path[name path=D1] \D1;
\path[name path=S1] \S1;
\path[name intersections={of=D1 and S1,by=EQ}];

%axis

\draw[->,thick] (0,0) -- (3,0) node[right] {Quantity};
\draw[->,thick] (0,0) -- (0,2) node[left] {Price};

%Initial Equilibrium

\fill[black] (EQ) circle (.5pt) node[right] {$EQ$};
\draw[blue] \D1;
\draw[red] \S1;

\end{tikzpicture}
\end{document}

如下图所示:

在此处输入图片描述

相关内容