使用 tikzmath 时遇到的问题

使用 tikzmath 时遇到的问题

我想重复绘制一个旋转 90 度的图形。我尝试使用 tikzmath,但不知道该怎么做。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{calc}
\usetikzlibrary{math}

\begin{document}
\begin{tikzpicture}[font=\sffamily,
    motorcoil/.style={
        decorate, decoration={coil, aspect=0.2, amplitude=20 mm,  segment length=2mm}, gray, very thick
    }
]  

  \tikzmath{
    coordinate \r1, \r2;
    real \th;
    \th = 0.0;
    for \k in {0,1}
    {
      \r1 = (\th : 2.5);
      \r2 = (\th : 4.4);
      \draw [motorcoil, color=red] (\r1) -- (\r2);
      \th = \th + 90;
    };

  }

\end{tikzpicture}
\end{document}

我收到一个错误:

Missing \endcsname inserted.} (followed by: )  line 27 pos -1

但这很好用:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{calc}
\usetikzlibrary{math}

\begin{document}
\begin{tikzpicture}[font=\sffamily,
    motorcoil/.style={
        decorate, decoration={coil, aspect=0.2, amplitude=20 mm,  segment length=2mm}, gray, very thick
    }
]  

  \coordinate (r1) at (0 : 2.5);
  \coordinate (r2) at (0 : 4.4);
  \draw [motorcoil, red] (r1) -- (r2);

\end{tikzpicture}
\end{document}

我做错了什么?或者还有其他方法可以重复旋转 90 度的绘图吗?

答案1

for \k in {0,1}您在开头之前添加了一个虚假空格{,这会导致解析器出错。请记住,新行是 TeX 的空格。

\documentclass[border=10pt,tikz,multi]{standalone}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{calc}
\usetikzlibrary{math}

\begin{document}
\begin{tikzpicture}
  [
    font=\sffamily,
    motorcoil/.style={%
      decorate,
      decoration={coil, aspect=0.2, amplitude=20 mm,  segment length=2mm},
      gray,
      very thick
    }
  ]
  \tikzmath{
    coordinate \r1, \r2;
    real \th;
    \th = 0.0;
    for \k in {0,1}%
    {
      \r1 = (\th : 2.5);
      \r2 = (\th : 4.4);
      {%
        \draw [motorcoil, color=red] (\r1) -- (\r2);
      };
      \th = \th + 90;
    };
  }
\end{tikzpicture}
\end{document}

无间距 TikZ 数学

答案2

grr,我用另一种方法做了\begin{scope}

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{calc}
\usetikzlibrary{math}

\begin{document}
\begin{tikzpicture}[font=\sffamily,
    motorcoil/.style={
        decorate, decoration={coil, aspect=0.2, amplitude=20 mm,  segment length=2mm}, gray, very thick
    }
]  

  \foreach\color [count=\k] in {red, blue}
  {
   \begin{scope}[rotate=90*\k]
   \coordinate (r1) at (0 : 2.5);
   \coordinate (r2) at (0 : 4.4);
   \draw [motorcoil, color=\color] (r1) -- (r2);
   \end{scope}
  }
\end{tikzpicture}
\end{document}

相关内容