6

I'm trying to fill a table with content that is stored in a sequence by a custom command.

\documentclass{article}

\usepackage{xparse}
\usepackage{expl3}

\ExplSyntaxOn

\seq_new:N \my_sequence

\NewDocumentCommand{\addToMySequence}{mmm}{ \seq_gput_right:Nn \my_sequence {#1 & #2 & #3 \\ \hline} }

\NewDocumentCommand{\fillTable}{}{
    \seq_map_inline:Nn \my_sequence
    {
        ##1
    }
}

\ExplSyntaxOff

\addToMySequence{a}{b}{c}
\addToMySequence{d}{e}{f}
\addToMySequence{g}{h}{i}

\begin{document}

\begin{tabular}{|c|c|c|}
    \hline
    \fillTable
\end{tabular}

\end{document}

The code basically works as expected, but produces an orphan cell at the end of the table:

orphan cell

This also happens if you comment out all calls to \addToMySequence or the ##1 inside the iteration. So it must come from somewhere inside that command.

Any suggestions how to fix this?

1 Answer 1

4

The problem is that the loop started by \seq_map_inline:Nn realizes the sequence has ended only after the last \hline has been issued and so a new cell is started. You can exploit \seq_use, instead.

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn

\seq_new:N \g_hurzelchen_cells_seq
\tl_new:N \l_hurzelchen_table_tl

\NewDocumentCommand{\addToMySequence}{mmm}
 {
  \seq_gput_right:Nn \g_hurzelchen_cells_seq  {#1 & #2 & #3 \\ \hline}
 }

\NewDocumentCommand{\fillTable}{}
 {
  \tl_clear:N \l_hurzelchen_table_tl
  \seq_map_inline:Nn \g_hurzelchen_cells_seq
   {
    \tl_put_right:Nn \l_hurzelchen_table_tl { ##1 }
   }
  \tl_use:N \l_hurzelchen_table_tl
 }

\ExplSyntaxOff

\addToMySequence{a}{b}{c}
\addToMySequence{d}{e}{f}
\addToMySequence{g}{h}{i}

\begin{document}

\begin{tabular}{|c|c|c|}
    \hline
    \fillTable
\end{tabular}

\end{document}

Note that every variable name should be prefixed by g (global usage), l (local usage) or c (global constant) and end with the variable type.

enter image description here

An alternative way, which can come handy if we have to do more things with the sequence and not only deliver it, is to build the whole table in a token list and deliver this one:

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn

\seq_new:N \g_hurzelchen_cells_seq

\NewDocumentCommand{\addToMySequence}{mmm}
 {
  \seq_gput_right:Nn \g_hurzelchen_cells_seq  {#1 & #2 & #3 \\ \hline}
 }

\NewDocumentCommand{\fillTable}{}
 {
  \seq_use:Nn \g_hurzelchen_cells_seq { }
 }

\ExplSyntaxOff

\addToMySequence{a}{b}{c}
\addToMySequence{d}{e}{f}
\addToMySequence{g}{h}{i}

\begin{document}

\begin{tabular}{|c|c|c|}
    \hline
    \fillTable
\end{tabular}

\end{document}

This completely performs the loop in the first cell, so the problem with the orphan cell disappears.

5
  • with your fix, I get this error: ! Undefined control sequence. \fillTable ->\seq_use:Nn \g_hurzelchen_cells_seq {} l.29 \fillTable
    – Hurzelchen
    Oct 17, 2013 at 15:18
  • and actually... shouldn't the last \\ \hline result in the end of the "g h i"-row with the bottom border?
    – Hurzelchen
    Oct 17, 2013 at 15:22
  • @Hurzelchen \seq_use:Nn has been added in the 2013-05-26 release, so you have an outdated expl3 suite. I don't understand your second comment.
    – egreg
    Oct 17, 2013 at 15:26
  • @Hurzelchen Oh, sorry! I changed the sequence name in only one place
    – egreg
    Oct 17, 2013 at 15:33
  • Your second solution works perfect for me, thanks! I think you just mixed up the two code blocks and their descriptions ;)
    – Hurzelchen
    Oct 18, 2013 at 8:26

You must log in to answer this question.

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