TikZ:无法从交点提取坐标

TikZ:无法从交点提取坐标

我正在应用这个答案以便在两个函数相交的点自动绘制一条垂直线。该解决方案意味着创建一个新命令,该命令接收坐标作为输入,并提供 x 和 y 坐标作为输出。然而,我得到了错误

包 tikz 错误:无法解析此坐标。

MWE 如下:

\documentclass[tikz,convert=false]{standalone}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usetikzlibrary{calc}

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

\begin{document}

\begin{tikzpicture}[scale=0.8,domain=0.8:13]
% Axis
\draw[thick] [->] (0,0) -- (14,0);
\draw[thick] [->] (0,0) -- (0,7);
\node at (14,-0.5) {$A$};
\node at (-0.5,7) {$B$};
% Functions
\draw[thick,name path=MR] plot (\x,{7-0.5*\x}) node[right] {{\small MR}};
\draw[thick,name path=PC] plot (\x,{0.5*\x}) node[right] {{\small PC}};
% Equilibrium
\draw[name intersections={of=MR and PC,by={equi1}}];
\fill (equi1) circle (3pt) node {};
\gettikzxy{equil1)}{\ax}{\ay}
\draw[thick,name path=VPC] [-] (\ax,0) -- (equi1);
\end{tikzpicture}

\end{document}

答案1

您不需要这个新命令。您可以使用 Tikz 运算符|-绘制一条垂直线,该线停止在另一个坐标/节点的高度。

在本例中,我们定义O(as Origin) 为 at0,0并将其用作参考。尽管您可以说这不是必需的,但(equi1|-0,0);这样更容易重用。

此外,您可以将节点添加到轴路径,而不必单独定义它们。

输出

在此处输入图片描述

代码

\documentclass[tikz,margin=10pt]{standalone}
\usetikzlibrary{calc, intersections}

\begin{document}

\begin{tikzpicture}[scale=0.8,domain=0.8:13]
% Axis
\coordinate (O) at (0,0);
\draw[thick] [->] (0,0) -- (14,0) node[below right] {$A$};
\draw[thick] [->] (0,0) -- (0,7) node[above left] {$B$};
% Functions
\draw[thick,name path=MR] plot (\x,{7-0.5*\x}) node[right] {{\small MR}};
\draw[thick,name path=PC] plot (\x,{0.5*\x}) node[right] {{\small PC}};
% Equilibrium
\draw[name intersections={of=MR and PC,by={equi1}}];
\fill (equi1) circle (3pt) node {};

\draw[thick,name path=VPC] (equi1) -- (equi1|-O);

% don't need these
%\gettikzxy{equil1)}{\ax}{\ay}
%\draw[thick,name path=VPC] [-] (\ax,0) -- (equi1);
\end{tikzpicture}
\end{document}

相关内容