如何强制表格适合整个页面?

如何强制表格适合整个页面?

我一直在看一些帖子……即如何强制表格适合页面宽度,但仍然无法让我的表格在页面宽度上正确显示。

您能帮我让两页上的表格虽然超出了页边距但仍然显示在页面中央吗?

这是我目前拥有的代码:

\documentclass[10pt,openany]{book}

\usepackage{calc}
\usepackage[left=1.5cm, right=1.5cm, bindingoffset=1.5cm, headheight=30pt, top={120pt+10mm}]{geometry}
\usepackage{graphicx, rotating, ltablex, dcolumn, booktabs}
\usepackage[english]{babel}
\usepackage[usenames,dvipsnames,svgnames,table,xcdraw]{xcolor}
\usepackage{array}
\usepackage{colortbl}
\usepackage{tabularx}
\usepackage{multicol}
\definecolor{mybluei}{RGB}{0,173,239}
\definecolor{myblueii}{RGB}{63,200,244}
\definecolor{myblueiii}{RGB}{199,234,253}

\begin{document}

\noindent

\begin{table}[htbt]
\centering
\caption{My caption}
\label{my-label}
\rowcolors{2}{myblueiii}{white}
\begin{tabular}{cccccccccc}
\rowcolor{myblueii!50}
\hline
\textbf{Model} & \textbf{NUMUORS} & \textbf{NUMUANDS} & \textbf{TOTOTORS} & \textbf{TOTOPANDS} & \textbf{VG} & \textbf{NLOGIC} & \textbf{LOC} & \textbf{ELOC} & \textbf{} \\ \hline
1 & -0.0516 & 0.0314 & -0.0027 & \cellcolor[HTML]{9B9B9B} & -0.0372 & 0.2119 & 0.0018 & 0.005 & -0.3091 \\
2 & -0.0482 & 0.0336 & -0.0021 & \cellcolor[HTML]{9B9B9B} & -0.0337 & 0.2088 & 0.0019 & \cellcolor[HTML]{9B9B9B} & -0.3255 \\
3 & -0.0517 & 0.0341 & -0.0026 & \cellcolor[HTML]{9B9B9B} & -0.0372 & 0.2118 & 0.0018 & 0.005 & -0.309 \\
4 &  &  &  & \cellcolor[HTML]{9B9B9B} &  &  &  &  &  \\
5 & -0.0516 & 0.0341 & -0.0027 & \cellcolor[HTML]{9B9B9B} & -0.0372 & 0.2119 & 0.0018 & 0.005 & -0.3091 \\
6 & -0.0482 & 0.0336 & -0.0021 & \cellcolor[HTML]{9B9B9B} & -0.0337 & 0.2088 & 0.0019 & \cellcolor[HTML]{9B9B9B} & -0.3255 \\
7 & -0.0517 & 0.0341 & -0.0026 & \cellcolor[HTML]{9B9B9B} & -0.0372 & 0.2118 & 0.0018 & -0.309 & -0.309 \\
8 &  &  &  & \cellcolor[HTML]{9B9B9B} &  &  &  &  &
\end{tabular}
\end{table}

\newpage
\begin{table}[htbt]
\centering
\caption{My caption}
\label{my-label}
\rowcolors{2}{myblueiii}{white}
\begin{tabular}{cccccccccc}
\rowcolor{myblueii!50}
\hline
\textbf{Model} & \textbf{NUMUORS} & \textbf{NUMUANDS} & \textbf{TOTOTORS} & \textbf{TOTOPANDS} & \textbf{VG} & \textbf{NLOGIC} & \textbf{LOC} & \textbf{ELOC} & \textbf{} \\ \hline
1 & -0.0516 & 0.0314 & -0.0027 & \cellcolor[HTML]{9B9B9B} & -0.0372 & 0.2119 & 0.0018 & 0.005 & -0.3091 \\
2 & -0.0482 & 0.0336 & -0.0021 & \cellcolor[HTML]{9B9B9B} & -0.0337 & 0.2088 & 0.0019 & \cellcolor[HTML]{9B9B9B} & -0.3255 \\
3 & -0.0517 & 0.0341 & -0.0026 & \cellcolor[HTML]{9B9B9B} & -0.0372 & 0.2118 & 0.0018 & 0.005 & -0.309 \\
4 &  &  &  & \cellcolor[HTML]{9B9B9B} &  &  &  &  &  \\
5 & -0.0516 & 0.0341 & -0.0027 & \cellcolor[HTML]{9B9B9B} & -0.0372 & 0.2119 & 0.0018 & 0.005 & -0.3091 \\
6 & -0.0482 & 0.0336 & -0.0021 & \cellcolor[HTML]{9B9B9B} & -0.0337 & 0.2088 & 0.0019 & \cellcolor[HTML]{9B9B9B} & -0.3255 \\
7 & -0.0517 & 0.0341 & -0.0026 & \cellcolor[HTML]{9B9B9B} & -0.0372 & 0.2118 & 0.0018 & -0.309 & -0.309 \\
8 &  &  &  & \cellcolor[HTML]{9B9B9B} &  &  &  &  &
\end{tabular}
\end{table}

\end{document} 

第一张表:

enter image description here

第二张表:

enter image description here

答案1

我认为最好的选择是以横向格式显示表格。由于您已经加载了包rotating,因此可以利用sidewaystable环境。

我还想建议您将第 2 列至第 10 列的数字与小数点对齐。这样表格的可读性会更强。

由于您使用交替行颜色,因此没有必要使用\hline来设置行边界,对吗?

enter image description here

\documentclass[10pt,openany]{book}
\usepackage{calc}
\usepackage[hmargin=1.5cm, bindingoffset=1.5cm, 
            headheight=30pt, top={120pt+10mm}]{geometry}
\usepackage{rotating,array,siunitx}
\usepackage[english]{babel}
\usepackage[usenames,dvipsnames,svgnames,table,xcdraw]{xcolor}
\definecolor{mybluei}{RGB}{0,173,239}
\definecolor{myblueii}{RGB}{63,200,244}
\definecolor{myblueiii}{RGB}{199,234,253}

\begin{document}

\begin{sidewaystable}
\setlength\tabcolsep{3pt}      % default: 6pt
\setlength\extrarowheight{1pt} % give the table a more open look
\centering
\caption{My caption}
\label{my-label}
\rowcolors{2}{myblueiii}{white}
\begin{tabular}{c *{5}{S[table-format=-1.4]}
                  *{2}{S[table-format= 1.4]}
                       S[table-format=-1.3]
                       S[table-format=-1.4]}
\rowcolor{myblueii!50}
\textbf{Model} & {\textbf{NUMUORS}} & {\textbf{NUMUANDS}} & {\textbf{TOTOTORS}} &
{\textbf{TOTOPANDS}} & {\textbf{VG}} & {\textbf{NLOGIC}} & {\textbf{LOC}} &
{\textbf{ELOC}} & {\textbf{}} \\
1 & -0.0516 & 0.0314 & -0.0027 & \cellcolor[HTML]{9B9B9B} & -0.0372 & 0.2119 & 0.0018 & 0.005 & -0.3091 \\
2 & -0.0482 & 0.0336 & -0.0021 & \cellcolor[HTML]{9B9B9B} & -0.0337 & 0.2088 & 0.0019 & \cellcolor[HTML]{9B9B9B} & -0.3255 \\
3 & -0.0517 & 0.0341 & -0.0026 & \cellcolor[HTML]{9B9B9B} & -0.0372 & 0.2118 & 0.0018 & 0.005 & -0.309 \\
4 &  &  &  & \cellcolor[HTML]{9B9B9B} & & & & & \\
5 & -0.0516 & 0.0341 & -0.0027 & \cellcolor[HTML]{9B9B9B} & -0.0372 & 0.2119 & 0.0018 & 0.005 & -0.3091 \\
6 & -0.0482 & 0.0336 & -0.0021 & \cellcolor[HTML]{9B9B9B} & -0.0337 & 0.2088 & 0.0019 & \cellcolor[HTML]{9B9B9B} & -0.3255 \\
7 & -0.0517 & 0.0341 & -0.0026 & \cellcolor[HTML]{9B9B9B} & -0.0372 & 0.2118 & 0.0018 & -0.309 & -0.309 \\
8 &  &  &  & \cellcolor[HTML]{9B9B9B} & & & & & \\
\end{tabular}

\end{sidewaystable}

\end{document} 

答案2

通过将列标题中的字体大小减小到\footnotesizetabcolsep(列之间的间距)到3pt,可以使表格适合文本宽度:

\documentclass[10pt,openany]{book}
\usepackage{calc}
\usepackage[showframe,
            left=1.5cm, right=1.5cm, 
            bindingoffset=1.5cm, headheight=30pt, 
            top={120pt+10mm}]{geometry}
\usepackage{graphicx, rotating, ltablex, dcolumn, booktabs}
\usepackage[english]{babel}
\usepackage[usenames,dvipsnames,svgnames,table,xcdraw]{xcolor}
\usepackage{array}
\usepackage{tabularx}
\usepackage{multicol}
\definecolor{mybluei}{RGB}{0,173,239}
\definecolor{myblueii}{RGB}{63,200,244}
\definecolor{myblueiii}{RGB}{199,234,253}

\usepackage{siunitx}                            % <-- added
\newcommand\mch[1]{\multicolumn{1}{c}{\footnotesize\textbf{#1}}} % <-- added

\begin{document}
    \begin{table}[htbt]
\centering
\caption{My caption}
    \label{my-label}
\setlength\tabcolsep{3pt}
\rowcolors{2}{myblueiii}{white}
    \begin{tabular}{c *{5}{S[table-format=-1.4]}
                      *{2}{S[table-format= 1.4]}
                           S[table-format=-1.3]
                           S[table-format=-1.4]}
\rowcolor{myblueii!50}
    \hline
\mch{Model} & \mch{NUMUORS} & \mch{NUMUANDS} & \mch{TOTOTORS} & \mch{TOTOPANDS} 
    & \mch{VG} & \mch{NLOGIC} & \mch{LOC} & \mch{ELOC} & \\ 
    \hline
1 & -0.0516 & 0.0314 & -0.0027 & \cellcolor[HTML]{9B9B9B} & -0.0372 & 0.2119 & 0.0018 & 0.005 & -0.3091 \\
2 & -0.0482 & 0.0336 & -0.0021 & \cellcolor[HTML]{9B9B9B} & -0.0337 & 0.2088 & 0.0019 & \cellcolor[HTML]{9B9B9B} & -0.3255 \\
3 & -0.0517 & 0.0341 & -0.0026 & \cellcolor[HTML]{9B9B9B} & -0.0372 & 0.2118 & 0.0018 & 0.005 & -0.309 \\
4 &  &  &  & \cellcolor[HTML]{9B9B9B} &  &  &  &  &  \\
5 & -0.0516 & 0.0341 & -0.0027 & \cellcolor[HTML]{9B9B9B} & -0.0372 & 0.2119 & 0.0018 & 0.005 & -0.3091 \\
6 & -0.0482 & 0.0336 & -0.0021 & \cellcolor[HTML]{9B9B9B} & -0.0337 & 0.2088 & 0.0019 & \cellcolor[HTML]{9B9B9B} & -0.3255 \\
7 & -0.0517 & 0.0341 & -0.0026 & \cellcolor[HTML]{9B9B9B} & -0.0372 & 0.2118 & 0.0018 & -0.309 & -0.309 \\
8 &  &  &  & \cellcolor[HTML]{9B9B9B} &  &  &  &  &  
    \end{tabular}
    \end{table}
\end{document}

enter image description here

相关内容