For example, I want to get an array with values sin(1),sin(2),..,sin(100) and store it to an variable. Is there a way to get this result by using foreach ?

EDIT: A more complicated example: Drawing 100 random points {(1,rnd),(2,rnd),...,(10,rnd)} and then drawing the distances from these points to their mean value.

I have no idea how to implements it in tikz, here is the pseudocode:

yarray = {}   //an array to store 100 random value
sum    = 0
for i = {1...100}
   yarray[i] = random
   sum = sum + yarray[i]
mean = sum/100
absarray = {}  //an array to store the distance from points to mean
for i = {1...100}
   absarray[i] = abs(yarray[i]-mean)

// draw the image
for i = {1...100}
   \fill (i,yarray[i]) circle (2pt)
   \fill (i,absarray[i]) circle (2pt)

Here is a way.


  % #1 = dummy variable
  % #2 = variable to store the list
  % #3 = expression
  % #4 = expression to store (with \pgfmathresult) and possibly #1
  % #5 = start point
  % #6 = end point
  \foreach #1 in {#5,...,#6}{\pgfmathparse{#3}\xdef\store@temp{\store@temp,#4}}%







Here's what's reported:

> \sineslist=macro:

> \randlist=macro:
| improve this answer | |

I have to say that I didn't get the distance argument in your question. I would not go with PGF or TikZ but produce data arrays stored in a file. That way you can access to the pgfplotstable and its quite fast macros. The TikZ part chokes in about 150 but the latter example goes pretty far (one other reason of course my code is not optimized at all so it's not only TikZ' fault).

Here is an example with 100 sample points drawn just with storing into arrays and reading them later and also a PGFPLOTSTABLE example I have compiled from the manual + some tweaks.



\pgfmathsetmacro{\runningrandarray}{rand} % Initial

\foreach \x[count=\xi from 1] in {2,...,\sample}{
\xdef\randarray{{\runningrandarray}} % Double brace needed if you want to access via TikZ

Some random points here : 

\foreach \x in {0,...,\number\numexpr\sample-1\relax}{
    \node[circle,inner sep=1pt,fill=red,ultra thin] at (\x mm,\ycoord mm){};

and their current mean value is \runningmean

Instead we can directly go with PGFPLOTSTABLE package to produce the values: 

create on use/y/.style={create col/expr={\pgfplotstablerow+1}},
% define how the 'sine' column shall be filled:
create on use/sine/.style={create col/expr={sin(\pgfplotstablerow+1)}},

show it as a table (columns skipped):
every head row/.style={before row=\toprule,after row=\midrule},
every last row/.style={after row=\bottomrule},
row predicate/.code={%
or directly use it in plots

    \begin{axis}[width=4cm,height=7cm,xlabel={angle [deg]}]
    \addplot[no marks] table[x=y,y=sine]\loadedtable;

enter image description here

| improve this answer | |

Unfortunately, I don't know how to deal with expansion related issues so can't give you the simple solution (attempt is commented out in). However, I have learned that using the datatool package works great for me:

enter image description here


  • I manually stepped thru the DB rather than use \DTLdisplaydb so that you can see how to step thru it (amusing that you need the individual entries).
  • The expansion experets here can show you how to to expand the result to store, but below I attemped to use one of the solutions from How keep a running list of strings and then process them one at a time to keep the list of sin values.



%% https://tex.stackexchange.com/questions/14393/how-keep-a-running-list-of-strings-and-then-process-them-one-at-a-time
%    \g@addto@macro\ListOfValues{{#1}}%

\foreach \x in {1,...,10} {%

List of $\sin$ Values:\bigskip

%\DTLdisplaydb{TableOfSinValues}% for automated display
$x$ [degrees] & $\sin{x}$ \\[0.5ex]
\DTLforeach{TableOfSinValues}{\Degree=Degree, \SinValue=SinValue}{%
    $\Degree$ & $\SinValue$\\
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.