1

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.

sppmg
  • 73

2 Answers2

4

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}

enter image description here

  • 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
  • 1
    @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 –  Jan 31 '18 at 23:14
  • @Skillmon: Yes, that's clear after his/her comment –  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
  • @sppmg pgf has programming features, but is no match to l3, imho, in programming (but drawing with TikZ is great). – Skillmon Jan 31 '18 at 23:57
2

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}
Skillmon
  • 60,462
  • 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 01 '18 at 00: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_tlin thetabularx`. 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 01 '18 at 09:06
  • @sppmg I don't know your usage and for what you're coding this. But building the content of tabularx outside of it is a good idea if feasible. Note however, that this would have been possible without l3 (just LaTeX) in a simple manner, too. – Skillmon Feb 01 '18 at 09:14