如何在列表中使用 \foreach 和两个 \pgfmathsetmacro

如何在列表中使用 \foreach 和两个 \pgfmathsetmacro
\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}

\usepackage{tikz}
\usepackage{tikz-cd}

\begin{document}
\begin{tikzpicture}


\pgfmathsetmacro{\N}{6}
\pgfmathsetmacro{\angle}{360/\N}

\tikzset{structureline/.style={black,thin}}

\draw[lightgray] (-6,-6) grid (6,6);

\filldraw[thick,fill=green!20,draw=green] 
(0,0) -- (1/3*5,0) arc[radius=1/3*5, start angle=0, end angle=\angle] node[below=0.5cm] {$\alpha = 
\angle$} -- cycle;  

\draw[->,structureline] (0,-5) -- (0,5);
\draw[->,structureline] (-5,0) -- (5,0);
\draw[structureline] (0,0) circle[radius=4];
\draw[structureline] (0,0) -- (\angle:4);
\foreach \i in {\angle,2*\angle,...,\N*\angle}{do something}

\end{tikzpicture}
\end{document}

它不允许我这样使用列表,但是当我删除“...”时它确实有效

我怎样才能解决这个问题?

谢谢!

答案1

当您删除 时...,它只是一个显式列表。使用...,它期望找到一个模式。但是,我不认为循环的模式\foreach可以包含tikz诸如 之类的算术评估\angle,2*\angle,...,\N*\angle

但您可以做的是执行循环并在循环内部1,2,...,\N合并乘数。\angle

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}

\usepackage{tikz}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzpicture}

\pgfmathsetmacro{\N}{6}
\pgfmathsetmacro{\angle}{360/\N}

\tikzset{structureline/.style={black,thin}}

\draw[lightgray] (-6,-6) grid (6,6);

\filldraw[thick,fill=green!20,draw=green] 
(0,0) -- (1/3*5,0) arc[radius=1/3*5, start angle=0, end angle=\angle] node[right=0.5cm] {$\alpha = 
\angle$} -- cycle;  

\draw[->,structureline] (0,-5) -- (0,5);
\draw[->,structureline] (-5,0) -- (5,0);
\draw[structureline] (0,0) circle[radius=4];
%\draw[structureline] (0,0) -- (\angle:4);
\foreach \i in {1,2,...,\N}{
  \draw[structureline] (0,0) -- (\i*\angle:4);
  \filldraw[thick,fill=red!\i0,draw=red] 
  (0,0) -- ({(\i/2)*cos((\i-1)*\angle)},{(\i/2)*sin((\i-1)*\angle)}) 
  arc[radius=(\i/2), start angle=(\i-1)*\angle, 
  end angle=(\i)*\angle] node[] {\LARGE\i} -- cycle; 
}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

在这些情况下,手册建议使用parse=true

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}

\usepackage{tikz}
\usepackage{tikz-cd}

\begin{document}
\begin{tikzpicture}


\pgfmathsetmacro{\N}{6}
\pgfmathsetmacro{\angle}{360/\N}

\tikzset{structureline/.style={black,thin}}

\draw[lightgray] (-6,-6) grid (6,6);

\filldraw[thick,fill=green!20,draw=green] 
(0,0) -- (1/3*5,0) arc[radius=1/3*5, start angle=0, end angle=\angle] node[below=0.5cm] {$\alpha = 
\angle$} -- cycle;  

\draw[->,structureline] (0,-5) -- (0,5);
\draw[->,structureline] (-5,0) -- (5,0);
\draw[structureline] (0,0) circle[radius=4];
\draw[structureline] (0,0) -- (\angle:4);
\foreach \i [parse=true] in {\angle,2*\angle,...,\N*\angle}{
\node at (\i:4) {do something};
}

\end{tikzpicture}
\end{document}

\foreach 中的宏

相关内容