具有非 X 列的 {tabu} 中的 \multicolumn

具有非 X 列的 {tabu} 中的 \multicolumn

tabu环境(tabu2.8 包)中,我习惯于\multicolumn省略一些单元格的前言(有时会跨越多列)。不幸的是,当存在非 X 列时,这会破坏宽度计算:如果\multicolumn{1}{l}{TEXT}比最宽的正常单元格更宽,则整个表格会变得更宽。

手册中提到这是一个已知问题,可以通过包含来修复\tabuphantomline,但我没有看到任何效果。

MNWE:

\documentclass{article}
\usepackage{tabu}
\usepackage{tabularx}
\pagestyle{empty}
\setlength{\parindent}{0pt}
\setlength{\parskip}{1em}
\begin{document}
This document illustrates difficulties with the \texttt{tabu} environment. All
tables are supposed to be \verb`\textwidth` wide, just like the present
paragraph.

\verb`tabularx` for reference: \\
\begin{tabularx}{\textwidth}{>{\ttfamily}lX}
  \hline
  foo & wibble \\
  \multicolumn{1}{l}{foo} & wibble \\
  \hline
\end{tabularx}

\verb`tabu` with a \verb`\multicolumn`: \\
\begin{tabu}{>{\ttfamily}lX}
  \hline
  foo & wibble \\
  \multicolumn{1}{l}{multicolumn} & wibble \\
  \hline
\end{tabu}

\verb`tabu` with a \verb`\multicolumn` and a \verb`\tabuphantomline`: \\
\begin{tabu}{>{\ttfamily}lX}
  \hline
  \tabuphantomline
  foo & wibble \\
  \tabuphantomline
  \multicolumn{1}{l}{multicolumn} & wibble \\
  \tabuphantomline
  \hline
\end{tabu}

\end{document}

tabu-多列-mwe.png

我怎样才能\multicolumn按预期工作(影响它所替换的列的宽度,但不影响整个表格的宽度,如上所示tabularx?)

答案1

\multicolumn仅当您使用命令并将1其作为第一个参数传递给它并使用非 X 列类型时,才会出现此问题。比较以下内容:

\documentclass{article}
\usepackage{tabu}
\usepackage{parskip}

\begin{document}

This document illustrates difficulties with the \texttt{tabu} environment.
All tables are supposed to be \verb`\textwidth` wide, just like the present paragraph.

\verb`tabu` with a \verb`\multicolumn` and a \verb`\tabuphantomline`:

\begin{tabu}{>{\ttfamily}lX}
  \hline
  foo & wibble \\
  \multicolumn{1}{c}{multicolumn} & wibble \\
  \hline
  \tabuphantomline
\end{tabu}

\begin{tabu}{>{\ttfamily}llX}
  \hline
  foo & wibble & wobble \\
  \multicolumn{2}{c}{multicolumn} & wibble \\
  \hline
  \tabuphantomline
\end{tabu}

\end{document}

生成结果:

在此处输入图片描述

因此,当存在非 X 列类型时,这似乎是一个有关如何\tabuphantomline处理\multicolumn宽度为 的错误。1

如果您只是使用\multicolumn宽度为1来更改单个单元格的水平对齐方式,则可以定义一个名为 的新宏\CenteredCell,例如,从而避免使用\multicolumn{1}...\tabuphantomline

\documentclass{article}
\usepackage{tabu}
\usepackage{parskip}

\newcommand*{\CenteredCell}[1]{%
  \begin{tabular}{@{}>{\rmfamily}c@{}}
    #1%
  \end{tabular}}

\begin{document}

This document illustrates difficulties with the \texttt{tabu} environment.
All tables are supposed to be \verb`\textwidth` wide, just like the present paragraph.

\verb`tabu` with a \verb`\multicolumn` and a \verb`\tabuphantomline`:

\begin{tabu}{>{\ttfamily}lX}
  \hline
  foo & wibble \\
  \multicolumn{1}{c}{multicolumn} & wibble \\
  \hline
  \tabuphantomline
\end{tabu}

\begin{tabu}{>{\ttfamily}lX}
  \hline
  foo & wibble \\
  \CenteredCell{multicolumn} & wibble \\
  \hline
\end{tabu}

\end{document}

得出的结果为:

在此处输入图片描述

相关内容