如何将标题分成多行以适应多列和多行?

如何将标题分成多行以适应多列和多行?

我正在使用下面的代码,

\begin{table}
\centering
\caption{Before\strut}
\begin{tabular}{|l|l|l|l|l|l|l|l|}
    \hline
      \multirow{2}{*}{Ser.} &
      \multirow{2}{*}{$V_{tune}$ (V)} &
      \multicolumn{2}{c|}{Frequency (GHz)} &
      \multicolumn{2}{c|}{Power Consumption (mW)} &
      \multicolumn{2}{c|}{Phase Noise at 1MHz (dBc/Hz)} \\
      \cline{3-8}
    && Schematic & Post-layout & Schematic & Post-layout & Schematic & Post-layout\\
    \hline
    a1 & D1 & 2.1\% & 2.1\% & 2.1\% & 2.1\% & 2.1\% & 2.1\% \\
    \hline
    a1 & D2 & 11.6\% & 11.6\% & 11.6\% & 11.6\% & 11.6\% & 11.6\% \\
    \hline
    a1 & D3 & 5.5\% & 5.5\% & 5.5\% & 5.5\% & 5.5\% & 5.5\% \\
    \hline
\end{tabular}
\end{table}

输出 在此处输入图片描述

内容超出了我的页面范围rgin。我想将标题分成多行。我该怎么做?

答案1

如果您加载geometry比默认设置更合适的边距,并使用makecell,它可以适合边距之间。使用默认布局,您可以减少值(默认为 6pt,您可以毫无问题地将其减少到 3pt)和字体大小(例如,\tabcolsep将表格设置为)。\small

\documentclass{article}
\usepackage[showframe]{geometry} 
\usepackage{multirow, makecell, caption}
\captionsetup{skip =4pt }
\usepackage{siunitx}
\DeclareSIUnit{\dBc}{dBc}

\begin{document}

\begin{table}
\centering
\setlength{\extrarowheight}{2pt}
\caption{Before}
\begin{tabular}{|l|l|*{6}{S|}}
    \hline
      \multirow{2.5}{*}{Ser.} &
      \multirowcell{2.5}{$V_\text{tune}$\\ (V)} &
      \multicolumn{2}{c|}{Frequency (GHz)} &
      \multicolumn{2}{c|}{\makecell{Power Consumption\\ (\si{\mW})}} &
      \multicolumn{2}{c|}{\makecell{Phase Noise at \SI{1}{\MHz} \\(\si{dBc\per\Hz})}} \\
      \cline{3-8}
    && {Schematic} & {Post-layout} & {Schematic} & {Post-layout} & {Schematic} & {Post-layout}\\
    \hline
    a1 & D1 & 2.1\% & 2.1\% & 2.1\% & 2.1\% & 2.1\% & 2.1\% \\
    \hline
    a1 & D2 & 11.6\% & 11.6\% & 11.6\% & 11.6\% & 11.6\% & 11.6\% \\
    \hline
    a1 & D3 & 5.5\% & 5.5\% & 5.5\% & 5.5\% & 5.5\% & 5.5\% \\
    \hline
\end{tabular}
\end{table}

\end{document} 

在此处输入图片描述

答案2

这是您表格的两个版本。在这两个版本中,我都使用了包\thead中的命令makecell,以便在表格标题中引入换行符。此外,我还使用了siunitx标题中的单位以及改善表格中数字的对齐方式。我还提供了一个没有垂直线和更少水平线的版本,以使您的表格看起来更开放。请注意,这两个表格仍然比常规类的文本宽度更宽article。由于您的示例中没有给出文档类,所以我没有在这里解决这个问题。

在此处输入图片描述

\documentclass{article}
\usepackage{makecell}
\renewcommand{\theadfont}{\normalsize}
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage{multirow}
\begin{document}

\begin{table}
\centering
\caption{Before with thead from the makecell package and siunitx}
\begin{tabular}{|c|c|*{6}{S[table-format=2.1, table-space-text-post=\%]|}}
    \hline
      \thead{Ser.} &
      \thead{$V_{tune}$\\ (\si{\volt})} &
      \multicolumn{2}{c|}{\thead{Frequency\\ (\si{\giga\hertz})}} &
      \multicolumn{2}{c|}{\thead{Power Consumption\\ (\si{\milli\watt})}} &
      \multicolumn{2}{c|}{\thead{Phase Noise at\\ \SI{1}{\mega\hertz} (\si{dBc\per\hertz})}} \\
      \cline{3-8}
    && {Schematic} & {Post-layout} & {Schematic} & {Post-layout} & {Schematic} & {Post-layout}\\
    \hline
    a1 & D1 & 2.1\% & 2.1\% & 2.1\% & 2.1\% & 2.1\% & 2.1\% \\
    \hline
    a1 & D2 & 11.6\% & 11.6\% & 11.6\% & 11.6\% & 11.6\% & 11.6\% \\
    \hline
    a1 & D3 & 5.5\% & 5.5\% & 5.5\% & 5.5\% & 5.5\% & 5.5\% \\
    \hline
\end{tabular}
\end{table}

\begin{table}
\centering
\caption{Before with thead from the makecell package, siunitx and booktabs}
\begin{tabular}{cc*{6}{S[table-format=2.1, table-space-text-post=\%]}}
    \toprule
      \thead{Ser.} &
      \thead{$V_{tune}$\\ (\si{\volt})} &
      \multicolumn{2}{c}{\thead{Frequency\\ (\si{\giga\hertz})}} &
      \multicolumn{2}{c}{\thead{Power Consumption\\ (\si{\milli\watt})}} &
      \multicolumn{2}{c}{\thead{Phase Noise at\\ \SI{1}{\mega\hertz} (\si{dBc\per\hertz})}} \\
      \cmidrule(r){3-4} \cmidrule(lr){5-6} \cmidrule(l){7-8}
    && {Schematic} & {Post-layout} & {Schematic} & {Post-layout} & {Schematic} & {Post-layout}\\
    \midrule
    a1 & D1 & 2.1\% & 2.1\% & 2.1\% & 2.1\% & 2.1\% & 2.1\% \\
    a1 & D2 & 11.6\% & 11.6\% & 11.6\% & 11.6\% & 11.6\% & 11.6\% \\
    a1 & D3 & 5.5\% & 5.5\% & 5.5\% & 5.5\% & 5.5\% & 5.5\% \\
    \bottomrule
\end{tabular}
\end{table}

\end{document}

article如果您使用,表格可以轻松适应文本宽度

\renewcommand{\theadfont}{\small}
\setlength{\tabcolsep}{0pt}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}cc*{6}{S[table-format=2.1, table-space-text-post=\%]}}

反而。

答案3

以下是我的表格版本先前的答案它已适应您的新表格。它使用tabularx环境来允许在标题单元格中换行。它还会对数字的小数位进行对齐。

在此处输入图片描述

\documentclass{article}
\usepackage{geometry} % set page parameters suitably
\usepackage{amsmath,booktabs,tabularx,ragged2e,dcolumn}'
% To align numbers on decimal markers:
\newcolumntype{d}[1]{D..{#1}}  
% Centered version of 'X' column type:
\newcolumntype{C}{>{\Centering\hspace{0pt}}X}
% Allow line-breaking across two-wide cells
\newcolumntype{E}{>{\hsize=\dimexpr2\hsize+2\tabcolsep\relax}C}
% Handy shortcut macro:
\newcommand\mC[1]{\multicolumn{1}{C}{#1}} 
\newcommand\mytab[1]{\smash{\begin{tabular}[t]{@{}c@{}} #1 \end{tabular}}}

\begin{document}
\begin{table}
\setlength\tabcolsep{3pt} % default: 6pt
\centering
\caption{Table with automatic line breaks in header cells\strut}
\begin{tabularx}{\textwidth}{@{} lc *{6}{d{2.2}} @{}}
\toprule
Ser. &
\mytab{$V_{\mathrm{tune}}$ \\ (V)} &
\multicolumn{2}{E}{Frequency (GHz)} &
\multicolumn{2}{E}{Power Consumption (mW)} &
\multicolumn{2}{E@{}}{Phase Noise at 1\,MHz (dBc/Hz)} \\
\cmidrule(lr){3-4} \cmidrule(lr){5-6} \cmidrule(l){7-8} 
&& \mC{Schematic} & \mC{Post-layout} 
 & \mC{Schematic} & \mC{Post-layout} 
 & \mC{Schematic} & \mC{Post-layout} \\
\midrule
 a1 & D1 & 2.1\% & 2.1\% & 2.1\% & 2.1\% & 2.1\% & 2.1\% \\
 a1 & D2 & 11.6\% & 11.6\% & 11.6\% & 11.6\% & 11.6\% & 11.6\% \\
 a1 & D3 & 5.5\% & 5.5\% & 5.5\% & 5.5\% & 5.5\% & 5.5\% \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}

相关内容