2

My question concerns the combination of the tabular environment and the \DeclareDocumentEnvironment command (from the xparse package).

How do I set up an environment that contains in its code some of the operators of the tabular environment, such as & or \\ ?

Here is my minimal (not) working example of what I thought I could do, but I get an error :

\documentclass{article}
\usepackage{xparse,booktabs}
\DeclareDocumentEnvironment{env}{mm}{\toprule #1 & #2 &}{\\ \bottomrule}

\begin{document}
\begin{tabular}{lll}
\begin{env}{one}{two}
three
\end{env}
\end{tabular}
\end{document}

Any idea of what I could do to have this environment working Inside the tabular environment?

Thank you in advance,

Pierric

2
  • AFAIK, it is not possible to span an environment across cell boundaries.
    – quinmars
    Commented Mar 6, 2014 at 12:32
  • @quinmars It's not the only problem: \begin{env} expands to \env which is \protected, so expansion ends here as far as \halign is concerned for finding \noalign, so \toprule shows up too late, when a cell has already been started.
    – egreg
    Commented Mar 6, 2014 at 14:34

1 Answer 1

2

The \toprule will complain if something unexpandable was encountered before. You can circumvent the issue by using plain old commands:

tabular

\documentclass{article}
%\usepackage{xparse,booktabs}
%\DeclareDocumentEnvironment{env}{mm}{\toprule #1 & #2 &}{\\ \bottomrule}

\usepackage{booktabs}
\newcommand{\env}[2]{\toprule #1 & #2 &}
\newcommand{\envend}{\\ \bottomrule}

\begin{document}\thispagestyle{empty}
\begin{tabular}{lll}
\env{one}{two}
three
\envend
\env{four}{five}
six
\envend
\end{tabular}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .