表格未扩展到整个页面宽度

表格未扩展到整个页面宽度

表格需要占据整个页面宽度(均匀拉伸)(由于使用自动表格生成器,代码看起来有点糟糕,任何清理/美化都会有所帮助)。MWE 如下--

\documentclass[listof=totoc,a4paper,11pt,oneside,chapterprefix=true,sfdefaults=false]{scrbook}
\usepackage{caption}
\usepackage{booktabs}

\begin{document}
% For tables use
\begin{table}[ht]
\centering
\caption[Supported Sensors in Smartphone]{Some supported sensors in smartphone\\\emph{Em:Embedded, Ex:External, PC:Proprioceptive, EC:Exteroceptive, A:Active, P:Passive}}
\label{comptable}
\begin{tabular}{l|l|l|l|l}
\toprule
\textit{\textbf{Category}} & \textit{\textbf{Name}}                                                              & \textit{\textbf{Em/Ex}}                                  & \textit{\textbf{PC/EC}}                            & \textit{\textbf{A/P}}                            \\
\midrule
Tactile                    & Proximity                                                                           & Em/Ex                                                    & EC                                                 & A/P                                              \\
\hline
Acceleration               & \begin{tabular}[c]{@{}l@{}}Gyroscope\\Accelerometer\end{tabular}                    & \begin{tabular}[c]{@{}l@{}}Em\\Ex\end{tabular}           & \begin{tabular}[c]{@{}l@{}}PC\\PC\end{tabular}     & \begin{tabular}[c]{@{}l@{}}P\\P\end{tabular}     \\
\hline
Thermal                    & Temperature                                                                         & Ex                                                       & EC                                                 & P/A                                              \\
\hline
~Image                     & \begin{tabular}[c]{@{}l@{}}CMOS camera\\Camcorder\end{tabular}                      & \begin{tabular}[c]{@{}l@{}}Em\\Ex\end{tabular}           & \begin{tabular}[c]{@{}l@{}}EC\\EC\end{tabular}     & \begin{tabular}[c]{@{}l@{}}P/A\\A\end{tabular}   \\
\hline
Light                      & \begin{tabular}[c]{@{}l@{}}Ambient light\\Back illuminated\end{tabular}             & \begin{tabular}[c]{@{}l@{}}Em/Ex\\Ex\end{tabular}        & \begin{tabular}[c]{@{}l@{}}EC\\EC\end{tabular}     & \begin{tabular}[c]{@{}l@{}}A\\P\end{tabular}     \\
\hline
Hydro                      & \begin{tabular}[c]{@{}l@{}}Moisture\\Humidity\end{tabular}                          & \begin{tabular}[c]{@{}l@{}}Em\\Ex\end{tabular}           & \begin{tabular}[c]{@{}l@{}}EC\\EC\end{tabular}     & \begin{tabular}[c]{@{}l@{}}P\\P\end{tabular}     \\
\hline
Location                   & \begin{tabular}[c]{@{}l@{}}Digital compass\\GPS\end{tabular}                        & \begin{tabular}[c]{@{}l@{}}Em\\Em\end{tabular}           & \begin{tabular}[c]{@{}l@{}}EC\\EC\end{tabular}     & \begin{tabular}[c]{@{}l@{}}A\\P\end{tabular}     \\
\hline
Height                     & Altimeter                                                                           &------                                                     & EC                                                 & P                                                \\
\hline
Medical                    & \begin{tabular}[c]{@{}l@{}}Barometer \\Heart rate monitor\\Bio-sensors\end{tabular} & \begin{tabular}[c]{@{}l@{}}Em\\------\\------\end{tabular} & \begin{tabular}[c]{@{}l@{}}EC\\EC\\EC\end{tabular} & \begin{tabular}[c]{@{}l@{}}P\\P\\P\end{tabular}  \\
\hline
Acoustic                   & Microphone                                                                          & Em                                                       & EC                                                 & P                                                \\
\hline
Radio                      & \begin{tabular}[c]{@{}l@{}}RFID\\Bluetooth\end{tabular}                             & \begin{tabular}[c]{@{}l@{}}------\\Em\end{tabular}         & \begin{tabular}[c]{@{}l@{}}EC\\EC\end{tabular}     & \begin{tabular}[c]{@{}l@{}}A\\A\end{tabular}     \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

答案1

为了使表格占据文本块的整个宽度,我建议您切换到环境tabular*。为了让表格看起来更有吸引力,我会删除所有垂直规则,并将所有实例替换\hline\addlinespace哦,我会去掉完全不必要的加粗和斜体标题行。

在此处输入图片描述

\documentclass[listof=totoc,a4paper,11pt,oneside,
               chapterprefix=true,sfdefaults=false
              ]{scrbook}
\usepackage{booktabs}
% handy shortcut macro:
\newcommand\mytab[1]{% 
   \begin{tabular}[t]{@{}l@{}} #1 \end{tabular}}

\begin{document}

\begin{table}[ht]

\setlength{\tabcolsep}{0pt} % make LaTeX figure out intercolumn padding

\caption[Supported Sensors in Smartphones]{%
         Some supported sensors in smartphones\\ 
         Em:~Embedded, Ex:~External; PC:~Proprioceptive, EC:~Exteroceptive;\\
         A:~Active, P:~Passive.}
\label{ comptable}

\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} lllll }

\toprule
Category & Name & Em/Ex & PC/EC & A/P \\
\midrule

Tactile & Proximity & Em/Ex & EC & A/P \\
\addlinespace
Acceleration & \mytab{Gyroscope\\Accelerometer} & \mytab{Em\\Ex} & \mytab{PC\\PC} & \mytab{P\\P} \\
\addlinespace
Thermal & Temperature & Ex & EC & P/A \\
\addlinespace
Image  & \mytab{CMOS camera\\Camcorder} & \mytab{Em\\Ex} & \mytab{EC\\EC} & \mytab{P/A\\A} \\
\addlinespace
Light & \mytab{Ambient light\\Back illuminated} & \mytab{Em/Ex\\Ex} & \mytab{EC\\EC} & \mytab{A\\P} \\
\addlinespace
Hydro & \mytab{Moisture\\Humidity} & \mytab{Em\\Ex} & \mytab{EC\\EC} & \mytab{P\\P} \\
\addlinespace
Location & \mytab{Digital compass\\GPS} & \mytab{Em\\Em} & \mytab{EC\\EC} & \mytab{A\\P} \\
\addlinespace
Height  & Altimeter &-- & EC & P \\
\addlinespace
Medical & \mytab{Barometer \\Heart rate monitor \\Bio-sensors} & \mytab{Em\\--\\--} & \mytab{EC\\EC\\EC} & \mytab{P\\P\\P}  \\
\addlinespace
Acoustic & Microphone & Em & EC & P \\
\addlinespace
Radio & \mytab{RFID\\Bluetooth} & \mytab{--\\Em} & \mytab{EC\\EC} & \mytab{A\\A} \\
\bottomrule
\end{tabular*}

\end{table}

\end{document}

答案2

另一种方法是使用tabularray表格。使用它的代码更简洁一些:

\documentclass[listof=totoc,
               a4paper,11pt,oneside,
               chapterprefix=true,
               sfdefaults=false
              ]{scrbook}
\usepackage{caption}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}
    \begin{table}[ht]
\caption[Supported Sensors in Smartphone]
        {Some supported sensors in smartphone\\
         \emph{Em:Embedded, Ex:External, PC:Proprioceptive, EC:Exteroceptive, A:Active, P:Passive}
         }
\label{comptable}
\begin{tblr}{colspec = {@{} l X[2, l] *{3}{X[l] } @{}},
             row{1}  = {font=\bfseries\itshape},
             colsep=8pt, 
             rowsep  = 1pt,
             row{even[4]} = {abovesep=7pt}
             }
    \toprule
Category    
    &   Name  
        &   Em/Ex 
            &   PC/EC
                &   A/P \\
    \midrule
Tactile
    &   Proximity 
        &   Em/Ex 
            &   EC
                &   A/P         \\
Acceleration               
    &   {Gyroscope\\    Accelerometer}
        &   {Em\\   Ex}
            &   {PC\\   PC}
                &   {P\\P}      \\
Thermal                    
    &   Temperature
        &   Ex
            &   EC
                &   P/A         \\
Image
    &   {CMOS camera\\  Camcorder}
        &   {Em\\   Ex}           
            &   {EC\\   EC}     
                &   {P/A\\  A}  \\
Light                      
    &   {Ambient light\\    Back illuminated}             
        &   {Em/Ex\\    Ex}        
            &   {EC\\   EC}     
                &   {A\\    P}  \\
Hydro                      
    &   {Moisture\\ Humidity}                          
        &   {Em\\Ex}           
            &   {EC\\   EC}     
                &   {P\\    P}  \\
Location                   
    &   {Digital compass\\  GPS}
        &   {Em\\   Em}           
            &   {EC\\   EC}     
                &   {A\\    P}  \\
Height                     
    &   Altimeter
        &   --
            &   EC 
                &   P           \\
Medical                    
    &   {Barometer\\    Heart rate monitor\\    Bio-sensors} 
        &   {Em\\   -- \\--} 
            &   {EC\\   EC\\    EC} 
                &   {P\\    P\\ P}  \\
Acoustic                   
    &   Microphone
        &   Em 
            &   EC
                &   P               \\
Radio                      
    &   {RFID\\ Bluetooth}                             
        &   {--\\   Em}         
            &   {EC\\   EC}     
                &   {A\\    A}     \\
    \bottomrule
\end{tblr}
    \end{table}
\end{document}

在此处输入图片描述

相关内容