进一步增强生成表格的 \newcommand:是否可以允许段落中断?

进一步增强生成表格的 \newcommand:是否可以允许段落中断?

我想寻求 TeXpert 的帮助,以进一步完善这个答案:

\newcommand 带有多个参数,用于生成表

来自 mr. @egreg 。命令(或宏?)\mymacro完美运行,但只有一个限制:不允许在生成的单元格内分段(即使p{<width>}指定了)。可以克服这个限制吗?如果可以,如何克服?

MWE(取自@egreg先生的回答,并稍作修改以显示所述问题):

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
% user interface
\NewDocumentCommand{\mymacro}{ommm}
 {% #2 = column headers, #3 = row headers, #4 = entries
  \IfNoValueTF { #1 }
   {
    \gabriel_mymacro:nnnn { c *{ \clist_count:n { #2 } } { |c } } { #2 } { #3 } { #4 }
   }
   {
    \gabriel_mymacro:nnnn { #1 } { #2 } { #3 } { #4 }
   }
 }

% variables
\tl_new:N \l__gabriel_mymacro_body_tl
\tl_new:N \l__gabriel_mymacro_row_tl
\seq_new:N \l__gabriel_mymacro_entries_seq

% functions
\cs_new_protected:Nn \gabriel_mymacro:nnnn
 {
  % clear the tl containing the body and collect the entries
  \tl_clear:N \l__gabriel_mymacro_body_tl
  \seq_set_split:Nnn \l__gabriel_mymacro_entries_seq { , } { #4 }
  % make the first row
  \tl_put_right:Nx \l__gabriel_mymacro_body_tl
   {
    \clist_item:nn { #3 } { 1 }
   }
  \clist_map_function:nN { #2 } \__gabriel_mymacro_add_entry:n
  \tl_put_right:Nn \l__gabriel_mymacro_body_tl { \\ \hline }
  % build the following rows
  \int_step_inline:nn { \clist_count:n { #3 } - 1 }
   {
    \tl_put_right:Nx \l__gabriel_mymacro_body_tl
     {
      \clist_item:nn { #3 } { ##1 + 1 }
     }
    \seq_pop_left:NN \l__gabriel_mymacro_entries_seq \l__gabriel_mymacro_row_tl
    \tl_map_function:NN \l__gabriel_mymacro_row_tl \__gabriel_mymacro_add_entry:n
    \tl_put_right:Nn \l__gabriel_mymacro_body_tl { \\ }
   }
  % typeset the table
  \begin{tabular}{#1}
  \tl_use:N \l__gabriel_mymacro_body_tl
  \end{tabular}
 }

\cs_new_protected:Nn \__gabriel_mymacro_add_entry:n
 {
  \tl_put_right:Nn \l__gabriel_mymacro_body_tl { & #1 }
 }

\ExplSyntaxOff

\begin{document}

\mymacro{1,2,3}{A,B,C}{011,001}

\bigskip

\mymacro[l|cccc]{Apple,Banana,Cherry,Date}{X,Apple,Banana,Cherry,Date}{
 0000,0101,1111,1010
}

\bigskip

\mymacro[l|cccc]{Apple,Banana,Cherry,Date}{{},Apple,Banana,Cherry,Date}{
 {cell with words}000,0101,1111,1010
}

%Not working
\bigskip

\mymacro[l|p{0.3\textwidth}ccc]{Apple,Banana,Cherry,Date}{{},Apple,Banana,Cherry,Date}{
 {cell with 

paragraph}000,0101,1111,1010
}
\end{document}

产生的错误是段落在\mymacro结束之前结束,所以(我猜?)列表项被定义为短命令?有没有办法,如何重新定义它?

答案1

如果参数应该接受段落分隔符,请使用修饰符 +:

\documentclass{article}
\usepackage{xparse}

\NewDocumentCommand{\mymacrolong}{+m}
 {text}

\begin{document}

\mymacrolong{a\par b}
\end{document}

相关内容