我想生成一个名为“anglelist
生成图表”的列表...对我来说,首选的命令是,\edef\anglelist{\fullanglelist}
但却出现了错误。
但使用\edef\anglelist{\fullanglelist}
和\edef\anglelist{\fixedlist}
不会给我任何错误。
我的问题是“你能告诉我该如何工作吗
\edef\anglelist{\fullanglelist}
?
先谢谢了。接下来请看一下我所说的代码。
\documentclass[11pt]{article}
\usepackage{tikz}
\begin{document}
\newcount\nodes
\nodes=8
\newcount\hnodes
\hnodes=\nodes
\advance \hnodes by -1
\divide \hnodes by 2
%
\newcount\hnodesplus
\hnodesplus=\nodes
\advance \hnodesplus by 1
\divide \hnodesplus by 2
%
\newcount\initialstepsize
\initialstepsize=360
\divide \initialstepsize by \nodes
%
\newcount\initialremainder
\initialremainder=\initialstepsize
\multiply \initialremainder by -\nodes
\advance \initialremainder by 360
%
\newcount\hstep
\hstep=\initialstepsize
\divide \hstep by 2
\newcount\simplestep
\simplestep=\initialstepsize
\ifnum \initialremainder > 0%this makes angles a larger than expected.
\advance \simplestep by 1
\fi
\def\fullanglelister{%
\newcount\tempa
\newcount\tempb
\tempa=0
\tempb=0
%
\loop
\the\tempa\relax
\advance\tempa by \initialstepsize
\advance\tempb by \initialremainder
\ifnum\tempb > \hnodes% usually chosen to be 0, (better to be) \hnodes, or \nodes
\advance\tempa by 1
\advance\tempb by -\nodes
\fi
\ifnum \tempa < 360
,
\repeat}
\def\shortanglelister{0, \the\simplestep, ..., 359}
\def\fixedlist{0, 45, 90, 135, 180, 225, 270, 315}
\edef\anglelist{\shortanglelister}
%\edef\anglelist{\fullanglelister} % this one doesn't work!
%\edef\anglelist{\fixedlist} % but this works!
\begin{tikzpicture}
\foreach \angle in \anglelist
\node[rectangle,draw=black!50] (\angle) at (\angle:2) {\angle};
\foreach \from in \anglelist
\foreach \to in \anglelist
\path (\from) edge [->,bend right=\the\hstep,looseness=0.8] (\to);
\end{tikzpicture}
\fullanglelister
\shortanglelister
\fixedlist
\end{document}
答案1
如果(出于某种原因)您想扩展 edef 中的循环,则需要使用扩展而不是使用赋值来编写它。以下代码扩展为
0, 45, 90, 135, 180, 225, 270, 315
如 typeout 行所示
B: 0, 45, 90, 135, 180, 225, 270, 315
在控制台上
\documentclass[11pt]{article}
\usepackage{tikz}
\begin{document}
\newcount\nodes
\nodes=8
\newcount\hnodes
\hnodes=\nodes
\advance \hnodes by -1
\divide \hnodes by 2
%
\newcount\hnodesplus
\hnodesplus=\nodes
\advance \hnodesplus by 1
\divide \hnodesplus by 2
%
\newcount\initialstepsize
\initialstepsize=360
\divide \initialstepsize by \nodes
%
\newcount\initialremainder
\initialremainder=\initialstepsize
\multiply \initialremainder by -\nodes
\advance \initialremainder by 360
%
\newcount\hstep
\hstep=\initialstepsize
\divide \hstep by 2
\newcount\simplestep
\simplestep=\initialstepsize
\ifnum \initialremainder > 0%this makes angles a larger than expected.
\advance \simplestep by 1
\fi
\makeatletter
\def\fullanglelister{%
\expandafter\@gobble\romannumeral`\^^@%
\expandafter\zz\shortanglelister\relax}
\def\zz#1,#2,#3,#4\relax{%
\ifnum#1<\numexpr#4\relax
, \the\numexpr#1\relax
\expandafter\zz\the\numexpr#1+#2\relax,#2,#3,#4\relax
\fi}
\makeatother
\def\shortanglelister{0, \the\simplestep, ..., 359}
\def\fixedlist{0, 45, 90, 135, 180, 225, 270, 315}
\edef\anglelist{\shortanglelister}
\typeout{A:\anglelist}
\edef\anglelist{\fullanglelister} % this one does work!
\typeout{B:\anglelist}
%\edef\anglelist{\fixedlist} % but this works!
\begin{tikzpicture}
\foreach \angle in \anglelist
\node[rectangle,draw=black!50] (\angle) at (\angle:2) {\angle};
\foreach \from in \anglelist
\foreach \to in \anglelist
\path (\from) edge [->,bend right=\the\hstep,looseness=0.8] (\to);
\end{tikzpicture}
\fullanglelister
\shortanglelister
\fixedlist
\end{document}
答案2
如果您希望整数尽可能均匀地朝着特定目标移动,最好的方法是在每次迭代时重新计算步长,即剩余距离除以剩余步数。
通常的做法是将 的结果放入\edef
与创建它的宏不同的宏中。例如, 的输出\pgfmathparse
放入\pgfmathresult
,并\pgfmathsetmacro
接受输出宏名称作为参数。这是我采取的方法。
\documentclass[11pt]{article}
\usepackage{tikz}
\newcount\nodes% considering how infrequently this is used, a macro would suffice
\def\fullanglelister#1{% #1 = output macro name (global)
\bgroup% use local registers
\count1=0 % angle
\count2=\nodes% remaining steps
\edef\temp{\the\count1}% output macro (local)
\loop
\count3=360 % compute step size (360-angle)/steps
\advance \count3 by -\count1
\divide \count3 by \count2
\advance\count1 by \count3
\advance\count2 by -1
\edef\temp{\temp,\the\count1}%
\ifnum \count2>1 \repeat
\global\let#1\temp
\egroup}
\begin{document}
\nodes=8
\fullanglelister{\anglelist}%
\pgfmathsetmacro{\hstep}{360/\nodes}%
\begin{tikzpicture}
\foreach \angle in \anglelist
\node[rectangle,draw=black!50] (\angle) at (\angle:2) {\angle};
\foreach \from in \anglelist {
\foreach \to in \anglelist {
\path (\from) edge [->,bend right=\hstep,looseness=0.8] (\to);}}
\end{tikzpicture}
\end{document}