![如何在列表中使用 \foreach 和两个 \pgfmathsetmacro](https://linux22.com/image/431772/%E5%A6%82%E4%BD%95%E5%9C%A8%E5%88%97%E8%A1%A8%E4%B8%AD%E4%BD%BF%E7%94%A8%20%5Cforeach%20%E5%92%8C%E4%B8%A4%E4%B8%AA%20%5Cpgfmathsetmacro.png)
\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}