如何使用 tabulary 将合并的列居中?

如何使用 tabulary 将合并的列居中?

我试图将单元格内容居中,但在合并单元格中却无法实现。在普通单元格中可以正常使用,但在合并单元格中却无法使用。

有人知道如何修复它吗?

这是我的代码

\documentclass{article}

\usepackage{tabulary,booktabs}

\begin{document}

\begin{center}
\begin{tabulary}{\linewidth}{C|C|C|C|C|C|C|}

\toprule

\multicolumn{7}{>{\centering\arraybackslash}m{13cm}}{Lorem ipsum dolor sit amet} \\

\midrule
\midrule

\multicolumn{7}{>{\centering\arraybackslash}m{13cm}}{Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes} \\

\midrule

\multicolumn{7}{>{\centering\arraybackslash}m{13cm}}{Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus.} \\

\midrule

Vivamus Integer tincidunt  & elementum & semper & nisi & Aenean & vulputate & eleifend \\

\midrule

Donec & vitae & sapien & libero Integer tincidunt & venenatis & faucibus & libero \\

\bottomrule

\end{tabulary}
\end{center}

\end{document}

答案1

我不确定我是否完全理解了您的问题,但这里有一种方法可以制作一个表格,其中第一行换行multicolumn。显然,您必须m{}根据需要调整列描述符中的长度。

\documentclass{article}

\usepackage{tabulary,booktabs}

\begin{document}

\begin{tabulary}{\linewidth}{CCCC}
\toprule
\multicolumn{4}{>{\centering\arraybackslash}m{2.65cm}}{Table This is a very long title for my table} \\
\midrule
\midrule
0 & 14 & 18 & 32 \\
\midrule
1 & 47 & 67 & 114 \\
\midrule
5 & 81 & 61 & 142 \\
\bottomrule
\end{tabulary}

\end{document}

输出结果如下:

输出1

笔记

如果您想稍微自动化一下这个过程,您可以使用环境tabularx并在序言中为表格定义一个一般长度。

\documentclass{article}

\usepackage{tabularx,booktabs}

\newlength{\tablelength}
\newlength{\titlelength}
\setlength{\tablelength}{6cm}
\setlength{\titlelength}{\tablelength}
\addtolength{\titlelength}{-1.5em}

\begin{document}

\begin{tabularx}{\tablelength}{>{\centering\arraybackslash}X>{\centering\arraybackslash}X>{\centering\arraybackslash}X>{\centering\arraybackslash}X}
\toprule
\multicolumn{4}{>{\centering\arraybackslash}m{\titlelength}}{Table This is a very long title for my table} \\
\midrule
\midrule
0 & 14 & 18 & 32 \\
\midrule
1 & 47 & 67 & 114 \\
\midrule
5 & 81 & 61 & 142 \\
\bottomrule
\end{tabularx}

\end{document}

它产生这个输出

输出 2

答案2

对于一般的跨度,使用当前界面并不容易tabulary,但在这里您跨越整个表格,因此您提前知道确切的宽度是\linewidth减去规则的宽度和单元格填充:

请注意,使用垂直规则booktabs是一个奇怪的组合,因为该包有意针对没有垂直规则的设计。

在此处输入图片描述

\documentclass{article}

\usepackage{tabulary,booktabs}

\begin{document}

\begin{center}
\begin{tabulary}{\linewidth}{|C|C|C|C|C|C|C|}

\toprule

\multicolumn{7}
{|>{\centering\arraybackslash}m{\dimexpr\textwidth-2\tabcolsep-2\arrayrulewidth}|}
{Lorem ipsum dolor sit amet} \\

\midrule
\midrule

\multicolumn{7}
{|>{\centering\arraybackslash}m{\dimexpr\textwidth-2\tabcolsep-2\arrayrulewidth}|}
{Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes} \\

\midrule

\multicolumn{7}
{|>{\centering\arraybackslash}m{\dimexpr\textwidth-2\tabcolsep-2\arrayrulewidth}|}
{Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus.} \\

\midrule

Vivamus Integer tincidunt  & elementum & semper & nisi & Aenean & vulputate & elei\-fend \\

\midrule

Donec & vitae & sapien & libero Integer tincidunt & venenatis & faucibus & libero \\

\bottomrule

\end{tabulary}
\end{center}

\end{document}

相关内容