Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
AFAIK, it is not possible to span an environment across cell boundaries. –  quinmars Mar 6 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 Mar 6 at 14:34

1 Answer 1

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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.