在使用Latex table/tabular制作表格时,一个常见问题是表格过长,超出页面宽度,无法完整显示表格内容,如图所示:
这里提供两种解决思路以供需要的读者参考。
方法1:使用resizebox放缩命令
这里提供我经常使用的解决办法[1],即使用\resizebox命令放缩表格大小。附上代码如下:
\usepackage{
tabularx} % for 'tabularx' env. and 'X' col. type
\usepackage{
ragged2e} % for \RaggedRight macro
\usepackage{
booktabs} % for \toprule, \midrule etc macros
%% create a derivative column type called 'L':
\newcolumntype{
L}{
>{
\RaggedRight\hangafter=1\hangindent=0em}X}
\begin{
table}[!htb]\centering
\resizebox{
.8\columnwidth}{
!}{
\begin{
tabularx}{
\textwidth}{
p{
1cm} p{
2cm} p{
2cm} p{
1.5cm} p{
1.5cm} p{
1cm} }
\toprule
Set Size & parameter setup, on client side & parameter transfer, on client side & threshold PSI & standard PSI & total(threshold+standard PSI) \\
\midrule
10 & 6.466s & 5.511s & 1.455s & 0.028s & 1.483s \\
50 & 7.084s & 5.166s & 8.805s & 0.074s & 8.879s\\
100 & 22.673s & 16.252s & 31.621s & 0.145s & 31.766s\\
200 & 78.327s & 40.318s & 106.344s & 0.200s & 106.544s\\
\bottomrule
\end{
tabularx}
}
\caption{
Timing Performance of the FHE-based Threshold PSI for small sets, 80-bit security level and multiple-threads (maximum 10 threads used).}
\end{
table}
具体地,\resizebox{<width>}{<height>}
可以按照指定的宽(width)和高(height)限制表格的大小。代码例子中使用了命令\resizebox{.8\columnwidth}{!}
,表示设置宽度为0.8倍的页面宽度,高度跟随宽度适应性地变化(即符号!的含义)。 最终显示效果如图:
latex 将表格缩小
在LaTeX中,可以使用resizebox命令来缩放表格。这个命令来自graphicx宏包,因此你需要在文档的序言部分包含这个宏包:
\usepackage{
graphicx}
然后,你可以使用resizebox
命令来缩放表格。例如,如果你想将表格的宽度设置为文本宽度的一半,你可以这样做:
\begin{
table}[ht]
\centering
\resizebox{
\textwidth}{
!}{
\begin{
tabular}{
|l|c|r|}
\hline
左列 & 中列 & 右列 \\
\hline
数据1 & 数据2 & 数据3 \\
数据4 & 数据5 & 数据6 \\
\hline
\end{
tabular}
}
\caption{
缩小后的表格}
\end{
table}
在上面的例子中,\resizebox{\textwidth}{!}
将表格的宽度调整为文本宽度(\textwidth
),而高度会自动调整保持表格的纵横比。如果你想手动设置高度,可以替换!为具体的长度值。
请注意,频繁地缩放表格可能会导致文档中的文本也随之缩小,从而影响文档的整体可读性和美观。因此,应当根据实际需要谨慎缩放表格。
方法2:使用sidewaystable环境
对于大表格,可以使用rotation包中的sidewaystable环境对其进行竖排[2]。参考代码如下:
\usepackage{
tabularx} % for 'tabularx' env. and 'X' col. type
\usepackage{
ragged2e} % for \RaggedRight macro
\usepackage{
booktabs} % for \toprule, \midrule etc macros
\usepackage{
rotating} % display table vertically
%% create a derivative column type called 'L':
\newcolumntype{
L}{
>{
\RaggedRight\hangafter=1\hangindent=0em}X}
\begin{
sidewaystable} [!htb]\centering
\begin{
tabularx}{
\textwidth}{
c c c c c c }
\toprule
Set Size & parameter setup, on client side & parameter transfer, on client side & threshold PSI & standard PSI & total(threshold+standard PSI) \\
\midrule
10 & 6.466s & 5.511s & 1.455s & 0.028s & 1.483s \\
50 & 7.084s & 5.166s & 8.805s & 0.074s & 8.879s\\
100 & 22.673s & 16.252s & 31.621s & 0.145s & 31.766s\\
200 & 78.327s & 40.318s & 106.344s & 0.200s & 106.544s\\
\bottomrule
\end{
tabularx}
\caption{
Timing Performance of the FHE-based Threshold PSI for small sets, 80-bit security level and multiple-threads (maximum 10 threads used).}
\end{
sidewaystable}
显示效果如下图:
方法3:使用makecell包
makecell包可以对单元格内的文字进行强制换行[3],因此也可以缩短表格宽度。
引入makecell包后,直接使用命令\makecell{NTT-accelerated FHEW\ (NTT of 2 processors)} 可以将文字内容强制分成两行。参考代码如下:
\usepackage{
multirow,array}
\usepackage{
makecell}
\begin{
table*}[!t]\centering
\caption{
Performance of FHEW-like FHE schemes with and without NTT hardware module on Xilinx Artix-7 FPGA}
\label{
table:fhew-hardware-result}
\begin{
minipage}{
\textwidth}\centering
\scalebox{
0.75}{
\begin{
tabular}{
cccccccc}
\hline
Instance & & Computing Time & Freq & LUT/Reg & Memory & DSPs \\
\hline
\multirow{
3}{
*}{
Pure Software} & Overall & 1.3s & \multirow{
3}{
*}{
2.5 GHz} & n.a. & n.a. & n.a. \\
& Excluding NTT & 0.24s & & n.a. & n.a. & n.a. \\
& NTT (9484 times) & 1.06s & & n.a. & n.a. & n.a. \\
\hline
\multirow{
4}{
*}{
\makecell{
NTT-accelerated FHEW\\ (NTT of 2 processors)}} & Overall & --- & n.a. & n.a. & 3 & 30 \\
& FHEW Software & --- & 2.5 GHz & n.a. & n.a. & n.a. \\
& NTT commputation & --- & 250 MHz & {
2377/1879} & 3 & 30 \\
& NTT communication & --- & 2.5 GHz & 123090/135107 & 142 & 6 \\
\hline
\multirow{
4}{
*}{
\makecell{
NTT-accelerated FHEW\\ (NTT of 4 processors)}} & Overall & 1.2s & n.a. & n.a. & 3 & 30 \\
& FHEW Software & 0.25s & 2.5 GHz & n.a. & n.a. & n.a. \\
& NTT commputation & 0.14s & 250 MHz & {
3702/3191} & 6 & 60 \\
& NTT communication & 0.81s & 2.5 GHz & 123090/135107 & 142 & 6 \\
\hline
\end{
tabular}}
\end{
minipage}
\vspace{
0mm}
\end{
table*}
显示效果如下图:
参考
^https://tex.stackexchange.com/questions/10863/is-there-a-way-to-slightly-shrink-a-table-including-font-size-to-fit-within-th
^https://blog.csdn.net/yq_forever/article/details/130337964
^https://tex.stackexchange.com/questions/2441/how-to-add-a-forced-line-break-inside-a-table-cell