多行/多列中的居中和换行

多行/多列中的居中和换行

我正在尝试制作如下表格: 在此处输入图片描述

这是我当前的代码:

\documentclass[a4paper, 12pt]{article}

%Tables
\usepackage{multirow}
\usepackage{array}
\usepackage{tabu}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\titleformat{\section}
{\normalfont\large\bfseries}{\thesection}{1em}{}
\titleformat{\subsection}
{\normalfont\large\bfseries}{\thesubsection}{1em}{}

\begin{document}

\hline
\multirow{3}{4em}{\center{Time elapsed (s) $\pm$1s}} & \multicolumn{9}{|c|}{Volume of gas produced (cm$^3$) $\pm$1cm$^3$}\\\cline{2-10}
& \multicolumn{3}{|P{9em}|}{0.42mol/dm$^3$ C$_2$H$_4$O$_2$ and 0.4mol/dm$^3$ of NaHCO3} & \multicolumn{3}{|P{9em}|}{0.42mol/dm$^3$ C$_2$H$_4$O$_2$ and 0.4mol/dm$^3$ of NaHCO3} & \multicolumn{3}{|P{9em}|}{0.84mol/dm$^3$ C$_2$H$_4$O$_2$ and 0.8mol/dm$^3$ of NaHCO$_3$}\\\cline{2-10}
& Trial 1 & Trial 2 & Trial 3 & Trial 1 & Trial 2 & Trial 3 & Trial 1 & Trial 2 & Trial 3\\
\hline
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
\hline
15 & 20 & 22 & 18 & 25 & 15 & 17 & 5 & 5 & 6\\
\hline
30& 27& 29& 25& 31& 25& 22& 8&  8&  8\\
\hline
45& 32& 33& 29& 35& 32& 28& 10& 11& 11\\
\hline
60& 35& 36& 33& 38& 38& 34& 12& 13  12\\
\hline

\end{tabular}
\end{center}

\end{document}

大部分都是好的,但是多列的居中是关闭的,我不确定如何修复它,左上角的多行也是如此。

答案1

您的文档示例存在一些问题,例如:

  • 它不完整(缺少“\begin{tabular}
  • 表格太大,无法容纳文本宽度(在下面的 MWE 中增加了文本宽度并减小了字体大小)
  • 对于化学公式来说,使用这个mhchem包是明智的
  • 对于带有数字的单位和列,使用该siunitx包很方便
\documentclass[12pt]{article}
\usepackage[a4paper, margin=25mm]{geometry}
\usepackage{makecell, multirow, tabularx}
\usepackage[version=4]{mhchem}
\usepackage{siunitx}
\usepackage{xparse}
\NewExpandableDocumentCommand\mcc{O{1}m}
    {\multicolumn{#1}{c|}{#2}}
\NewExpandableDocumentCommand\mcx{O{1}m}
    {\multicolumn{#1}{>{\centering\arraybackslash}X|}{#2}}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%


\begin{document}
    \begin{table}[htb]
\caption{My wide table}
\label{tab:wide}
    \footnotesize
\setlength\tabcolsep{1pt}
\setcellgapes{3pt}
\makegapedcells
    \begin{tabularx}{\linewidth}{|*{10}{S[table-format=2.0]|}}
    \hline
\multicolumn{1}{|X|}{{\multirow{5}{=}{\centering Time elapsed\\ \si{s \pm 1 s}}}}
    & \mcc[9]{Volume of gas produced (\si{cm^3 \pm 1cm^3})}  \\
    \cline{2-10}
    & \mcc[3]{\makecell{\SI{0.42}{mol/dm^3} \ce{C2H4O2} and\\ \SI{0.4}{mol/dm^3} of \ce{NaHCO3}}}
        & \mcc[3]{\makecell{\SI{0.42}{mol/dm^3} \ce{C2H4O2} and\\ \SI{0.4}{mol/dm^3} of \ce{NaHCO3}}}
            & \mcc[3]{\makecell{\SI{0.84}{mol/dm^3} \ce{C2H4O2} and\\ \SI{0.8}{mol/dm^3} of \ce{NaHCO3}}}\\
    \cline{2-10}
    & \mcx{Trial 1} 
        & \mcx{Trial 2}
            & \mcx{Trial 3}
                    & \mcx{Trial 1}
                        & \mcx{Trial 2}
                            & \mcx{Trial 3}
                                    & \mcx{Trial 1}
                                        & \mcx{Trial 2}
                                            & \mcx{Trial 3}     \\
        \hline
 0  & 0     & 0     & 0     & 0     & 0     & 0     & 0     & 0     & 0\\
    \hline
15  & 20    & 22    & 18    & 25    & 15    & 17    & 5     & 5     & 6\\ 
    \hline
30  & 27    & 29    & 25    & 31    & 25    & 22    & 8     & 8     & 8\\
    \hline
45  & 32    & 33    & 29    & 35    & 32    & 28    & 10    & 11    & 11\\
    \hline
60  & 35    & 36    & 33    & 38    & 38    & 34    & 12    & 13    & 12\\
    \hline
    \end{tabularx}
    \end{table}
\end{document}

在此处输入图片描述

(红线显示部分页面布局)

答案2

以下是 Zarko 答案的变体。除了{tabularx},您还可以{NiceTabular}使用nicematrix

hvlines由于您只需指定密钥即可获得所有预期的规则,因此规则更加简单。

但是,没有列类型X{NiceTabular}这就是为什么我要计算列的宽度,\colwith以便表格完全适合\textwidth

\documentclass[12pt]{article}
\usepackage[a4paper, margin=25mm]{geometry}
\usepackage{nicematrix}
\usepackage[version=4]{mhchem}
\usepackage{siunitx}


\begin{document}

\NiceMatrixOptions{cell-space-limits=3pt}

\begin{table}[htb]
\caption{My wide table}

\footnotesize

\setlength\tabcolsep{0pt}

% We compute the width of the columns to fit in \textwidth
\newlength{\colwidth}
\setlength{\colwidth}{(\textwidth-11\arrayrulewidth)/10}

\begin{NiceTabular}{*{10}{S[table-format=2.0]}}[hvlines,columns-width = \colwidth]
\Block{3-1}{Time\\ elapsed\\ \unit{s} $\pm$ \qty{1}{s}}
    & \Block{1-9}{Volume of gas produced (\unit{cm^3} $\pm$ \qty{1}{cm^3})}  \\
        & \Block{1-3}{\qty{0.42}{mol/dm^3} \ce{C2H4O2} and\\ \qty{0.4}{mol/dm^3} of \ce{NaHCO3}} &&
            & \Block{1-3}{\qty{0.42}{mol/dm^3} \ce{C2H4O2} and\\ \qty{0.4}{mol/dm^3} of \ce{NaHCO3}} &&
                 & \Block{1-3}{\qty{0.84}{mol/dm^3} \ce{C2H4O2} and\\ \qty{0.8}{mol/dm^3} of \ce{NaHCO3}} \\
\RowStyle{\bfseries}
    & {Trial 1} 
        & {Trial 2}
            & {Trial 3}
                 & {Trial 1}
                      & {Trial 2}
                           & {Trial 3}
                                & {Trial 1}
                                     & {Trial 2}
                                          & {Trial 3}     \\
 0  & 0     & 0     & 0     & 0     & 0     & 0     & 0     & 0     & 0\\
15  & 20    & 22    & 18    & 25    & 15    & 17    & 5     & 5     & 6\\ 
30  & 27    & 29    & 25    & 31    & 25    & 22    & 8     & 8     & 8\\
45  & 32    & 33    & 29    & 35    & 32    & 28    & 10    & 11    & 11\\
60  & 35    & 36    & 33    & 38    & 38    & 34    & 12    & 13    & 12\\
\end{NiceTabular}
\end{table}
\end{document}

上述代码的输出

答案3

如果你坚持使用垂直和水平规则、垂直和水平跨越单元格等,你应该看看这个包卡路里. 设置起来非常简单。

此外,我还使用了KOMA 脚本类,因为它具有合理的边距和更改标题的内部命令。

我设置的方式calstable,它可能跨越多个页面。它不是浮动的,我使用

\captionaboveof{table}{My wide table\label{tab:wide}} 

以获得正确格式的标题。-\label命令位于 caption 命令内部,因此不会丢失。

最好的办法当然是废除大部分的规则,使用书签以及用于对行和列进行分组的额外间距,使表格更具可读性。

在此处输入图片描述

\documentclass[british, DIV=12, captions=tableheading]{scrartcl}
\usepackage{cals, url}
\usepackage{babel}
\usepackage{siunitx}
\usepackage[version=4]{mhchem}
\usepackage[T1]{fontenc}

\let\nc=\nullcell                                                  % Shortcuts
\let\sc=\spancontent

\addtokomafont{caption}{\sffamily\bfseries\small}
\setkomafont{captionlabel}{\normalfont}

\begin{document}

\bigskip

\begin{calstable}[c]

\captionaboveof{table}{My wide table\label{tab:wide}}


% Defining columns relative to each other and relative to the margins
\colwidths{{\dimexpr(\columnwidth)/10\relax}
            {\dimexpr(\columnwidth)/10\relax}
            {\dimexpr(\columnwidth)/10\relax}
            {\dimexpr(\columnwidth)/10\relax}
            {\dimexpr(\columnwidth)/10\relax}
            {\dimexpr(\columnwidth)/10\relax}
            {\dimexpr(\columnwidth)/10\relax}
            {\dimexpr(\columnwidth)/10\relax}
            {\dimexpr(\columnwidth)/10\relax}
            {\dimexpr(\columnwidth)/10\relax}
    }
% The tabular fills the text area if sum of all columns is 10

% Set up the tabular
\makeatletter
\def\cals@framers@width{0.8pt}   % Outside frame rules, reduce if the rule is too heavy
\def\cals@framecs@width{0.4pt}
\def\cals@bodyrs@width{0.6pt}
\def\cals@cs@width{0.4pt}             % Inside rules, reduce if the rule is too heavy
\def\cals@rs@width{0.6pt}
\def\cals@bgcolor{}

\setlength{\cals@paddingL}{3.2pt}
\setlength{\cals@paddingR}{3.2pt}


% R1H1
\thead{\sffamily\small%           The first 3 rows are heading and vill be repeated on every page
\brow
    \nc{lrt}
    \nc{ltb}
    \nc{tb}
    \nc{tb}
    \nc{tb}
    \nc{tb}
    \nc{tb}
    \nc{tb}
    \nc{tb}
    \nc{rtb}\alignC\sc{Volume of gas produced (\si{cm^3 \pm 1cm^3})}
\erow
%R2H2
\brow
    \nc{lr}
    \nc{ltb}
    \nc{tb}
    \nc{rtb}\sc{\SI{0.42}{mol/dm^3} \ce{C2H4O2} and \SI{0.4}{mol/dm^3} of \ce{NaHCO3}}
    \nc{ltb}
    \nc{tb}
    \nc{rtb}\sc{\SI{0.42}{mol/dm^3} \ce{C2H4O2} and\\ \SI{0.4}{mol/dm^3} of \ce{NaHCO3}}
    \nc{ltb}
    \nc{tb}
    \nc{rtb}\sc{\SI{0.84}{mol/dm^3} \ce{C2H4O2} and\\ \SI{0.8}{mol/dm^3} of \ce{NaHCO3}}
\erow
%R3H3
\brow
    \nc{lrb}\alignC\sc{\vfil Time elapsed \si{s \pm 1 s}}
    \bfseries\cell{Trial 1}
    \cell{Trial 2}
    \cell{Trial 3}
    \cell{Trial 1}
    \cell{Trial 2}
    \cell{Trial 3}
    \cell{Trial 1}
    \cell{Trial 2}
    \cell{Trial 3}\normalfont
\erow
}
\tfoot{\lastrule\strut}
%R4B1
\brow
    \cell{15}
    \cell{20}
    \cell{22}
    \cell{18}
    \cell{25}
    \cell{15}
    \cell{17}
    \cell{5}
    \cell{5}
    \cell{6}
\erow
%R5B2
\brow
    \cell{30}
    \cell{27}
    \cell{29}
    \cell{25}
    \cell{31}
    \cell{25}
    \cell{22}
    \cell{8}
    \cell{8}
    \cell{8}
\erow

%R6B3
\brow
    \cell{45}
    \cell{32}
    \cell{33}
    \cell{29}
    \cell{35}
    \cell{32}
    \cell{28}
    \cell{10}
    \cell{11}
    \cell{11}
\erow
%R7B4
\brow
    \cell{60}
    \cell{35}
    \cell{36}
    \cell{33}
    \cell{38}
    \cell{38}
    \cell{34}
    \cell{12}
    \cell{13}
    \cell{12}
\erow
%R8B5
\brow
    \cell{75}
    \cell{38}
    \cell{38}
    \cell{36}
    \cell{41}
    \cell{42}
    \cell{38}
    \cell{14}
    \cell{15}
    \cell{14}
\erow
%R9B6
\brow
    \cell{90}
    \cell{41}
    \cell{42}
    \cell{38}
    \cell{43}
    \cell{46}
    \cell{42}
    \cell{15}
    \cell{17}
    \cell{16}
\erow
%R10B7
\brow
    \cell{105}
    \cell{42}
    \cell{44}
    \cell{41}
    \cell{44}
    \cell{50}
    \cell{46}
    \cell{16}
    \cell{19}
    \cell{17}
\erow
%R11B8
\brow
    \cell{120}
    \cell{44}
    \cell{45}
    \cell{43}
    \cell{46}
    \cell{53}
    \cell{48}
    \cell{18}
    \cell{21}
    \cell{18}
\erow
%R12B9
\brow
    \cell{135}
    \cell{46}
    \cell{47}
    \cell{44}
    \cell{47}
    \cell{55}
    \cell{51}
    \cell{18}
    \cell{22}
    \cell{20}
\erow
%R13B10
\brow
    \cell{150}
    \cell{48}
    \cell{49}
    \cell{46}
    \cell{49}
    \cell{58}
    \cell{54}
    \cell{19}
    \cell{24}
    \cell{22}
\erow
%R14B11
\brow
    \cell{165}
    \cell{50}
    \cell{51}
    \cell{47}
    \cell{50}
    \cell{60}
    \cell{56}
    \cell{20}
    \cell{25}
    \cell{23}
\erow
%R15B12
\brow
    \cell{180}
    \cell{51}
    \cell{52}
    \cell{49}
    \cell{51}
    \cell{61}
    \cell{58}
    \cell{21}
    \cell{26}
    \cell{25}
\erow

\makeatletter
\end{calstable}

\end{document}

相关内容