0

I have an environment whose behavior depends some outer circumstances. In some cases, the environment should act as a table cell (and in other cases it should act as a paragraph). The appropriate \begin{tabular}{...} and \end{tabular} are printed by another macros, which also change their behavior appropriately.

However, I can't define the appropriate environment. The MWE is:

\documentclass{article}

\newenvironment{cell}{}{&}

\begin{document}

\begin{tabular}{lll} \begin{cell} abc \end{cell} \end{tabular}

\end{document}

What am I doing wrong and how can I fix it?

NickKolok
  • 657
  • 1
    The & has to be actually seen by the tabular, and not merely produced by some intermediate macro/environment. Thus, you would need \newenvironment{cell}{}{} and \begin{tabular}{lll} \begin{cell} abc \end{cell}&& \end{tabular} – Steven B. Segletes Aug 25 '23 at 22:14
  • 2
    basically you can either not use an environment for the cell, or not use \halign/tabular and make your environment produce boxes of known size that you can stack to make an alignment – David Carlisle Aug 25 '23 at 23:06

1 Answers1

0

Using this post: https://tex.stackexchange.com/a/8084/168600 I've found a solution:

\documentclass{article}

\usepackage{environ} \NewEnviron{cell}{% \expandafter\gdef\expandafter\gtemp\expandafter{% \BODY\expandafter& }% \aftergroup\gtemp } \begin{document}

\begin{tabular}{l|l|l|l} \begin{cell} abc \end{cell} \begin{cell} def \end{cell}

\end{tabular}

\end{document}

NickKolok
  • 657