# Why does an empty tabularx throw an error?

Why does an empty tabularx throw an error ? (by empty, I mean only space, no newline)

MWE:

\documentclass{article}
\usepackage{tabularx}

\begin{document}

\begin{tabular}{|c | c|}
\end{tabular}                                        % <-- Works

\begin{tabular}{|c | c|}\end{tabular}                % <-- Works

\begin{tabularx}{\textwidth}{|c | c|}
\end{tabularx}                                        % <-- Works

\begin{tabularx}{\textwidth}{|c | c|}\end{tabularx}   % <-- Does not work

\end{document}


(This is particulary frustrating with \ExplSyntaxOn)

• "why" is probably that it didn't occur to me to test for this and trap it. – David Carlisle Apr 1 at 9:37
• Well... I guess it's probably a good reason ^^' – hl037_ Apr 1 at 9:54
• This code has been available for almost 30 years and you are the first person to report this issue:-) – David Carlisle Apr 1 at 9:56

"why" is probably that it didn't occur to me to test for this and trap it.

Looking at tracing all the reason is that in

\begin{tabularx}{\textwidth}{|c | c|}\end{tabularx}


The entire body (that tabularx needs to grab as a macro argument) is {|c | c|} and tex silently drops brace groups that surround an entire delimited argument, so this is passed to tabularx as

\begin{tabularx}{\textwidth}|c | c|\end{tabularx}


and things go wrong.

If you are generating this and there is a chance it might be empty you could use the optional argument

\begin{tabularx}{\textwidth}[c]{|c | c|}\end{tabularx}   % <-- Does not work


works for example.

I can probably catch this in a future release.

possible fix

\documentclass{article}
\usepackage{tabularx}

\makeatletter
\def\foo#1{%
\def\tabularx##1{%
\edef\TX@{\@currenvir}%
{\ifnum0=}\fi
\setlength\TX@target{##1}%
\TX@typeout{Target width: ##1 = \the\TX@target}%
\toks@{}\TX@get@body#1}}
\foo{ }
\makeatother
\begin{document}

\begin{tabularx}{\textwidth}{|c | c|}\end{tabularx}   % <-- Does not work

\end{document}
`
• In my case, it's not that a big deal once i know it can't be empty. I actually encountered it when debugging a macro to generate table, and I stuck on this error not knowing that an empty tabularx would error – hl037_ Apr 1 at 9:57
• @hl037_ see update – David Carlisle Apr 1 at 10:53