# LaTeX3: \clist_gpop has extra pop in tabularx

I asked about \clist_gpop and tabular yesterday: LaTeX3: token list from clist_pop can't display in tabular 2nd column

It's fixed. But when I change to use tabularx, it look like has extra pop, and it's affect by table alignment.

This is MWE:

\documentclass[12pt]{article}
\usepackage{expl3}
\usepackage{tabularx}
\ExplSyntaxOn

\begin{document}
\clist_new:N \g_sppmg_my_cl
\clist_gset:Nn \g_sppmg_my_cl {a,b,c,d,e,f,g,h}

\begin{tabularx}{\textwidth}{|c|}
% --------------------------------
\clist_gpop:NN \g_sppmg_my_cl \l_tmpa_tl
\tl_gset_eq:NN \g_tmpa_tl \l_tmpa_tl
\g_tmpa_tl'' \\
% --------------------------------
\end{tabularx}
\ExplSyntaxOff
\end{document}

1. In this code, the output is b (Should be a)
2. If the block between two % ---- use twice, it will output c and d two rows.
3. If change table alignment from c to X (with 2 block / 2 rows), output is e and f

Why ?

Note, I search tabularx and expl3, then I find expl3 code with tabularx , but I still don't know how to fix it.

• Use \clist_pop:NN instead, but I don't understand what you want to achieve here. If I remember correctly, tabularx is setting up a box first with its content, measuring the width, then recalculating – user31729 Jan 31 '18 at 22:08
• tabularx processes its contents twice. See tabularx processes its body multiple times; how do I know which cycle I am in? – Werner Jan 31 '18 at 22:19
• @Werner usually it's more than twice, it can be variable number depending how many \multicolumn you have and how many iterations it takes to find good width. – David Carlisle Jan 31 '18 at 23:25

The reason is that \clist_gpop:NN is wrong here, since tabularx is set in a box first, then the width of the non - X columns is determined, using the value to calculate potential X columns in, then the box is typeset after possible multiple further calculation steps.

Now the first 'usage' (the calculation stage here) pops 'a' from the given list there, leaving b as 'first' element, which is then displayed in the typeset stage with \tl_use:N \g_tmpa_tl after the actual 2nd pop operation. If more calculation steps occur, even more items from the \clist are popped (here, the tabularx table is processed only twice, so b remains)

This will happen with \clist_pop:NN too, but it does no harm then, since this occurs in a group (i.e. the table cell)

In total, it might be frowned on that mixing expl3 code and a LaTeX2e environment is awkward....

\documentclass[12pt]{article}
\usepackage{expl3}
\usepackage{tabularx}
\ExplSyntaxOn

\begin{document}
\clist_new:N \g_sppmg_my_cl
\clist_gset:Nn \g_sppmg_my_cl {a,b,c,d,e,f,g,h}

\begin{tabularx}{\textwidth}{|c|}
% --------------------------------
\clist_pop:NN \g_sppmg_my_cl \l_tmpa_tl
\tl_gset_eq:NN \g_tmpa_tl \l_tmpa_tl%
\tl_use:N \g_tmpa_tl'' \\
% --------------------------------
\end{tabularx}
\ExplSyntaxOff
\end{document}


• Thanks, so maybe change to other not pop method is the only way ? I use \tl_set_eq because in real case, I need use loop and put popped string to other table cell, that must be globally. (full code in my yesterday post ). Oh, I know mix expl3 code with LaTeX2e maybe bad, but ... If you need program something like table, do we have another choice ? – sppmg Jan 31 '18 at 23:04
• @sppmg yes, we have. You could hide your l3 code in a macro which is in LaTeX2e syntax (reasonable). Or (less serious) use pgf, or pythontex, or luatex... – Skillmon Jan 31 '18 at 23:13
• @Skillmon: I will change later on about the LaTeX2e wrapper – user31729 Jan 31 '18 at 23:14
• @Skillmon: Yes, that's clear after his/her comment – user31729 Jan 31 '18 at 23:17
• @Skillmon: Thanks. I'll try hide expl3 code to macro , but not sure it can apply to all case. LuaTeX maybe not good for me, because I remember it has (very) little problem for Chinese display(I need it).pgf, don't know it can program. pythontex first time know it. XD – sppmg Jan 31 '18 at 23:29

This is roughly how I'd do this. Set the list in your first cell to solve the multiple gpop problem.

\documentclass[12pt]{article}
\usepackage{expl3}
\usepackage{tabularx}
\ExplSyntaxOn
\clist_new:N \g_sppmg_my_cl
\newcommand\SetMyList[1]{
\clist_gset:Nn \g_sppmg_my_cl { #1 }
}
\newcommand\PopMyList[1]{
\clist_gpop:NN \g_sppmg_my_cl \l_tmpa_tl
\cs_gset_eq:NN #1 \l_tmpa_tl
%or \xdef#1{ \tl_use:N \l_tmpa_tl }
}
\ExplSyntaxOff

\begin{document}
\begin{tabularx}{\textwidth}{|c|}
\SetMyList{a,b,c,d,e,f,g,h}% Set the list in the first cell of tabularx
\PopMyList{\MyTMP}%
\MyTMP\\
\PopMyList{\MyTMP}%
\MyTMP\\
\end{tabularx}
\end{document}

• That's hiding method right ? So tabularx only see the \MyTMP content. – sppmg Jan 31 '18 at 23:49
• Yes, I wrote a wrapper around the \clist_gset:Nn function and around the \clist_gpop:NN function. If you don't have fancy l3 macros, \newcommand might be enough, else you should perhaps use xparse for this purpose. It also solves your problem of multiple evaluation tabularx does by redefining the list on each evaluation. – Skillmon Jan 31 '18 at 23:55
• Oh, Is \tl_use:N necessary ? Can we just use variable (\l_tmpa_tl) ? – sppmg Jan 31 '18 at 23:57
• No, it is not necessary. But I'd use \cs_gset_eq:NN or \tl_gset_eq:NN and not the \xdef approach. – Skillmon Feb 1 '18 at 0:18
• Your code give me a idea . Now I move gpop to tabularx outside and save popped string and table format (& and \\  ) to \g_sppmg_tableContent_tl by \tl_gput_right:Nx. After all done, only put \g_sppmg_tableContent_tl in the tabularx`. For define macros, because every table's format may different, so I am not sure write only one usage macro is good.(But it's still has independent advantage.) – sppmg Feb 1 '18 at 9:06