# babel clashing with booktabs and tabularx in custom environment

I have a document like this, that is compiled normally:

\documentclass{article}

\usepackage{booktabs}
\usepackage{tabularx}

\newenvironment{myenv}
{\tabularx{\columnwidth}{X}\toprule}
{\bottomrule\endtabularx}

\begin{document}

\begin{myenv}
Foo\\
\end{myenv}

\end{document}


I also need to \usepackage[italian]{babel}, but if I do so and then compile I get the following error:

! Misplaced \noalign.
\bottomrule ->\noalign
{\ifnum 0=}\fi \@aboverulesep =\aboverulesep \global...
l.16 \end{myenv}


and in the PDF output the table has two \bottomorules, the first one aligned with the table, and the second one moved to the left:

How can I solve this?

Update: the error occurs only if babel is used in italian, all the other languages I tried work well.

• I must say that even if you load [italian,english] so that english is the main language, you have the error, whereas with [english]  alone, there's no error. That's very strange. Sep 8, 2020 at 11:06
• Depending on the document, the alternative way to define languages can be a solution. Perhaps not so complete, but in many cases will be enough: \usepackage{babel} \babelprovide[import,main]{italian}. Sep 8, 2020 at 14:54

The problem is etoolbox which is loaded by italian. It inserts a hook in the end-environment code and tabularx doesn't like this.

With the development version of latex, which you can use by calling pdflatex-dev it works, its hook code is more robust.

Alternatively you could try the b-argument of xparse:

\documentclass{article}
\usepackage[italian]{babel}
\usepackage{xparse}
\usepackage{booktabs}
\usepackage{tabularx}

\NewDocumentEnvironment{myenv}{b}
{\begin{tabularx}{\columnwidth}{X}\toprule #1  \bottomrule\end{tabularx}}
{}

\begin{document}

\begin{myenv}
Foo\\
\end{myenv}

\end{document}

• pdflatex-dev doesn't work for me, but etoolbox` does. Thanks for the explanation too! Sep 8, 2020 at 19:34