5

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}
  • I think you meant \end{tabularx} instead of \end{customTabular} in environment definition. – Yuriy Petrovskiy Sep 29 '11 at 16:23
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.

  • 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 – Andriy Drozdyuk Mar 24 '11 at 17:27
  • 2
    why do you have \begingroup... \endgroup? – user2478 Mar 24 '11 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. – Matthew Leingang Mar 24 '11 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. – Matthew Leingang Mar 25 '11 at 11:59
  • 1
    @matthew Thanks! That solved my problem exactly. I wish I'd searched here hours ago. – Colin Fraizer Jul 23 '11 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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