6

Why can't I define an environment, that encloses another tabularx environment inside it?

For example, why can't I do this:

\newenvironment{customTabular}{
    % This is the begin code
    \begin{tabularx}{\linewidth}{l l X}
}
{
    % This is the end code
    \end{customTabular}
}

and then use it like:

\begin{customTabular}
        Blah & Blah & Blah\\
\end{customeTabular}
1
  • I think you meant \end{tabularx} instead of \end{customTabular} in environment definition. Sep 29, 2011 at 16:23

1 Answer 1

11

The tabular extensions are all based on hacking TeX's alignment system which requires some expansion intricacies (that's as much as I understand about it). One trick which sometimes works is to use the control sequences one level below the LaTeX abstraction. This works:

\documentclass{article}
\usepackage{tabularx}
\newenvironment{customTabular}{
    % This is the begin code
    \begingroup
    \tabularx{\linewidth}{l l X}
}
{
    % This is the end code
    \endtabularx
    \endgroup
}
\begin{document}
\begin{customTabular}
        Blah & Blah & Blah\\
\end{customTabular}
\end{document}

Edit: The \begingroup...\endgroup are in there because \begin{tabularx} begins a group then expands \tabularx. But since \begin{customTabular} begins its own group you don't need it for this simple case. I suppose if you wanted to have extra code after \endtabular that would not be affected by the contents of the environment you would need it.

6
  • Is there a good alternative to hacking tabular, for aligning paragraphs in table-like structure for example? As in my other question tex.stackexchange.com/questions/14164/position-line-in-a-resume Mar 24, 2011 at 17:27
  • 2
    why do you have \begingroup... \endgroup?
    – user2478
    Mar 24, 2011 at 17:30
  • @Herbert: because \begin{tabularx} begins a group, then expands \tabularx. I wanted the same thing. But on the other hand, a group is begun by \begin{customTabular} too...so I guess you're right, it's not necessary. Mar 24, 2011 at 17:58
  • @drozzy: your other question looks more like a description list with item heads rather than aligned paragraphs. So you could use a variation of the description environment. But résumés are a well known use case of LaTeX, and many classes exist such as the ones you were pointed to. Mar 25, 2011 at 11:59
  • 1
    @matthew Thanks! That solved my problem exactly. I wish I'd searched here hours ago. Jul 23, 2011 at 15:04

You must log in to answer this question.

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