理解数学 tikzlibrary

理解数学 tikzlibrary

在我的问题中TikZ 中的根螺旋,Mark Wibrow 的回答引起了我的注意,但在尝试编辑代码时,我发现自己在 ifelse 语句上遇到了困难。我想要实现的只是在第一个直角三角形的底部放置 1 而不是 1 的平方根。我试着说

if \n = 1 then {1} else {$\sqrt{\n}$}

但它不起作用。我不清楚数学库(pgfmanual 第 652 页)及其工作原理。我很确定我使用的 ifelse 语句是错误的;可能更多是在声明之外。我甚至尝试过

\p = (\n == \N && \N > 1) ? "$\sqrt{\n}$" : "1";

但没有什么用。关于如何实现预期结果的任何见解。

\newcommand\sqrtspiral[2][]{%
\tikz[line cap=round, x=2cm,y=2cm, line join=round,#1]{%
\tikzmath{%
  int \n;
  \b = 0; \d = 1; \N = #2;
  for \n in {1,...,\N}{
    \l = (\n == \N && \N > 1) ? "red" : "black";
    \e = (\n == 1) ? " -- cycle" : "";
    {
      \path [rotate=\b, draw=\l] (0,0) -- (\d,1) -- (\d,0)
        node [\l, midway, anchor=\b+180] {1}
        \e (\d/2, 0)  node [fill=white] {if \n = 1 then {1} else {$\sqrt{\n}$}}; % edit here
      \path [draw=\l, rotate=\b] (\d-.1,0) |- ++(.1,.1);
    };
    \d = sqrt(1+(\d)^2); \b = \b + asin(1/\d);
  };
  {
    \path [rotate=\b, \l] (\d/2, 0) node [fill=white] {$\sqrt x$};
  };
}}}

以下是答案的链接:关联

MWE 如下(当然只有一个):

\documentclass[tikz,border=0.125cm]{standalone}
\usetikzlibrary{math}
\begin{document}

\newcommand\sqrtspiral[2][]{%
\tikz[line cap=round, x=2cm,y=2cm, line join=round,#1]{%
\tikzmath{%
  int \n;
  \b = 0; \d = 1; \N = #2;
  for \n in {1,...,\N}{
    \l = (\n == \N && \N > 1) ? "red" : "black";
    \e = (\n == 1) ? " -- cycle" : "";
    {
      \path [rotate=\b, draw=\l] (0,0) -- (\d,1) -- (\d,0)
        node [\l, midway, anchor=\b+180] {1}
        \e (\d/2, 0)  node [fill=white] {%
            if \n = 1  then {1} else {$\sqrt{\n}$} %edit here
        };
      \path [draw=\l, rotate=\b] (\d-.1,0) |- ++(.1,.1);
    };
    \d = sqrt(1+(\d)^2); \b = \b + asin(1/\d);
  };
  {
    \path [rotate=\b, \l] (\d/2, 0) node [fill=white] {$\sqrt x$};
  };
}}}

\sqrtspiral{10}

\end{document}

答案1

首先要知道:里面的所有内容{ };(带分号)都被视为外面的tikzmath。因此,您不能将if逻辑放在里面{ };(TikZ 3.0.0 手册的 56.7 在解析器外执行代码,第 634 页)。

第二件要知道的事:if \n = 1 then {1} else {2}没有print命令,语句12不是有效的评估tikzmath,我们与进行比较==
因此,要纠正这个问题,你必须执行if \n == 1 then {print{1};} else {print{2};};。如果你使用数学模式,
这里是可选的,但如果你想要类似或的东西,则是强制性的(print$ $print{1};print{Some text here};查看 Mark Wibrow 的回答)。

因此,在您的情况下,放入节点的正确代码是:

\tikzmath{ 
  if \n == 1 then {
    {$1$};
  } else {
    {$\sqrt{\n}$};
  };
}

结果如下:

在此处输入图片描述

\documentclass[tikz,border=0.125cm]{standalone}
\usetikzlibrary{math}

\newcommand\sqrtspiral[2][]{%
\tikz[line cap=round, x=2cm,y=2cm, line join=round,#1]{%
\tikzmath{%
  int \n;
  \b = 0; \d = 1; \N = #2;
  for \n in {1,...,\N}{
    \l = (\n == \N && \N > 1) ? "red" : "black";
    \e = (\n == 1) ? " -- cycle" : "";
    { % --------------------------------------------------- outside math library
      \path [rotate=\b, draw=\l] (0,0) -- (\d,1) -- (\d,0)
        node [\l, midway, anchor=\b+180] {1}
        \e (\d/2, 0)  node [fill=white] { \tikzmath{ % -- back in math library
            if \n == 1 then {
              {$1$}; % ------------------------- $1$ is outside math library
            } else {
              {$\sqrt{\n}$}; % --------- $\sqrt{\n}$ is outside math library
            };
          }% -------------------------------------------- outside math library
        };
      \path [draw=\l, rotate=\b] (\d-.1,0) |- ++(.1,.1);
    }; % -------------------------------------------------- back in math library
    \d = sqrt(1+(\d)^2); \b = \b + asin(1/\d);
  };
  { % ----------------------------------------------------- outside math library
    \path [rotate=\b, \l] (\d/2, 0) node [fill=white] {$\sqrt x$};
  }; % ---------------------------------------------------- back in math library
}}}

\begin{document}

\sqrtspiral{10}

\end{document}

答案2

因为它只是排版节点内容而不是使用它进行计算,所以如果满足以下条件,您可以使用普通 tex:

\documentclass[tikz,border=0.125cm]{standalone}
\usetikzlibrary{math}
\begin{document}

\newcommand\sqrtspiral[2][]{%
\tikz[line cap=round, x=2cm,y=2cm, line join=round,#1]{%
\tikzmath{%
  int \n;
  \b = 0; \d = 1; \N = #2;
  for \n in {1,...,\N}{
    \l = (\n == \N && \N > 1) ? "red" : "black";
    \e = (\n == 1) ? " -- cycle" : "";
    {
      \path [rotate=\b, draw=\l] (0,0) -- (\d,1) -- (\d,0)
        node [\l, midway, anchor=\b+180] {1}
        \e (\d/2, 0)  node [fill=white] {$\ifnum\n=1 1\else\sqrt{\n}\fi $};
      \path [draw=\l, rotate=\b] (\d-.1,0) |- ++(.1,.1);
    };
    \d = sqrt(1+(\d)^2); \b = \b + asin(1/\d);
  };
  {
    \path [rotate=\b, \l] (\d/2, 0) node [fill=white] {$\sqrt x$};
  };
}}}

\sqrtspiral{10}

\end{document}

答案3

描述"x"手册第 935 页:

这些运算符用于引用 x。但是,由于每个表达式在解析之前都使用 \edef 进行扩展,因此宏(例如,字体命令 \tt 或 \Huge)可能需要“保护”以免受到此扩展的影响(例如,\noexpand\Huge)。

您可以定义一个新的宏

\newcommand\mysqrt{$\sqrt{\n}$}

然后使用

\p = (\n == 1) ? 1 : "\noexpand\mysqrt";

代码:

\documentclass[tikz,border=0.125cm]{standalone}
\usetikzlibrary{math}
\newcommand\mysqrt{$\sqrt{\n}$}

\begin{document}

\newcommand\sqrtspiral[2][]{%
\tikz[line cap=round, x=2cm,y=2cm, line join=round,#1]{%
\tikzmath{%
  int \n;
  \b = 0; \d = 1; \N = #2;
  for \n in {1,...,\N}{
    \l = (\n == \N && \N > 1) ? "red" : "black";
    \e = (\n == 1) ? " -- cycle" : "";
    \p = (\n == 1) ? 1 : "\noexpand\mysqrt";
    {
      \path [rotate=\b, draw=\l] (0,0) -- (\d,1) -- (\d,0)
        node [\l, midway, anchor=\b+180] {1}
        \e (\d/2, 0)  node [fill=white] {\p};
      \path [draw=\l, rotate=\b] (\d-.1,0) |- ++(.1,.1);
    };
    \d = sqrt(1+(\d)^2); \b = \b + asin(1/\d);
  };
  {
    \path [rotate=\b, \l] (\d/2, 0) node [fill=white] {$\sqrt x$};
  };
}}}

\sqrtspiral{10}

\end{document}

相关内容