2

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
3
  • 1
    You may wait for version 2022B of tabularray which will be released on June 1st. See github.com/lvjr/tabularray/issues/81 for more details.
    – L.J.R.
    May 20, 2022 at 14:02
  • @L.J.R. Thanks for making me discover this future functionality. I also discovered the functional package (ctan.org/pkg/functional) which seems interesting.
    – Wabylete
    May 20, 2022 at 14:15
  • It's my pleasure, since I am the author.
    – L.J.R.
    May 20, 2022 at 14:56

1 Answer 1

2

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

1
  • Thanks a lot @L.J.R. ! Your suggestions are all pertinent and your explanation is very clear. The example you provided is indeed working as intended.
    – Wabylete
    May 22, 2022 at 12:22

You must log in to answer this question.

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