6

I want to create a long table containing some statistical data, such as the sample, the average, and the variance, etc. I am using the pgf package and its various extensions to do this.

Here is a silly MWE:

\documentclass{article}
\usepackage{pgf}
\usepackage{tikz}
\usepgflibrary{fpu}
\pgfmathsetseed{\number\pdfrandomseed}
\begin{document}
\xdef\Sum{0}
\xdef\Rand{0}
\xdef\Avg{0}
\xdef\Var{0}
\xdef\Sq{0}
%\begin{table}[h]
%\begin{tabular}{|c|c|c|c|c|c|}
%\hline
%Trial & Rand & Sum & Sum-of-squares & Average & Variance\\
%\hline\hline
%\noindent Trial Rand Sum Sum-of-squares Average Variance\\
\noindent\foreach \i in {1 ,...,15} {
    %\pgfkeys{/pgf/fpu,/pgf/fpu/output format=sci}
        \pgfmathparse{random(10)}
        \xdef\Rand{\pgfmathresult}
        \pgfkeys{/pgf/fpu,/pgf/fpu/output format=sci}
        \pgfmathparse{\Sum+\Rand}
        \xdef\Sum{\pgfmathresult}
        \pgfmathparse{\Sq+\Rand*\Rand}
        \xdef\Sq{\pgfmathresult}
        \pgfmathparse{\Sum/\i}
        \xdef\Avg{\pgfmathresult}
        \pgfmathparse{\Sq/\i-\Avg*\Avg}
        \xdef\Var{\pgfmathresult}
        \noindent \i,\                             %&%
        \pgfmathparse{\Rand}\pgfmathresult,\    %&%
        \pgfmathparse{\Sum}\pgfmathresult,\      %&%
        \pgfmathparse{\Sq}\pgfmathresult,         %&%
        \pgfmathparse{\Avg}\pgfmathresult,       %&%
        \pgfmathparse{\Var}\pgfmathresult        \\
        %\hline
        }
        %\end{tabular}
%\end{table}
\end{document}

Here is an output (depending on what random numbers you get):

enter image description here

As you can see, the table is commented out, as I have given up on it. I want to generate a document like this with 1000 pages, and see the computed empirical average and variance.


Very closely related:


Note: this question has been heavily edited due to showing signs of the XY problem. I will reask problem X in a separate question later.

1
  • While egreg is right that you cannot build a table in a \foreach loop, you might want to take a look at the pgfplotstable package. (Or someone with more experience can show us both how to do it.) May 28, 2016 at 22:30

2 Answers 2

14

I suspected that your question How do I use the ampersand (&) inside a foreach or conditional (or other group/environment) when building tables? was of the XY type.

The usual problem in these cases is that you cannot build a table inside a \foreach statement, because table cells form groups. The strategy is to build the table body beforehand.

\documentclass{article}

\usepackage{booktabs,etoolbox}

\usepackage{pgf}
\usepackage{tikz}
\usepgflibrary{fpu}

\pgfmathsetseed{\number\pdfrandomseed}

\begin{document}

\def\Sum{0}
\def\Rand{0}
\def\Avg{0}
\def\Var{0}
\def\Sq{0}
\def\TableBody{}
\foreach \i in {1,...,15} {
  %\pgfkeys{/pgf/fpu,/pgf/fpu/output format=sci}
  \pgfmathparse{random(10)}
  \xdef\Rand{\pgfmathresult}
  \pgfkeys{/pgf/fpu,/pgf/fpu/output format=sci}
  \pgfmathparse{\Sum+\Rand}
  \xdef\Sum{\pgfmathresult}
  \pgfmathparse{\Sq+\Rand*\Rand}
  \xdef\Sq{\pgfmathresult}
  \pgfmathparse{\Sum/\i}
  \xdef\Avg{\pgfmathresult}
  \pgfmathparse{\Sq/\i-\Avg*\Avg}
  \xdef\Var{\pgfmathresult}
  \xappto\TableBody{\i & \Rand & \Sum & \Sq & \Avg & \Var \noexpand\\}
}

\begin{tabular}{ *{6}{c} }
\toprule
Trial & Rand & Sum & Sum-of-squares & Average & Variance \\
\midrule
\TableBody
\bottomrule
\end{tabular}

\end{document}

enter image description here

Here's what I get with longtable and 500 draws.

enter image description here

1
  • I will edit my question to match your answer to problem Y, and after more empirical testing I hope to reask question X again separately. Related: What is the XY problem?.
    – Matsmath
    May 28, 2016 at 11:09
2

I am no expert in pgfplotstable but here is an idea to build upon.

To get the entries to align prettier, see Q131081.

(I find pgfplotstable overly complicated and it surely doesn't help that the manual repeats parts of the PGFmanual …)

Code

\documentclass{article}
\usepackage{booktabs,pgfplotstable}
\pgfmathsetseed{\number\pdfrandomseed}
\pgfplotstableset{
  duck ini/.style={columns={Trial,Rand}},
  duck table/.style={
    columns={Trial,Rand,Sum,Sq,Avg,Var},
    set column name/.list={Sq:Sum-of-squared, Avg:Average, Var:Variance},
    every head row/.append style={before row=\toprule, after row=\midrule},
    every last row/.append style={after row=\bottomrule},
    set column/.list={Avg:prec=1, Var:prec=2}},
  set column name/.style args={#1:#2}{columns/#1/.append style={column name={#2}}},
  set column/.style args={#1:#2}{columns/#1/.append style={#2}},
  set expr/.style args={#1=#2}{create on use/#1/.style={create col/expr={#2}}}}
\pgfset{number format/prec/.style={fixed, fixed zerofill, precision={#1}}}
\pgfplotstableset{set expr/.list={Trial=\pgfplotstablerow+1,Rand=int(rnd*11)}}
\newcommand*\addmyusualcolumns[1]{%
  \pgfplotstablecreatecol[expr={\pgfmathaccuma+\thisrow{Rand}}]                           {Sum} #1
  \pgfplotstablecreatecol[expr={\pgfmathaccuma+\thisrow{Rand}*\thisrow{Rand}}]            {Sq}  #1
  \pgfplotstablecreatecol[expr={\thisrow{Sum}/\thisrow{Trial}}]                           {Avg} #1
  \pgfplotstablecreatecol[expr={\thisrow{Sq}/\thisrow{Trial}-\thisrow{Avg}*\thisrow{Avg}}]{Var} #1}
\begin{document}
\pgfplotstablenew[duck ini]{15}\t
\addmyusualcolumns\t
\pgfplotstabletypeset[duck table]\t

\pgfplotstablenew[duck ini]{30}\T
\addmyusualcolumns\T
\pgfplotstabletypeset[duck table]\T
\end{document}

You must log in to answer this question.

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