可重复使用的 Tikz 图片

可重复使用的 Tikz 图片

我有一张经常重复使用的 tikz 图片。我想创建一个函数来进行计算并绘制图形。

\documentclass[preview]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

%% Create the General Function
\tikzset{
declare function={

EQUILIBRIUM(\DINT, \DSLP, \SINT, \SSLP)=

    %Define linear parameters for supply and demand
        \def\dint{\DINT}           %Y-intercept for DEMAND.
        \def\dslp{\DSLP}          %Slope for DEMAND.
        \def\sint{\SINT}          %Y-intercept for SUPPLY.
        \def\sslp{\SSLP}          %Slope for SUPPLY.

    % Define equilibrium points
        \def\qeq{ {(\sint-\dint)/(\dslp-\sslp) } }
        \def\peq{ {(\sint-\dint)/(\dslp-\sslp)*\sslp+\sint} }

    % Define equilibrium coordinates.
        \coordinate (ints)  at  ( \qeq, \peq);
        \coordinate (ep)    at  ( 0, \peq);
        \coordinate (eq)    at  ( \qeq, 0);
        \coordinate (dint)  at  ( 0, \dint);
        \coordinate (sint)  at  ( 0, \sint);

    % DEMAND
        \def\demand{\x,{\dslp*\x+\dint}}
        \draw[thick,color=blue] plot (\demand) node[right] {Demand}; %{$P(q) = -\frac{1}{2}q+\frac{9}{2}$};

    % SUPPLY
        \def\supply{\x,{\sslp*\x+\sint}}
        \draw[thick,color=purple] plot (\supply) node[right] {Supply};

    % Draw axes
        \draw[->] (0,0) -- (6.2,0) node[right] {$Q$};
        \draw[->] (0,0) -- (0,6.2) node[above] {$P$};

    % Equilibirum Price
        \def\pp{\pgfmathparse{ \peq }\pgfmathprintnumber[precision=1] \pgfmathresult}
        \draw[thin] (ep) node[left]{$P*=\pp$}-- (ints);

    % Equilibirum Quantity
        \def\qq{\pgfmathparse{ \qeq }\pgfmathprintnumber[precision=1] \pgfmathresult}
        \draw[thin] (eq) node[below]{$Q*=\qq$}-- (ints);
}
}


\begin{document}

%% USE the Function
\begin{tikzpicture}[domain=0:5,scale=1,thick]
EQUILIBRIUM(4.5, -0.5, 3.49, 0.01)
\end{tikzpicture}

%% USE the Function Again
\begin{tikzpicture}[domain=0:5,scale=1,thick]
EQUILIBRIUM(4.1, -0.3, 3.49, 0.01)
\end{tikzpicture}

\end{document}

答案1

在大多数定义中,您不应该使用空行,因为它们通常被标记为错误(Paragraph ended before ...),以防止缺少右括号。

此外,您需要的是新命令的定义,而不是declare function。使用\newcommandLaTeX 原语,您可以缩写 LaTeX 代码以供以后重用;它可能包含最多九个参数,#1#9。另一方面,declare function需要 tikz 语法中的数学函数定义。

\documentclass[preview]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

%% Create the General Function
\newcommand\EQUILIBRIUM[4]%
   {%Define linear parameters for supply and demand
        \def\dint{#1}          %Y-intercept for DEMAND.
        \def\dslp{#2}          %Slope for DEMAND.
        \def\sint{#3}          %Y-intercept for SUPPLY.
        \def\sslp{#4}          %Slope for SUPPLY.
    % Define equilibrium points
        \def\qeq{ {(\sint-\dint)/(\dslp-\sslp) } }
        \def\peq{ {(\sint-\dint)/(\dslp-\sslp)*\sslp+\sint} }
    % Define equilibrium coordinates.
        \coordinate (ints)  at  ( \qeq, \peq);
        \coordinate (ep)    at  ( 0, \peq);
        \coordinate (eq)    at  ( \qeq, 0);
        \coordinate (dint)  at  ( 0, \dint);
        \coordinate (sint)  at  ( 0, \sint);
    % DEMAND
        \def\demand{\x,{\dslp*\x+\dint}}
        \draw[thick,color=blue] plot (\demand) node[right] {Demand}; %{$P(q) = -\frac{1}{2}q+\frac{9}{2}$};
    % SUPPLY
        \def\supply{\x,{\sslp*\x+\sint}}
        \draw[thick,color=purple] plot (\supply) node[right] {Supply};
    % Draw axes
        \draw[->] (0,0) -- (6.2,0) node[right] {$Q$};
        \draw[->] (0,0) -- (0,6.2) node[above] {$P$};
    % Equilibirum Price
        \def\pp{\pgfmathparse{ \peq }\pgfmathprintnumber[precision=1] \pgfmathresult}
        \draw[thin] (ep) node[left]{$P*=\pp$}-- (ints);
    % Equilibirum Quantity
        \def\qq{\pgfmathparse{ \qeq }\pgfmathprintnumber[precision=1] \pgfmathresult}
        \draw[thin] (eq) node[below]{$Q*=\qq$}-- (ints);
   }


\begin{document}

%% USE the Function
\begin{tikzpicture}[domain=0:5,scale=1,thick]
\EQUILIBRIUM{4.5}{-0.5}{3.49}{0.01}
\end{tikzpicture}

%% USE the Function Again
\begin{tikzpicture}[domain=0:5,scale=1,thick]
\EQUILIBRIUM{4.1}{-0.3}{3.49}{0.01}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容