The arrays defined by arrayjobx
(which should be used in LaTeX instead of arrayjob
) are local, so if they are assigned values in a group (or a scope, if you prefer), the value will be lost when the group ends.
Also, it seems that arrayjobx
doesn't like 0
as an index.
A feature of \foreach
is that it executes its cycles in a group, so you're in a blind alley.
You could define a new array type, let's call it a “global array”, but some other tricks are needed; a syntax like
\pntAngles(\i)={\val}
will assign \val
to the correspondent array element, not the value of \val
.
\documentclass{article}
\usepackage{tikz}
\usepackage{fp}
\usepackage{arrayjobx}
\usepackage{etoolbox}
% define a new data structure
\let\newglobalarray\newarray
\patchcmd{\newglobalarray}{\edef}{\xdef}{}{}
\begin{document}
\def\lastn{8} % number of points
\FPeval{\angleOffset}{clip(360/\lastn)} % angle offset between dots
\newglobalarray\pntAngles
\foreach \i in {1,...,\lastn} {
\FPeval{\val}{clip((\i-1)*\angleOffset)}
% expand \val before doing the assignment
\begingroup\edef\x{\endgroup\noexpand\pntAngles(\i)= {\val}}\x
}
\foreach \i in {1,...,\lastn} {
\pntAngles(\i)
}
\end{document}
A different approach with expl3
and property lists; sequences are more efficient, but this better emulates arrayjobx
that allows any kind of index for its arrays.
\documentclass{article}
\usepackage{tikz}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\newarray}{m}
{
\prop_new:c { g_daugmented_array_#1_prop }
}
\DeclareExpandableDocumentCommand{\arrayget}{mm}
{
\prop_get:cx { g_daugmented_array_#1_prop } { #2 }
}
\NewDocumentCommand{\arrayput}{mmm}
{
\prop_gput:cxx { g_daugmented_array_#1_prop } { #2 } { #3 }
}
\cs_generate_variant:Nn \prop_get:Nn { cx }
\cs_generate_variant:Nn \prop_gput:Nnn { cxx }
\DeclareExpandableDocumentCommand{\fpeval}{m}
{
\fp_eval:n { #1 }
}
\NewDocumentCommand{\fpevalset}{mm}
{
\cs_new:Npx #1 { \fp_eval:n { #2 } }
}
\ExplSyntaxOff
\begin{document}
\fpevalset{\np}{8}
\fpevalset\lastn{\np-1} % number of points
\fpevalset{\angleOffset}{floor(360/\np)} % angle offset between dots
\newarray{pntAngles}
\foreach \i in {0,...,\lastn} {
\arrayput{pntAngles}{\i}{\fpeval{floor(\i*\angleOffset)}}
}
\foreach \i in {0,...,\lastn} {
\arrayget{pntAngles}{\i}
}
\end{document}
arrayjob
, butarrayjobx
. Depending on what you want to use the array for, there may be better methods. Anyway, you should always remember that\foreach
executes each cycle in a group, so the assignment to the array is lost when the group ends.arrayjobx
there's no provision for global assignments.