I would like to use tabularx in my own environment, but nested inside a table environment.
Please see my MWE below: I first produce the desired result without a custom environment. Then I try to accomplish the same output with my own environment. Alas that doesn't work:
When I switch from tabular* to tabularx, I get the compile error File ended while scanning use of \TX@get@body.. If I stay with tabular*, but switch the last column descriptor from c to X, I get another compile error: Extra alignment tab has been changed to \cr. a & b &.
In the answer to question tabularx inside a \newenvironment, one suggests to go for \tabularx and \ endtabularx, but that doesn't work in my case where tabularx is nested inside a table environment.
What am I doing wrong?
\documentclass{article}
\usepackage{tabularx}
\begin{document}
\begin{table}[!h]
\begin{tabularx}{\textwidth}{|c|c|X|}\hline
a & b & tabularx \\hline
\end{tabularx}
\end{table}
\newenvironment{MyTabular}{
\begin{table}[!h]
\begin{tabular}{\textwidth}{|c|c|c|}\hline
}{%
\hline
\end{tabular}
\end{table}
}
\begin{MyTabular}
a & b & MyTabular \
\end{MyTabular}
\end{document}

tableif you don't add a caption? There's no law imposing that atabular(or variant thereof) must live insidetable. – egreg Oct 19 '23 at 13:51\begin{tabular*}{\textwidth}{|c|c|c|}looks suspect for two separate reasons. First, you're not providing any mechanism that would enable LaTeX to achieve your goal of making the width of thetabular*environment equal\textwidth. Second, never use vertical rules in a propertabular*environment. In short, I think you should replace\begin{tabular*}{\textwidth}{|c|c|c|}with\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}ccc @{}}. – Mico Oct 19 '23 at 17:03