1

I would need a list, possibly an array (for later use in a foreach loop / TikZ graphic), which contains the following values ​​(a_n) in order (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 .........

The system is the following:
1x45, 0x90, 1x other angles
2x45, 1x90, 2x other angles
3x45, 2x90, 3x other angles
4x45, 3x90, 4x other angles
....

Note: I'm not looking for the formula function or anything like that, just looking for a list for TikZ-use.

\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}
0

2 Answers 2

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}

enter image description here

Everything in one list.

\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}

enter image description here

5
  • @cis I do not need \protected@xdef: \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 \foreach \X in {225,270,315} {\foreach \Y in {1,...,\A} {\xdef\myList{\myList,\X}}} \tikz{\foreach \w in \myList { \draw (0,0)--(\w:1); } } }
    – user121799
    Commented Mar 28, 2019 at 13:56
  • @marmot I cannot use this list in this kind \tikz[] \foreach \w in \myList { \draw (0,0)--(\w:1); } There has to be a comma after all numbers except after the last number (and maybe no line breaks).
    – cis
    Commented Mar 28, 2019 at 13:58
  • 1
    @cis I added such a code.
    – user121799
    Commented Mar 28, 2019 at 14:02
  • 1
    Oh yes Sir, that seems to be good. Have a look on the usage: [![enter image description here][1]][1] [1]: i.sstatic.net/zPtte.jpg
    – cis
    Commented Mar 28, 2019 at 14:13
  • @cis Schaut gut aus.
    – user121799
    Commented Mar 28, 2019 at 14:14
1

With 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}

enter image description here

A classical implementation, which shows why expl3 is preferable.

\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}
2
  • Ah, very good. Do you think, there is a possibility with pgfmath, xdef and so on?
    – cis
    Commented Mar 28, 2019 at 0:09
  • @cis I added a version with no additional package.
    – egreg
    Commented Mar 28, 2019 at 7:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .