1

I have created a macro to display some multiplication tables. It's implemented with latex3. I have simplified my code, you can use the following as an example:

\documentclass[a4paper,12pt]{article}

\ExplSyntaxOn \NewDocumentCommand{\Table}{m}{ \clist_set:Nn \l_tmpa_clist {1,2,3,4,5,6,7,8,9,10,12} \begin{tabular}{|c|c|} \hline \clist_map_inline:Nn {\l_tmpa_clist} { $##1 \cdot #1$ & \fpeval{##1*#1} \ \hline } \end{tabular}

} \ExplSyntaxOff

\begin{document} \Table{3} \end{document}

I get:

enter image description here

I don't understand why I get two vertical lines at the bottom of the table. I expect that when all numbers are processed, there must be an horizontal line at the end of the table.

Does somebody have an idea of what's happening here ? Thank you!

  • you can see this https://tex.stackexchange.com/questions/70470/extra-line-in-a-tabular-using-expl3-syntax – pascal974 Sep 22 '22 at 07:48
  • unrelated but N arguments should not be braced so \clist_map_inline:Nn \l_tmpa_clist not \clist_map_inline:Nn {\l_tmpa_clist} (sometims, as here, it makes no difference, but sometimes it will fail completely) – David Carlisle Sep 22 '22 at 08:14
  • I think the definitive answer to this should come from the LaTeX3 team, but I looked in the code of \clist_map_inline:Nn and there are a whole bunch of "scan marks" in the code. Scan marks are largely invisible (i.e. they leave nothing in the typeset text), but they are still there. My guess is that one or more of these cause an new (empty) row to be started in the tabular. – Pieter van Oostrum Sep 22 '22 at 10:36
  • I found that it works correctly when you use an explicit function rather than the inline function. This is a different solution than the one mentioned in the referenced answer. Care to reopen the question? – Pieter van Oostrum Sep 22 '22 at 13:00
  • Thank you for all your comments, I was able to solve it. Also thank to David Carlisle for the hint. – Luc Armand Sep 22 '22 at 14:21
  • @PietervanOostrum: The question has been re-opened if you wish to post a different answer. – Werner Sep 22 '22 at 18:15
  • @Werner To be fair it's also possible (and better, as answers are collected in one place) to just port the solution to the other question. The method is also applicable there. – user202729 Sep 23 '22 at 02:44
  • @user202729 Yes, but the other question is about seq, not about clist, so I would have to check that it also applies in that case. – Pieter van Oostrum Sep 23 '22 at 10:23

1 Answers1

1

It appears that using a named function rather than inlining it solves the problem.

\documentclass[a4paper,12pt]{article}

\ExplSyntaxOn \NewDocumentCommand{\Table}{m}{ \clist_set:Nn \l_tmpa_clist {1,2,3,4,5,6,7,8,9,10,12} \newcommand{\mycalc}[1]{$##1 \cdot #1$ & \fpeval{##1*#1}\ \hline } \begin{tabular}{|c|c|} \hline \clist_map_function:NN \l_tmpa_clist \mycalc \end{tabular}

} \ExplSyntaxOff

\begin{document} \Table{3} \end{document}

enter image description here

By the way, I also tried \clist_map_variable:NNn, but it seems the variable doesn't make it to the second row cell. It looks like it is defined locally to the first row cell, which seems a bug to me.

  • clist_map_variable does explicitly say "the assignments to the ⟨variable⟩ are local", so partially by-design. The other part is that tabular makes each cell a "group". – user202729 Sep 23 '22 at 02:37
  • @user202729 I understand, but I would have expected it to be local to the whole map invocation. I guess it would be difficult to implement that. – Pieter van Oostrum Sep 23 '22 at 10:24
  • "Local scope" just works like that in TeX, so e.g. the variable value will leak after the map ends, and if you do an explicit \begingroup \clist_map_variable... {... \endgroup ...} the variable value would be cleared as well. The alternative is to make it TeX-global then explicitly restore the old value after the loop ends, which seems to result in what you want, but that's not "local" by-name and would probably interfere with the save stack. – user202729 Sep 23 '22 at 15:28