babel 在自訂環境中與 booktabs 和 tabularx 發生衝突

babel 在自訂環境中與 booktabs 和 tabularx 發生衝突

我有一個這樣的文檔,可以正常編譯:

\documentclass{article}

\usepackage{booktabs}
\usepackage{tabularx}

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

\begin{document}

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

\end{document}

我也需要\usepackage[italian]{babel},但如果我這樣做然後編譯我會收到以下錯誤:

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

在 PDF 輸出中,表格有兩個\bottomorules,第一個與表格對齊,第二個向左移動:

在此輸入影像描述

我該如何解決這個問題?

更新:只有當 babel 在義大利語中使用時才會出現該錯誤,我嘗試過的所有其他語言都運作良好。

答案1

問題是etoolbox它是由意大利語加載的。它在最終環境程式碼中插入一個鉤子,而 tabularx 不喜歡這樣。

有了latex的開發版本,透過呼叫works即可使用pdflatex-dev,它的hook程式碼更加健壯。

或者你可以嘗試 xparse 的 b 參數:

\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}

相關內容