Latex3 floating point calculations in longtblr (tabularray)

calculationsfloatslatex3tablestabularray

I'm learning LaTeX3 and trying to do simple calculations on floats in a longtblr environment.

Trouble is that I cannot manage to dynamically compute sub-totals per row. The global total is working fine but the sub-total is evaluated only once. (l_subtot_fp should be 6 on the first row and 8 on the second)

The second problem is : How do you append rows containing the alignment character "&" ? I get "Misplaced alignment tab character &." error if I add them.

I suspect a TeX expansion enigma, can anyone clarify this ?

Thanks a lot in advance for any helping answer !

\documentclass{article}
\usepackage{tabularray}

\ExplSyntaxOn
\tl_new:N \g_rowlist_tl
\fp_set:Nn \g_total_fp {0}

\newcommand*{\NewRow}[3]{
    \fp_set:Nn \l_subtot_fp {#2}
    \fp_set:Nn \l_subtot_fp { \l_subtot_fp * #3 }
    % Next line should contain three "&" like this:
    % \tl_put_right:Nn \g_rowlist_tl {#1 & #2 & #3 & {\fp_use:N \l_subtot_fp}\\}
    \tl_put_right:Nn \g_rowlist_tl {#1 #2 #3 {\fp_use:N \l_subtot_fp}\\}
    \fp_add:Nn \g_total_fp {\l_subtot_fp}
}

\begin{document}

\NewRow{A}{3}{2}
\NewRow{B}{2}{4}

% Dynamic version (NOT WORKING)
\begin{longtblr}{cccc}
    \tl_use:N \g_rowlist_tl
    Total &&& \fp_eval:n {\g_total_fp}
\end{longtblr}

% What I would like to generate
\begin{longtblr}{cccc}
    A & 3 & 2 & 6\\
    B & 2 & 4 & 8\\
    Total &&& 14
\end{longtblr}

\end{document}
\ExplSyntaxOff

Best Answer

Although I think it would be better to use functional library to do programming inside tabularray tables, you could still use expl3 if you want. You may compare this answer with another answer for a similar question asked by the OP.


Here are some suggestions for the code in the question:

(1) \ExplSyntaxOn and \ExplSyntaxOff should only include expl3 code only. Putting \begin{document} inside them may cause problems. Also any code after \end{document} is omitted, so it is no use writing \ExplSyntaxOff after it.

(2) \fp_set:Nn can do many kinds of calculation in its second argument. So

\fp_set:Nn \l_subtot_fp {#2}
\fp_set:Nn \l_subtot_fp { \l_subtot_fp * #3 }

can be simplified as

\fp_set:Nn \l_subtot_fp {#2 * #3}

(3) You need to expand the value of \l_subtot_fp before adding it into \g_rowlist_tl. This can be done with e variant of arguments. And you can create e variant of commands with \cs_generate_variant:Nn function.

(4) You need to use outer specification expand=\g_rowlist_tl since you want to expand it inside the tabularray table.

The following is the modified code which produces the expected result:

\documentclass{article}
\usepackage{tabularray}

\ExplSyntaxOn
\tl_new:N \g_rowlist_tl
\fp_set:Nn \g_total_fp {0}
\cs_generate_variant:Nn \tl_put_right:Nn {Ne}
\newcommand*\NewRow[3]{
  \fp_set:Nn \l_subtot_fp {#2 * #3}
  \tl_put_right:Nn \g_rowlist_tl {#1 & #2 & #3 &}
  \tl_put_right:Ne \g_rowlist_tl {\fp_use:N \l_subtot_fp}
  \tl_put_right:Nn \g_rowlist_tl {\\}
  \fp_add:Nn \g_total_fp {\l_subtot_fp}
}
\ExplSyntaxOff

\begin{document}

\NewRow{A}{3}{2}
\NewRow{B}{2}{4}

% Dynamic version (NOW WORKING)
\ExplSyntaxOn
\begin{longtblr}[expand=\g_rowlist_tl]{cccc}
    \g_rowlist_tl
    Total &&& \fp_use:N \g_total_fp
\end{longtblr}
\ExplSyntaxOff

% What I would like to generate
\begin{longtblr}{cccc}
    A & 3 & 2 & 6\\
    B & 2 & 4 & 8\\
    Total &&& 14
\end{longtblr}

\end{document}

enter image description here

Related Question