4

This idea evolved from the question: How can I get a count of the optional arguments with xparse?

Problem

I cannot get the ampersand to function as a delineator (for tab stops) inside the \DeclareDocumentCommand{\mygroup}. I thought maybe I should be looking here: How can I give active characters definitions with expl3?. I am quite sure what the issue is, because the ampersand works in the DeclareDocumentEnvironment{mytab}{O{}}.

Working (not really, just does not crash when typesetting): \doargs:n{#1} & \\ % interestingly, this ampersand does not crash typesetting.

Not working: {\clist_item:Nn \arglist {##1}\&} % added backslash to & for debugging

Example

\documentclass{article}
\usepackage{fontspec}

\usepackage{xparse,expl3}

\ExplSyntaxOn % based on https://tex.stackexchange.com/a/243442/13552
\DeclareDocumentEnvironment{mytab}{O{}}%
{\noindent\begin{tabular}{*{50}{l}}}
{\end{tabular}}
\DeclareDocumentCommand{\mygroup}{O{}m}{
\doargs:n{#1} &  \\ % interestingly, this ampersand does not crash typesetting.
\multicolumn{\clist_count:N \arglist}{l}{\indent\parbox{\dimexpr\textwidth-\parindent\relax}{{\small\textit{#2}}}} \\%[\baselineskip]
}%
\clist_new:N\arglist
\cs_new_protected:Nn\doargs:n{{%
\clist_set:Nn\arglist{#1}%
\int_step_inline:nnnn {1}{1}{\clist_count:N \arglist}
    {\clist_item:Nn \arglist {##1}\&} % added backslash to & for debugging
}}%
\ExplSyntaxOff

\begin{document}

\begin{mytab}
\mygroup[
Special 1,
ID 1,
A,
B]
{Some nice long description.}

\mygroup[
Special 2,
ID 2,
A,
B]
{Some nice long description.}

\mygroup[
Special 3,
ID 3,
A,
B]
{Some nice long description.}
\end{mytab}

\end{document}
  • 3
    Tabular cells are groups. http://tex.stackexchange.com/questions/3108/use-column-separator-ampersand-inside-newenvironment/3110#3110 – Ulrike Fischer May 07 '15 at 12:06

1 Answers1

3

You're trying to start a loop in a cell, ending it in another one. This can't work, because alignment cells form groups.

You want to use \clist_use:Nn

\documentclass[draft]{article}
\usepackage{fontspec}

\usepackage{xparse,expl3}

\ExplSyntaxOn

\DeclareDocumentEnvironment{mytab}{O{}}
  {\begin{tabular}{*{50}{l}}}
  {\end{tabular}}

\clist_new:N \g_macmadness_mygroup_items_clist

\DeclareDocumentCommand{\mygroup}{O{}m}
 {
  \clist_gset:Nn \g_macmadness_mygroup_items_clist { #1 }
  \clist_use:Nn \g_macmadness_mygroup_items_clist { & } \\
  \multicolumn{ 50 } {@{}l@{}}
   {
    \hspace{\parindent}
    \parbox{\dim_eval:n { \textwidth-\parindent } } {\small\itshape #2}
   }
   \\[\normalbaselineskip]
 }

\ExplSyntaxOff

\begin{document}

\noindent
\begin{mytab}
\mygroup[
Special 1,
ID 1,
A,
B]
{Some nice long description.}

\mygroup[
Special 2,
ID 2,
A,
B]
{Some nice long description.}

\mygroup[
Special 3,
ID 3,
A,
B]
{Some nice long description.}
\end{mytab}

\end{document}

This expands the whole list with the stated separator, so TeX won't see & during the process, but only when it has ended.

enter image description here

egreg
  • 1,121,712
  • Thanks for the answer. Unfortunately, I seem to loose flexibility at every turn due to the simplicity of MWEs. I need to be able to use \int_step_inline:nnnn{2}{1}{\clist_count:N \g_macmadness_mygroup_items_clist}. I am going to have item 1 be left-aligned and the rest be right-aligned in the tabular. (Also, I will be using longtable, but I think that is superfluous here) – Jonathan Komar May 07 '15 at 12:50
  • Actually, I do not even want to simplify it that much. I just need to be able to access each item individually. Therefore, \clist_use isn't really an option, I think. – Jonathan Komar May 07 '15 at 13:18
  • @macmadness86 I can't read in your mind. – egreg May 07 '15 at 14:22
  • You say that often, I noticed. This is why I explicitly said what I want above. Your solution is quite restrictive (you cannot isolate items in the loop), which is why I left it unanswered for the time being. Perhaps in time it will be the best answer. I am already looking into other solutions (ways of spreading information automatically across a page horizontally where widths are calculated automatically based on the number of args other than tab stops so that I can use the int_step_inline iterator). – Jonathan Komar May 08 '15 at 07:06
  • 1
    @macmadness86 If you want to use an \int_step_inline:nnnn loop, you have to builld a token list step by step and deliver it only at the end. – egreg May 08 '15 at 09:22