同时使用 xltabular 和 floatrow 会导致宽度不正确

同时使用 xltabular 和 floatrow 会导致宽度不正确

floatrow设置似乎会中断xltabular,但仅在某些情况下。如果所有列都有X,它将正常工作。另请注意,tabularx不会受到影响。

考虑以下 MWE:

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{tabularx}
\usepackage{xltabular}
\usepackage{floatrow}
\usepackage{booktabs}

\floatsetup[table]{font = small, capposition = top}

\begin{document}
\begin{xltabular}{\linewidth}{|ccX|}
    \caption{Not correct.} \\
    \toprule
    1 & 2 & 3 \\
    \bottomrule
\end{xltabular}
\begin{xltabular}{\linewidth}{|ccX|}
    \caption{Not correct either.} \\
    \toprule
    1 longer text & 2 emmm & 3 \\
    \bottomrule
\end{xltabular}

\begin{xltabular}{\linewidth}{|XXX|}
    \caption{But this works.} \\
    \toprule
    1 longer text & 2 emmm & 3 \\
    \bottomrule
\end{xltabular}

\null

\begin{table}[h]
\caption{No problem for \texttt{tabularx}.}
\begin{tabularx}{\linewidth}{|ccX|}
    \toprule
    1 longer text & 2 emmm & 3 \\
    \bottomrule
\end{tabularx}
\end{table}

\end{document}

结果

答案1

这是我查看.sty源文件后找到的解决方案。

floatrowxltabular都对 有一些定义\LTxxx,因此floatrow在 之前加载xltabular可以解决宽度问题。关于floatrow和 的一些相关讨论longtable这里

副作用是\floatsetup现在字体设置不再适用。作为一种解决方法,我们可以\renewenvironmentxltabular字体大小硬编码到其中。

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{tabularx}
\usepackage{floatrow}
\usepackage{xltabular}
\usepackage{booktabs}

\makeatletter
\renewenvironment{xltabular}[1][x]%
{%
  \small % <-- font size change
  \par
  \if l#1%
    \LTleft\z@ \LTright\fill
  \else\if r#1%
    \LTleft\fill \LTright\z@
  \else\if c#1%
    \LTleft\fill \LTright\fill
  \fi\fi\fi
  \let\TX@endtabularx=\XLT@ii@TX@endtabularx
  \let\endtabularx\endxltabular
  \XLT@ii@tabularx}
{\def\@currenvir{tabularx}}
\makeatletter

\floatsetup[table]{font = small, capposition = top}

\begin{document}
\begin{xltabular}{\linewidth}{|ccX|}
    \caption{Not correct.} \\
    \toprule
    1 & 2 & 3 \\
    \bottomrule
\end{xltabular}
\begin{xltabular}{\linewidth}{|ccX|}
    \caption{Not correct either.} \\
    \toprule
    1 longer text & 2 emmm & 3 \\
    \bottomrule
\end{xltabular}

\begin{xltabular}{\linewidth}{|XXX|}
    \caption{But this works.} \\
    \toprule
    1 longer text & 2 emmm & 3 \\
    \bottomrule
\end{xltabular}

\null

\begin{table}[h]
\caption{No problem for \texttt{tabularx}.}
\begin{tabularx}{\linewidth}{|ccX|}
    \toprule
    1 longer text & 2 emmm & 3 \\
    \bottomrule
\end{tabularx}
\end{table}

ALL is working now.

\end{document}

正确结果

相关内容