# Producing expandable environments with only optional arguments with LaTeX3 syntax

I am trying to create a new document environment to complete the assembly of a table (simplified for MWE) but I am having a problem combining an expandable environment with an optional only argument.

The error I generate is xparse/expandable-ending-optional Argument specification for expandable command ends with optional argument.

\documentclass{article}

\usepackage{xparse}

% 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
%\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}

• What's the purpose of doing \DeclareExpandableDocumentCommand when you eventually do \begin{BuildTablePortion}? It makes no sense. What should an “expandable environment” be? – egreg Nov 10 '15 at 23:16
• What's the point of using \ExplSyntaxOn? You don't seem to use that syntax at all. For an explanation of the error see page 10 of xparse's manual: if you have any arguments, the last one must be of type m or r. That is, if you did need expandability, you couldn't use this argument specification. – cfr Nov 10 '15 at 23:27
• actually I mislead you in the comments on previous question, the command that you needed to be expandable was the end, so with the syntax used previously you needed to use the expandable form, but an environment end code is always expandable (enough) as it never takes arguments, so you should be able to use a standard declaration here. – David Carlisle Nov 10 '15 at 23:31

You can do the following, I also removed all the unneeded % which had come back:-)

\documentclass{article}

\usepackage{xparse}

% 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
%\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}

• I saw your comment previously about the unnecessary }% but I had been observing inconsistencies in my line breaks and cell distributions when I skipped the %. If they have 0% chance of being the responsible issue, I am not sure what I was doing other than implementing them. – EngBIRD Nov 10 '15 at 23:47
• @EngBIRD % at end of line within scope of \ExplSyntaxOn are never needed (that's more or less the point of the command) If you are going to use % you don't need \ExplSyntaxOn/Off – David Carlisle Nov 10 '15 at 23:49
• Thanks! I will watch carefully for scenarios, when adding % to the end of a line seemed to prevent the inclusion of a new line or something and post back if it is reproducible. – EngBIRD Nov 10 '15 at 23:56