在 draw let 中获得的值的全局使用

在 draw let 中获得的值的全局使用

在我的一个tikzpictures 中,我发现了一个标记为 的角度\n2。我想用这个值来旋转一个节点。但是,当我尝试时,在节点选项中,rotate = \n2我被告知未定义的控制序列。我猜只能\n2在命令中访问\draw let

我可以设置\n2为全局使用吗?


\documentclass[tikz, convert = false]{standalone}%

\usetikzlibrary{calc}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}[line join = round, line cap = round]
  \coordinate (O) at (0, 0);

  \draw (O) -- +(3, 0) coordinate (P1);
  \draw[name path = sline] (O) -- (3, 2) coordinate (P2);

  \draw[-stealth] let
    \p0 = (O),
    \p1 = (P1),
    \p2 = (P2),
    \n1 = {atan2(\y1 - \y0, \x1 - \x0)},
    \n2 = {atan2(\y2 - \y0, \x2 - \x0)},
    \n3 = {1cm},
    \n4 = {(\n1 + \n2)/2}
  in (O) +(\n1:\n3) arc[radius = \n3,
  start angle = \n1, end angle = \n2] node[right, font = \tiny] at (\n4:\n3)
  {$\theta$} \pgfextra{\xdef\myn{\n2}} ;

  \path[name path = line1] (1.5, 0) -- +(0, 1.25);
  \path[name path = line2] (2, 0) -- +(0, 1.5);
  \path[name intersections = {of = sline and line1, by = P3}];
  \path[name intersections = {of = sline and line2, by = P4}];

  \draw (P3) -- ($(P3)!.25cm!-90:(O)$) coordinate (P5);
  \draw (P4) -- ($(P4)!.25cm!-90:(O)$) coordinate (P6);
  \draw (P5) -- (P6) node[pos = .5, below, font = \tiny, rotate = {\n2}] {$M$};
\end{tikzpicture}
\end{document}

无论我将它放在哪里\pgfextra{\xdef\myn{\n2}},我都会得到未定义的控制序列

答案1

要进行辅助计算,您可以使用\pgfextra{}。然后,您可以使用扩展全局定义来定义全局变量。

\documentclass[tikz, convert = false]{standalone}%

\usetikzlibrary{calc}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}[line join = round, line cap = round]
  \coordinate (O) at (0, 0);

  \draw (O) -- +(3, 0) coordinate (P1);
  \draw[name path = sline] (O) -- (3, 2) coordinate (P2);

  \draw[-stealth] let
    \p0 = (O),
    \p1 = (P1),
    \p2 = (P2),
    \n1 = {atan2(\y1 - \y0, \x1 - \x0)},
    \n2 = {atan2(\y2 - \y0, \x2 - \x0)},
    \n3 = {1cm},
    \n4 = {(\n1 + \n2)/2}
  in (O) +(\n1:\n3) arc[radius = \n3,
  start angle = \n1, end angle = \n2] node[right, font = \tiny] at (\n4:\n3)
  {$\theta$} \pgfextra{\xdef\myn{\n2}} ;

  \path[name path = line1] (1.5, 0) -- +(0, 1.25);
  \path[name path = line2] (2, 0) -- +(0, 1.5);
  \path[name intersections = {of = sline and line1, by = P3}];
  \path[name intersections = {of = sline and line2, by = P4}];

  \draw (P3) -- ($(P3)!.25cm!-90:(O)$) coordinate (P5);
  \draw (P4) -- ($(P4)!.25cm!-90:(O)$) coordinate (P6);
  \draw (P5) -- (P6) node[pos = .5, below, font = \tiny, rotate = {\myn}] {$M$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容