使用 LaTeX3 语法生成仅具有可选参数的可扩展环境

使用 LaTeX3 语法生成仅具有可选参数的可扩展环境

我正在尝试创建一个新的文档环境来完成表格的组装(为 MWE 简化),但是在将可扩展环境与可选参数相结合时遇到了问题。

我产生的错误是xparse/expandable-ending-optional Argument specification for expandable command ends with optional argument.

\documentclass{article}

\usepackage{xparse}

\usepackage{environ,longtable,threeparttablex,booktabs,multirow,array,adjustbox,supertabular}% table adjustment packages
% change spacing/padding of long table:  http://tex.stackexchange.com/questions/5683/how-to-remove-top-and-bottom-whitespace-of-longtable
\setlength{\LTpre}{0pt}
\setlength{\LTpost}{0pt}

% Wrapping text in multicolumn:  http://tex.stackexchange.com/questions/115668/wrapping-text-in-multicolumn
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}
%http://tex.stackexchange.com/questions/70919/vertical-aligning-in-longtable-environment
%\adjustbox{valign=t}{}
%\raisebox{\normalbaselineskip-\height}{#6} & \multicolumn{2}{L{11cm}}{#7}
% changed m to p instead

\makeatletter
    \newcommand\eatpar{\@ifnextchar\par{\expandafter\eatpar\@gobble}\relax}
\makeatother

%--------------------New Commands for consistent formatting ---------------
\ExplSyntaxOn
% this one can't be a held in a local variable because the table paramaters are external
\DeclareDocumentCommand{\TableAssembly}{ m m }{%
    #1 & #2 \\%
}%

\DeclareExpandableDocumentCommand{\BuildTablePortion}{ O{R{2.5cm} | L{12.25cm}}}%
{%
    \begin{ThreePartTable}%
    \begin{TableNotes}[para,flushleft]%
    \end{TableNotes}%
    \renewcommand\multirowsetup{\raggedleft}%
    \begin{longtable}[l]{#1}%
}%

\DeclareExpandableDocumentCommand{\endBuildTablePortion}{ o }
{%
    %\insertTableNotes%
    \end{longtable}%
    \end{ThreePartTable}%
}%
\ExplSyntaxOff

\begin{document}
    \begin{BuildTablePortion}[R{2.5cm} | L{12.25cm}]
        \TableAssembly{1}{2}
        \TableAssembly{1}{3}
    \end{BuildTablePortion}
\end{document}

答案1

您可以执行以下操作,我还删除了所有不需要的%返回内容:-)

\documentclass{article}

\usepackage{xparse}

\usepackage{environ,longtable,threeparttablex,booktabs,multirow,array,adjustbox,supertabular}% table adjustment packages
% change spacing/padding of long table:  http://tex.stackexchange.com/questions/5683/how-to-remove-top-and-bottom-whitespace-of-longtable
\setlength{\LTpre}{0pt}
\setlength{\LTpost}{0pt}

% Wrapping text in multicolumn:  http://tex.stackexchange.com/questions/115668/wrapping-text-in-multicolumn
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}
%http://tex.stackexchange.com/questions/70919/vertical-aligning-in-longtable-environment
%\adjustbox{valign=t}{}
%\raisebox{\normalbaselineskip-\height}{#6} & \multicolumn{2}{L{11cm}}{#7}
% changed m to p instead

\makeatletter
    \newcommand\eatpar{\@ifnextchar\par{\expandafter\eatpar\@gobble}\relax}
\makeatother

%--------------------New Commands for consistent formatting ---------------
\ExplSyntaxOn
% this one can't be a held in a local variable because the table paramaters are external
\DeclareDocumentCommand{\TableAssembly}{ m m }{
    #1 & #2 \\
}

\DeclareDocumentCommand{\BuildTablePortion}{ O{R{2.5cm} | L{12.25cm}}}
{
    \begin{ThreePartTable}
    \begin{TableNotes}[para,flushleft]
    \end{TableNotes}
    \renewcommand\multirowsetup{\raggedleft}
    \begin{longtable}[l]{#1}
}

\def\endBuildTablePortion
{
    %\insertTableNotes
    \end{longtable}
    \end{ThreePartTable}
}
\ExplSyntaxOff

\begin{document}
    \begin{BuildTablePortion}[R{2.5cm} | L{12.25cm}]
        \TableAssembly{1}{2}
        \TableAssembly{1}{3}%
    \end{BuildTablePortion}
\end{document}

相关内容