3

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}

Markus Nißl
  • 267
  • 1
  • 6
  • 1
    Why would you use table if you don't add a caption? There's no law imposing that a tabular (or variant thereof) must live inside table. – egreg Oct 19 '23 at 13:51
  • The instruction \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 the tabular* environment equal \textwidth. Second, never use vertical rules in a proper tabular* 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
  • @egreg I provided a MWE. For that I needed a nested environment. In my real world document, tabularx is nested inside a table to which I do supply both a caption and a label ... both of which are totally unnessary in the context of my question. Hence MWE. – Markus Nißl Oct 20 '23 at 08:26
  • @Mico I provided a MWE that compiles and that demonstrates that either going for the column descriptor X or for tabularx in my custom environment breaks compilation. As indicated in my introduction, the aim is to use tabularx with X inside my custom environment. – Markus Nißl Oct 20 '23 at 08:29

1 Answers1

4

tabularx gathers everything up to the next \end{⟨current environment⟩}, and the name of the current environment is set up by \begin{...}. Therefore, if you do only

\newenvironment{MyTabular}{
  \begin{table}[!h]
    \tabularx{\textwidth}{|X|X|X|}\hline
    }{%
      \hline
    \endtabularx
  \end{table}
}

then \begin{MyTabular} will first save MyTabular as current environment name, but then \begin{table} changes the name of the current environment to table; tabularx tries to look up everything up to \end{table}, which it won't find, hence the errors.

You should use also \table and \endtable

\documentclass{article}
\usepackage{tabularx}

\newenvironment{MyTabular}[1][tbp]{% use !h as default only if you like pain \table[#1] \tabularx{\textwidth}{|X|X|X|} \hline }{% \% <-- I'd put this here, so you don't have to close every tabular with \ \hline \endtabularx \endtable }

\begin{document}

\begin{MyTabular} a & b & MyTabular \end{MyTabular}

\end{document}

enter image description here

EDIT After egreg's comment it came to me that you might be wanting to place the table where it is written in the code but you don't need a caption. In this case there is no need for a table environment, you can just use center (which adds a bit of space above and below).

\newenvironment{MyTabular}{%
  \center
  \tabularx{\textwidth}{|X|X|X|}
  \hline
  }{%
   \\% <-- I'd put this here, so you don't have to close every tabular with \\
   \hline
   \endtabularx
   \endcenter
}
campa
  • 31,130
  • The default placement should never be !h. – egreg Oct 19 '23 at 13:50
  • @egreg I agree but I don't feel entirely responsible for other people's bad habits ;-) I'll add a comment. – campa Oct 19 '23 at 13:52
  • +1 for your answer. – Sebastiano Oct 19 '23 at 21:18
  • @egreg @campa I provided a MWE. !h puts both tables underneath each other at the top of the document. Otherwise they would have been spread of the page. I intentionally used !h for the sake of a nice MWE. – Markus Nißl Oct 20 '23 at 08:33
  • @campa Thanks for pointing out that \begin{...} has an influence on the \newenvironment command. This is crucial to know and hence to understand why the original approach was not working. This was the only answer I was looking for. Thank you! – Markus Nißl Oct 20 '23 at 08:38
  • @MarkusNißl !h is still wrong (and LaTeX will replace it by !ht automatically). Note that it is often unwise to not include p in the possible placements, as otherwise you might get your floats flushed to the end of the chapter/document. – Skillmon Oct 23 '23 at 11:43