我需要一个列表,可能是一个数组(用于稍后在 foreach 循环/TikZ 图形中使用),其中按顺序包含以下值(a_n)(n = 1,2,3,...):
45,135,225,270,315, 45,45,90,135,135,225,225,270,270,315,315, 45,45,45,90,90,135,135,135,225,225,225,315,315,315, 45,45,45,45,90,90,90,135,135,135,135,225,225,225,225,270,270,270,270,315,315,315,315 .........
该系统如下:
1x45, 0x90, 1x other angles
2x45, 1x90, 2x other angles
3x45, 2x90, 3x other angles
4x45, 3x90, 4x other angles
....
注意:我不是在寻找公式函数或类似的东西,只是在寻找 TikZ 使用的列表。
\documentclass[margin=5pt, varwidth]{standalone}
\usepackage{tikz}
\begin{document}
\pgfmathsetmacro{\k}{2}
\pgfmathsetmacro{\Start}{\k}
\pgfmathsetmacro{\End}{\k+5}
\foreach[evaluate={\N=int(\n+1)}] \n in {\Start,...,\End}{
\pgfmathsetmacro{\A}{int(floor(\N/2)-floor(\N/3))}
\n: \A~
}
\end{document}
答案1
\documentclass[margin=5pt, varwidth]{standalone}
\usepackage{pgffor}
\begin{document}
\newcommand{\AddOtherAngles}[1]{\foreach \X in {225,270,315}
{\foreach \Y in {1,...,#1}
{\xdef\myList{\myList,\X}}}}
\foreach \A [evaluate=\A as \B using {int(\A-1)}] in {1,...,4}
{\foreach \a in {1,...,\A}
{\ifnum\a=1
\xdef\myList{45}
\else
\xdef\myList{\myList,45}
\fi}
\ifnum\B>0
\foreach \b in {1,...,\B}
{\xdef\myList{\myList,90}}
\fi
\AddOtherAngles{\A}
\myList\par
}
\end{document}
所有内容均包含在一个列表中。
\documentclass[margin=5pt, varwidth]{standalone}
\usepackage{tikz}
\begin{document}
\foreach \A [evaluate=\A as \B using {int(\A-1)}] in {1,...,4}
{\foreach \a in {1,...,\A}
{\ifnum\a=1
\ifnum\A=1
\xdef\myList{45}
\else
\xdef\myList{\myList,45}
\fi
\else
\xdef\myList{\myList,45}
\fi}
\ifnum\B>0
\foreach \b in {1,...,\B}
{\xdef\myList{\myList,90}}
\fi
\foreach \X in {225,270,315}
{\foreach \Y in {1,...,\A}
{\xdef\myList{\myList,\X}}}
}
\typeout{\myList}
\tikz{\foreach \w in \myList { \draw (0,0)--(\w:1); } }
\end{document}
答案2
和expl3
:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\generatelist}{mm}
{% #1 = control sequence, #2 = number of repetitions
\genlist_main:nn { #1 } { #2 }
}
\cs_new_protected:Nn \genlist_main:nn
{
\tl_set:Nx \l_tmpa_tl
{
45
\prg_replicate:nn { #2-1 } { ,45 }
\prg_replicate:nn { #2-1 } { ,90 }
\prg_replicate:nn { #2 } { ,135 }
\prg_replicate:nn { #2 } { ,225 }
\prg_replicate:nn { #2 } { ,270 }
\prg_replicate:nn { #2 } { ,315 }
}
\tl_if_exist:NTF #1
{
\msg_error:nnx {genlist}{exist}{\token_to_str:N #1}
}
{
\tl_new:N #1
\tl_set_eq:NN #1 \l_tmpa_tl
}
}
\msg_new:nnnn {genlist}{exist}
{
Control~sequence~#1~already~defined
}
{
The~control~sequence~#1~is~already~defined,~choose~a~different~name
}
\ExplSyntaxOff
\generatelist{\ONE}{1}
\generatelist{\TWO}{2}
\generatelist{\THREE}{3}
\generatelist{\FOUR}{4}
\generatelist{\box}{10} % this raises an error
\begin{document}
\ONE
\TWO
\THREE
\FOUR
\end{document}
一个经典的实现,展示了为什么它expl3
是可取的。
\documentclass{article}
\makeatletter
\newcommand{\genlist@aux}[2]{%
\ifnum#1>0
,#2%
\expandafter\@firstofone
\else
\expandafter\@gobble
\fi
{\expandafter\genlist@aux\expandafter{\the\numexpr#1-1}{#2}}%
}
\newcommand{\generatelist}[2]{%
\@ifdefinable#1{\genlist@main{#1}{#2}}%
}
\newcommand{\genlist@main}[2]{%
\edef#1{%
45%
\expandafter\genlist@aux\expandafter{\the\numexpr#2-1}{45}%
\expandafter\genlist@aux\expandafter{\the\numexpr#2-1}{90}%
\genlist@aux{#2}{135}%
\genlist@aux{#2}{225}%
\genlist@aux{#2}{270}%
\genlist@aux{#2}{315}%
}%
}
\makeatother
\generatelist{\ONE}{1}
\generatelist{\TWO}{2}
\generatelist{\THREE}{3}
\generatelist{\FOUR}{4}
%\generatelist{\box}{10} % this would raise an error
\begin{document}
\ONE
\TWO
\THREE
\FOUR
\end{document}