如何使表格适合页面的宽度?

如何使表格适合页面的宽度?

在此处输入图片描述我尝试了不同的解决方案来使下表适合页面的宽度,但没有成功。

非常感谢您的帮助!

%%%The Table

\documentclass[12pt,oneside]{book}
\usepackage{booktabs,tabularx}
\usepackage{caption}
\usepackage{booktabs,array}
\usepackage{multirow}
\begin{document}
\begin{table*}[]
\centering
\caption{XYZ}
\label{xyz}
\begin{tabularx}{\textwidth}{@{}ccccccccccccccccccccc@{}}
\hline \hline
\multirow{4}{*}{Distribution model} & \multicolumn{20}{c}{SNR (dB)} \\ \cmidrule(l){2-21} 
 & \multicolumn{4}{c}{-12} & \multicolumn{4}{c}{-6} & \multicolumn{4}{c}{0} & \multicolumn{4}{c}{4} & \multicolumn{4}{c}{8} \\ \cmidrule(lr){2-5} \cmidrule(lr){6-9} \cmidrule(lr){10-13} \cmidrule(lr){14-17} \cmidrule(lr){18-21}  
 & \multicolumn{2}{c}{Colored} & \multicolumn{2}{c}{White} & \multicolumn{2}{c}{Colored} & \multicolumn{2}{c}{White} & \multicolumn{2}{c}{Colored} & \multicolumn{2}{c}{White} & \multicolumn{2}{c}{Colored} & \multicolumn{2}{c}{White} & \multicolumn{2}{c}{Colored} & \multicolumn{2}{c}{White} \\ \cmidrule(lr){2-3}  \cmidrule(lr){4-5}  \cmidrule(lr){6-7} \cmidrule(lr){8-9}  \cmidrule(lr){10-11} \cmidrule(lr){12-13} \cmidrule(lr){14-15}  \cmidrule(lr){16-17}  \cmidrule(lr){18-19}  \cmidrule(lr){20-21} 
 & \multicolumn{1}{l}{$SNR_o$} & \multicolumn{1}{l}{MSE} & \multicolumn{1}{l}{$SNR_o$} & \multicolumn{1}{l}{MSE} & \multicolumn{1}{l}{$SNR_o$} & \multicolumn{1}{l}{MSE} & \multicolumn{1}{l}{$SNR_o$} & \multicolumn{1}{l}{MSE} & \multicolumn{1}{l}{$SNR_o$} & \multicolumn{1}{l}{MSE} & \multicolumn{1}{l}{$SNR_o$} & \multicolumn{1}{l}{MSE} & \multicolumn{1}{l}{$SNR_o$} & \multicolumn{1}{l}{MSE} & \multicolumn{1}{l}{$SNR_o$} & \multicolumn{1}{l}{MSE} & \multicolumn{1}{l}{$SNR_o$} & \multicolumn{1}{l}{MSE} & \multicolumn{1}{l}{$SNR_o$} & \multicolumn{1}{l}{MSE} \\ \hline
Laplacian & 2.66 & 1.51 & 5.26 & 0.60 & 6.27 & 0.386 & 10.17 & 0.15 & 11.48 & 0.096 & 15.70 & 0.038 & 15.26 & 0.038 & 19.55 & 0.015 & 19.22 & 0.015 & 23.49 & 0.0062 \\
Gaussian & 6.12 & 0.505 & 16.09 & 0.044 & 10.98 & 0.129 & 21.45 & 0.0127 & 16.44 & 0.032 & 26.44 & 0.0034 & 19.87 & 0.014 & \textbf{29.74} & \textbf{0.0016} & \textbf{23.57} & \textbf{0.006} & \textbf{32.92} & \textbf{0.0008} \\
Uniform & \textbf{8.62} & \textbf{0.29} & \textbf{17.11} & \textbf{0.032} & \textbf{12.55} & \textbf{0.084} & \textbf{22.15} & \textbf{0.0095} & \textbf{17.25} & \textbf{0.026} & \textbf{26.5} & \textbf{0.0033} & \textbf{20.28} & \textbf{0.013} & 29.37 & 0.0018 & 23.54 & 0.0072 & 32.54 & 0.001 \\  \hline \hline

\end{tabularx}
\end{table*}

\end{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%修改后的代码:

\documentclass[11pt,oneside]{book}
\usepackage{booktabs,tabularx}
\usepackage{caption}
\usepackage{booktabs,array}
\usepackage{graphicx,multirow}

\begin{document}
\begin{table}[htp]
\centering
\caption{XYZ}
\scriptsize\setlength{\tabcolsep}{2pt}
\hspace*{-4cm}
\begin{tabular}{l@{\hspace{4pt}} *{21}{c}}
\hline \hline
\multirow{4}{*}{\rotatebox[origin=c]{90}{\kern-2em Distribution}} & \multicolumn{20}{c}{SNR (dB)} \\ \cmidrule(l){2-21} 
 & \multicolumn{4}{c}{-12} & \multicolumn{4}{c}{-6} & \multicolumn{4}{c}{0} & \multicolumn{4}{c}{4} & \multicolumn{4}{c}{8} \\ \cmidrule(lr){2-5} \cmidrule(lr){6-9} \cmidrule(lr){10-13} \cmidrule(lr){14-17} \cmidrule(lr){18-21}  
 & \multicolumn{2}{c}{Colored} & \multicolumn{2}{c}{White} & \multicolumn{2}{c}{Colored} & \multicolumn{2}{c}{White} & \multicolumn{2}{c}{Colored} & \multicolumn{2}{c}{White} & \multicolumn{2}{c}{Colored} & \multicolumn{2}{c}{White} & \multicolumn{2}{c}{Colored} & \multicolumn{2}{c}{White} \\ \cmidrule(lr){2-3}  \cmidrule(lr){4-5}  \cmidrule(lr){6-7} \cmidrule(lr){8-9}  \cmidrule(lr){10-11} \cmidrule(lr){12-13} \cmidrule(lr){14-15}  \cmidrule(lr){16-17}  \cmidrule(lr){18-19}  \cmidrule(lr){20-21} 
 & \multicolumn{1}{l}{$\textrm{SNR}_o$} & \multicolumn{1}{l}{MSE} & \multicolumn{1}{l}{$\textrm{SNR}_o$} & \multicolumn{1}{l}{MSE} & \multicolumn{1}{l}{$\textrm{SNR}_o$} & \multicolumn{1}{l}{MSE} & \multicolumn{1}{l}{$\textrm{SNR}_o$} & \multicolumn{1}{l}{MSE} & \multicolumn{1}{l}{$\textrm{SNR}_o$} & \multicolumn{1}{l}{MSE} & \multicolumn{1}{l}{$\textrm{SNR}_o$} & \multicolumn{1}{l}{MSE} & \multicolumn{1}{l}{$SNR_o$} & \multicolumn{1}{l}{MSE} & \multicolumn{1}{l}{$SNR_o$} & \multicolumn{1}{l}{MSE} & \multicolumn{1}{l}{$SNR_o$} & \multicolumn{1}{l}{MSE} & \multicolumn{1}{l}{$\textrm{SNR}_o$} & \multicolumn{1}{l}{MSE} \\ \hline
Laplacian & 2.66 & 1.51 & 5.26 & 0.60 & 6.27 & 0.386 & 10.17 & 0.15 & 11.48 & 0.096 & 15.70 & 0.038 & 15.26 & 0.038 & 19.55 & 0.015 & 19.22 & 0.015 & 23.49 & 0.0062 \\
Gaussian & 6.12 & 0.505 & 16.09 & 0.044 & 10.98 & 0.129 & 21.45 & 0.0127 & 16.44 & 0.032 & 26.44 & 0.0034 & 19.87 & 0.014 & \textbf{29.74} & \textbf{0.0016} & \textbf{23.57} & \textbf{0.006} & \textbf{32.92} & \textbf{0.0008} \\
Uniform & \textbf{8.62} & \textbf{0.29} & \textbf{17.11} & \textbf{0.032} & \textbf{12.55} & \textbf{0.084} & \textbf{22.15} & \textbf{0.0095} & \textbf{17.25} & \textbf{0.026} & \textbf{26.5} & \textbf{0.0033} & \textbf{20.28} & \textbf{0.013} & 29.37 & 0.0018 & 23.54 & 0.0072 & 32.54 & 0.001 \\  \hline \hline

\end{tabular}
\hspace*{-3cm}
\end{table}

\end{document}

答案1

我的主要建议是在表格中使用非扩展粗体字体,这样粗体和正常粗细的数字都会垂直排列。我还建议您使用tabular*总宽度等于 的环境\textwidth

在此处输入图片描述

\documentclass[12pt,oneside]{book}
\usepackage[a4paper,margin=2.5cm]{geometry} % select suitable page parameters
\usepackage{booktabs,siunitx,etoolbox,amsmath}
\usepackage[skip=0.333\baselineskip]{caption}
\newcommand\smalltab[1]{\multicolumn{1}{@{}c@{}}{\smash[b]{%
   \begin{tabular}[t]{@{}l@{}}#1\end{tabular}}}}
\begin{document}
\begin{table}[!htb]
% Local redefinition of "\bfseries"
\renewrobustcmd{\bfseries}{\fontseries{b}\selectfont} 
\sisetup{detect-weight,mode=text}
\setlength{\tabcolsep}{0pt} % let LaTeX determine inter-col. whitespace
\caption{XYZ}
\label{tab:xyz}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}
l S[table-format=-2.0]
S[table-format=2.2]S[table-format=1.4]
S[table-format=2.2]S[table-format=1.4]
S[table-format=-1.0]
S[table-format=2.2]S[table-format=1.3]
S[table-format=2.2]S[table-format=1.4]}
\toprule
\smalltab{Distribution \\ model}& 
\smalltab{SNR\\(dB)} &
\multicolumn{2}{c}{Colored} & \multicolumn{2}{c}{White} &
\smalltab{SNR\\(dB)} & 
\multicolumn{2}{c}{Colored} & \multicolumn{2}{c}{White} \\
\cmidrule{3-4}\cmidrule{5-6}
\cmidrule{8-9}\cmidrule{10-11}
& & {$\mathrm{SNR}_0$} & {MSE} & {$\mathrm{SNR}_0$} & {MSE} 
& & {$\mathrm{SNR}_0$} & {MSE} & {$\mathrm{SNR}_0$} & {MSE}\\ 
\midrule
Laplacian & -12 &  2.66 & 1.51  &  5.26 & 0.60 
          &  -6 &  6.27 & 0.386 & 10.17 & 0.15\\
Gaussian  &     &  6.12 & 0.505 & 16.09 & 0.044 
          &     & 10.98 & 0.129 & 21.45 & 0.0127 \\
Uniform   &     & \bfseries 8.62 & \bfseries 0.29& \bfseries 17.11 & \bfseries 0.032 
           &     & \bfseries 12.55 & \bfseries 0.084 & \bfseries 22.15 & \bfseries 0.0095 \\
\addlinespace

Laplacian &   0 & 11.48 & 0.096 & 15.70 & 0.038 
          &   4 & 15.26 & 0.038 & 19.55 & 0.015\\
Gaussian  &     & 16.44 & 0.032 & 26.44 & 0.0034 
          &     & 19.87 & 0.014 & \bfseries 29.74 & \bfseries 0.0016 \\
Uniform   &     & \bfseries 17.25 & \bfseries 0.026 & \bfseries 26.5 & \bfseries 0.0033 
          &     & \bfseries 20.28 & \bfseries 0.013 & 29.37 & 0.0018 \\
\addlinespace

Laplacian &   8 & 19.22 & 0.015  & 23.49 & 0.0062 \\
Gaussian  &     & \bfseries 23.57 & \bfseries 0.006 & \bfseries 32.92 & \bfseries 0.0008 \\
Uniform   &     & 23.54 & 0.0072 & 32.54 & 0.001 \\
\bottomrule
\end{tabular*}
\end{table}

\end{document} 

答案2

我可以建议您重新组织表格并将其拆分为两行,如以下代码所示。此外,我使用列S类型将数字对齐到小数点上:

    \documentclass[12pt,oneside]{book}
    \usepackage[showframe]{geometry} %
    \usepackage{booktabs,tabularx}
    \usepackage[table]{xcolor}
    \usepackage{caption}
    \usepackage{booktabs,array}
    \usepackage{multirow, makecell, siunitx}
        \usepackage{xparse}
        \DeclareExpandableDocumentCommand\emptycells{O{c}m}{\multicolumn{#2}{#1}{}}
    \newcommand\emptycell{\emptycells{1}}

    \begin{document}
    \begin{table*}[!htb]
    \centering\small
    \setlength{\tabcolsep}{5pt}
    \sisetup{table-number-alignment=center, detect-weight}
    \caption{XYZ}
    \label{xyz}
    \begin{tabularx}{\textwidth}{@{}r |l@{}*{2}{S[table-format=2.2]S[table-format=1.4]}r|S[table-format=2.2]S[table-format=1.3]S[table-format=2.2]S[table-format=1.4]@{}}
    \toprule\midrule[0.4pt]
     \cmidrule(lr){3-6} \cmidrule(lr){7-11}
      \multicolumn{2}{c}{\multirowcell{2}{Distribution\\ model}}& \multicolumn{2}{c}{Colored} & \multicolumn{2}{c}{White} &\emptycell & \multicolumn{2}{c}{Colored} & \multicolumn{2}{c}{White} \\
     \emptycells{2} & \multicolumn{1}{l}{$SNR_o$} & \multicolumn{1}{l}{MSE} & \multicolumn{1}{l}{$SNR_o$} & \multicolumn{1}{l}{MSE} & \emptycell & \multicolumn{1}{l}{$SNR_o$} & \multicolumn{1}{l}{MSE}& \multicolumn{1}{l}{$SNR_o$} & \multicolumn{1}{l}{MSE}\\ \midrule
     \addlinespace
    \multicolumn{2}{@{}l}{SNR (dB)} & & & & & \multicolumn{2}{@{}l}{SNR (dB)} \\[0.5ex]
    & Laplacian & 2.66 & 1.51 & 5.26 & 0.60 & & 6.27 & 0.386 & 10.17 & 0.15\\
     $ -12 $ & Gaussian & 6.12 & 0.505 & 16.09 & 0.044 & $ -6 $ & 10.98 & 0.129 & 21.45 & 0.0127 \\
     & Uniform & \textbf{\phantom{0}8.62} & \bfseries 0.290& \textbf{17.11} & \textbf{0.032} & & \textbf{12.55} & \textbf{0.084} & \textbf{22.15} & \textbf{0.0095} \\ %
    \addlinespace[2ex]
[![enter image description here][1]][1]
    %%%%%%%%%%%
     & Laplacian & 11.48 & 0.096 & 15.70 & 0.038 & & 15.26 & 0.038 & 19.55 & 0.015\phantom{0}\\
     $ 0 $ & Gaussian & 16.44 & 0.032 & 26.44 & 0.0034 & $ 4 $ & 19.87 & 0.014 & \textbf{29.74} & \textbf{0.0016} \\
     & Uniform & \textbf{17.25} & \textbf{0.026} & \textbf{26.5\phantom{0}} & \textbf{0.0033} & & \textbf{20.28} & \textbf{0.013} & 29.37 & 0.0018 \\
    \addlinespace[2ex]
    %%%%%%%%%%%%%%%
       & Laplacian & 19.22 & 0.015 & 23.49 & 0.0062 \\
      $ 8 $ & Gaussian & \textbf{23.57} & \textbf{0.006} & \textbf{32.92} & \textbf{0.0008} \\
     & Uniform& 23.54 & 0.0072 & 32.54 & 0.001 \\
     \addlinespace
     \midrule[0.4pt]\bottomrule
    \end{tabularx}
    \end{table*}

    \end{document} 

在此处输入图片描述

相关内容