Here you can find how to calculate and print table for the binomial distribution. I would like to get rid of those lines that are all empty due to too small probabilities (<=0.0000).
I used the answer given to the above mentioned question (see here). Then I added a new \mySAVETable. With the help of the etoolbox package (as already used in the answer) I tried to save only those lines that contain at least one probabilty. I also introduced a new counter to count the number of empty cells. Fortunately, this works all fine. However, cancelling the all empty lines fails. I get an empty table instead, only the head and the foot are printed.
Could anyone please help me to find out what I'm doing wrong here? Thanks!
Here is my code:
\documentclass[]{article}
\usepackage{amsmath}
\usepackage[margin=.5cm, showframe=true]{geometry}
\usepackage{booktabs}
\usepackage{pgf,pgffor}
\usepackage{etoolbox}
\usepgflibrary{fpu}
\pgfkeys{/pgf/fpu}
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}
\pgfkeys{/pgf/number format/.cd,fixed,fixed zerofill,precision=4}
%%% Defining basic stuff
\def\myHeadList{0.05, 0.10, 1/6, 0.20, 0.25, 0.30, 1/3, 0.40, 0.45, 0.50}
\def\myFootList{0.95, 0.90, 5/6, 0.80, 0.75, 0.70, 2/3, 0.60, 0.55, 0.50}
\newcounter{EmptyCells}
\setcounter{EmptyCells}{0}
%\def\startN{9}
%\def\endN{10}
\newif\ifsomanyzeroesshouldbeprinted
\newcommand*{\binomTable}[3][0]{% #1 = 0 => results in the form 0.0000 are typeset
% #1 != 0 => results in the form 0.0000 are omitted
% #2 = startN
% #3 = endN
\edef\startN{#2}%
\edef\endN{#3}%
\expandafter\ifnum#1=0\relax%
\somanyzeroesshouldbeprintedtrue%
\else%
\somanyzeroesshouldbeprintedfalse%
\fi%
%
%%% Building head of table
\def\myHead{ $n$ & $k$}%
\foreach \p in \myHeadList {%
\xappto\myHead{& {$\p$}}%
}%
\appto\myHead{& $k$ & EmptyCells\\}%
%
%%% Building foot of table
\def\myFoot{ $n$ & $k$}%
\foreach \p in \myFootList {%
\xappto\myFoot{& {$\p$}}%
}%
\appto\myFoot{& $k$ & EmptyCells\\}%
%
%%% Bulding body of table
\def\myTable{}%
\def\mySAVETable{}%
\def\myN{\startN,...,\endN}%
\foreach \n in \myN {%
\foreach \k in {0,...,\n}{%
\setcounter{EmptyCells}{0}
\ifnum\k=0\relax%
\xappto\myTable{\noexpand\midrule\n}%
\fi%
\xappto\myTable{& \k}%
\pgfmathsetmacro{\binomProduct}{1}%
\xdef\myTempValue{\binomProduct}%
\ifnum\k=0\relax%
\xdef\oldK{\k}%
\else%
\foreach \j in {1,...,\k}{%
\pgfmathsetmacro{\binomProduct}{\myTempValue*(\n+1-\j)/\j}%
\xdef\myTempValue{\binomProduct}%
}%
\fi%
\pgfmathsetmacro{\myTempValue}{round(\myTempValue)}%
% \xappto\myTable{& \noexpand\pgfmathprintnumber[/pgf/number format/.cd,fixed,precision=0,set thousands separator={\,},min exponent for 1000 sep=4]{\myTempValue}}%
\foreach \p in \myHeadList {%
\pgfmathsetmacro{\result}{\myTempValue*\p^(\k)*(1-\p)^(\n-\k)}%
\ifsomanyzeroesshouldbeprinted%
\xappto\myTable{& \noexpand\pgfmathprintnumber[skip 0.=true, dec sep={}]{\result}}%
\else%
\ifdim\result pt<0.00005pt\relax%
\gappto\myTable{&}%
\addtocounter{EmptyCells}{1}
\else%
\xappto\myTable{& \noexpand\pgfmathprintnumber[skip 0.=true, dec sep={}]{\result}}%
\fi%
\fi%
}%
\pgfmathsetmacro{\result}{\n-\k}%
\xappto\myTable{& \noexpand\pgfmathprintnumber[/pgf/number format/.cd,fixed,precision=0,set thousands separator={\,},min exponent for 1000 sep=4]{\result} & \arabic{EmptyCells}}%
\gappto\myTable{\\}%
\ifnum\arabic{EmptyCells}=10%
\else%
\appto\mySAVETable{\myTable}%
\fi%
}%
}%
}
\pagestyle{empty}
\begin{document}
\binomTable[1]{15}{17}
\begin{tabular}{rrr*{10}{r}r}
\toprule\myHead\mySAVETable\midrule\myFoot\bottomrule
\end{tabular}
\end{document}


pgfpackage, or might you be interested in an alternative, LuaLaTeX-based solution? Please advise. – Mico Jul 22 '19 at 08:37