设置

设置

设置

使用下面的 MWE,我尝试将“标题 1”与 4 个子标题置于中心,将“标题 2”和“标题 3”与其子标题分别置于中心:

平均能量损失

\documentclass[12pt, oneside]{book}

%MARGIN SETTINGS
    \usepackage{geometry}
    \geometry{
    paper=a4paper, 
    inner=2.5cm, 
    outer=2.5cm, 
    bindingoffset=0.0cm, 
    top=2.5cm, 
    bottom=2.5cm,
    headsep=4.5mm, 
    footskip=7.4mm, 
    %showframe
    }

%FOR TABLES
\usepackage{float}
\usepackage{graphicx} 

\usepackage{booktabs}
\usepackage{multirow}
\usepackage{siunitx}
\newcolumntype{?}{!{\vrule width 1pt}}


%----------------------BEGIN-------------------------

\begin{document}


\begin{table}[H]
\centering
\resizebox{\textwidth}{!}{%
  \begin{tabular}{l|l ? S|S|S|S  ? S|S ? S|S} %S centres
  %\begin{tabular}{l|l ? S|S|S|S  | S|S | S|S}
    \toprule
    \multirow{2}{*}{Column 1} & 
    \multirow{2}{*}{Column 2} &  %& &
      \multicolumn{2}{c}{Heading 1} & %& 
      \multicolumn{2}{c}{Heading 2} & 
      \multicolumn{2}{c}{Heading 3} \\
       & & {Sub 1} & {Sub 2} & {Sub 3} & {Sub 4} & {Sub 1} & {Sub 2} & {Sub 1} & {Sub 2} \\
          \midrule
    A1 = item1 & 0.0001  & 1 & 1 & 1 & 2 & 3 & 4 & 5 & 6 \\
    %\hline
    A2 = item2 & 0.0002 & 1 & 1 & 7 & 8 & 9 & 10 & 11 & 12 \\
    %\hline
    A3 = item3 & 0.0003  & 1 & 1 & 13 & 14 & 15 & 16 & 17 & 18 \\
    \bottomrule
  \end{tabular}}
    \caption{Test Table} \label{tbl:BPF}
\end{table}

\end{document}

问题

如果我使用更多&'s 来对齐标题,则会出现以下错误。垂直线溢出到顶部:

在此处输入图片描述

目标

如何使标题居中(如上所示),但不要在“Sub 1 Sub 2”和“Sub 4 Sub 1”之间出现溢出的垂直线

答案1

我不确定你想要什么。你的意思是这样吗?如果是这样:你必须指定正确的列数\multicolumn(对于“标题 1”,则为 4)。要使用垂直规则,只需将它们添加到 的列类型规范中即可\multicolumn

\documentclass[12pt, oneside]{book}

%MARGIN SETTINGS
    \usepackage{geometry}
    \geometry{
    paper=a4paper, 
    inner=2.5cm, 
    outer=2.5cm, 
    bindingoffset=0.0cm, 
    top=2.5cm, 
    bottom=2.5cm,
    headsep=4.5mm, 
    footskip=7.4mm, 
    %showframe
    }

%FOR TABLES
\usepackage{float}
\usepackage{graphicx} 

\usepackage{booktabs}
\usepackage{multirow}
\usepackage{siunitx}
\newcolumntype{?}{!{\vrule width 1pt}}


%----------------------BEGIN-------------------------

\begin{document}


\begin{table}[H]
\centering
\resizebox{\textwidth}{!}{%
  \begin{tabular}{l|l ? S|S|S|S  ? S|S ? S|S} %S centres
  %\begin{tabular}{l|l ? S|S|S|S  | S|S | S|S}
    \toprule
    \multirow{2}{*}{Column 1} & 
    \multirow{2}{*}{Column 2} &  %& &
      \multicolumn{4}{c?}{Heading 1} & %& 
      \multicolumn{2}{c?}{Heading 2} & 
      \multicolumn{2}{c}{Heading 3} \\
       & & {Sub 1} & {Sub 2} & {Sub 3} & {Sub 4} & {Sub 1} & {Sub 2} & {Sub 1} & {Sub 2} \\
          \midrule
    A1 = item1 & 0.0001  & 1 & 1 & 1 & 2 & 3 & 4 & 5 & 6 \\
    %\hline
    A2 = item2 & 0.0002 & 1 & 1 & 7 & 8 & 9 & 10 & 11 & 12 \\
    %\hline
    A3 = item3 & 0.0003  & 1 & 1 & 13 & 14 & 15 & 16 & 17 & 18 \\
    \bottomrule
  \end{tabular}}
    \caption{Test Table} \label{tbl:BPF}
\end{table}

\end{document}

在此处输入图片描述

一般来说,调整表格大小被认为是不好的做法。相反,您可以使用\small使其适合文本宽度。此外,表格往往看起来更好,因为垂直线较少(读作“没有”)。您可以考虑使用以下方法:

\documentclass[12pt, oneside]{book}

%MARGIN SETTINGS
    \usepackage{geometry}
    \geometry{
    paper=a4paper, 
    inner=2.5cm, 
    outer=2.5cm, 
    bindingoffset=0.0cm, 
    top=2.5cm, 
    bottom=2.5cm,
    headsep=4.5mm, 
    footskip=7.4mm, 
    %showframe
    }

%FOR TABLES
\usepackage{float}
\usepackage{graphicx} 

\usepackage{booktabs}
\usepackage{multirow}
\usepackage{siunitx}


%----------------------BEGIN-------------------------

\begin{document}


\begin{table}[H]
  \small
\centering
  \begin{tabular}{ll  SSSS   SS  SS} %S centres
  %\begin{tabular}{l|l ? S|S|S|S  | S|S | S|S}
    \toprule
     & &
      \multicolumn{4}{c}{Heading 1} & %& 
      \multicolumn{2}{c}{Heading 2} & 
      \multicolumn{2}{c}{Heading 3} \\
      \cmidrule(rl){3-6}
      \cmidrule(rl){7-8}
      \cmidrule(rl){9-10}
     Column 1& Column 2& {Sub 1} & {Sub 2} & {Sub 3} & {Sub 4} & {Sub 1} & {Sub 2} & {Sub 1} & {Sub 2} \\
          \midrule
    A1 = item1 & 0.0001  & 1 & 1 & 1 & 2 & 3 & 4 & 5 & 6 \\
    %\hline
    A2 = item2 & 0.0002 & 1 & 1 & 7 & 8 & 9 & 10 & 11 & 12 \\
    %\hline
    A3 = item3 & 0.0003  & 1 & 1 & 13 & 14 & 15 & 16 & 17 & 18 \\
    \bottomrule
  \end{tabular}
    \caption{Test Table} \label{tbl:BPF}
\end{table}

\end{document}

在此处输入图片描述

相关内容