I would like to automate the production of the following output (the formatting is not so good but I will do a better one later).
This picture has been obtained using this code :
\documentclass[12pt,a4paper]{article}
\usepackage{nicematrix}
\usepackage{tikz}
\usetikzlibrary{fit}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}
\begin{document}
$\begin{NiceArray}{r|c|c|c|c|c}[name=tab]
x_k & 0 & 1 & 2 & 3 & 4 \
\hline
p_k & 0.2000 & 0.2 & 0.4 & 0.05 & 0.15 \
\end{NiceArray}$
\begin{tikzpicture}[remember picture,overlay]
\node[draw = red,
rounded corners,
fit = (tab-1-2.north west) (tab-1-2.north east)
(tab-2-2.south west) (tab-2-2.south east)] {};
\node[draw = blue,
rounded corners,
fit = (tab-1-4.north west) (tab-1-4.north east)
(tab-2-4.south west) (tab-2-4.south east)] {};
\node[draw = black!60!green,
rounded corners,
fit = (tab-1-6.north west) (tab-1-6.north east)
(tab-2-6.south west) (tab-2-6.south east)] {};
\end{tikzpicture}
$E(X) = \sum_k p_k x_k$
$E(X) = \tcboxmath[colframe = red]%
{0.2000 \cdot 0}
+ 0.2 \cdot 1
+ \tcboxmath[colframe = blue]%
{0.4 \cdot 2}
+ 0.05 \cdot 3
+ \tcboxmath[colframe = black!60!green]%
{0.15 \cdot 4}$
\end{document}
I would like to only type the array part of the code.
\begin{exvalexpval}
x_k & 0 & 1 & 2 & 3 & 4 \\
\hline
p_k & 0.2000 & 0.2 & 0.4 & 0.05 & 0.15 \\
\end{exvalexpval}
The algorithm could be the following one but I do not know how to do that with expl3.
- Store the xk value in a list.
- Store the pk value in other list.
- Use the lenght of the lists to build the argument
r|c|c|c|c|cofNiceArray. - Use the two lists to build the content after the table.
- Wich colors ? In practice I think that no more than five colors
red,blue,black!60!green,orangeandgraywill be really needed. If more colors are needed, just use a cycle of colors.
Note : I do not want to calculate the value of the expected value. There will be cases of use with formal values in the table.



l3seq, especially the\seq_set_split:Nnnand mapping functions. They are documented ininterface3.pdf. And you may also input in csv format and use existing csv-parsing packages. – muzimuzhi Z Aug 08 '20 at 20:14