表格 - 格式化行,使分隔行的宽度和字体大小不间断

表格 - 格式化行,使分隔行的宽度和字体大小不间断

我最近开始学习 LaTeX,我有一个疑问。我正在为一份工作获取下图中的表格,我想让最后一行(平均值)更美观(例如没有间断)。这可能吗?

我的代码在图片下方。

注意:表格的最大宽度可以是 8.55 厘米,列宽可以变化。图例中可以有分隔符(如果没有分隔符会更好,但这不是必需的)。字体大小可以满足此要求“表格标题、第一个字母使用 10 号常规字体,其余表格标题和表格图例使用 8 号常规字体”。

谢谢!

在此处输入图片描述

\begin{table}[ht]
  \centering
      \footnotesize \onehalfspacing
      \caption{Table caption}
      \begin{tabular}{p{0.4cm}p{1.1cm}p{1.1cm}p{1.1cm}p{1.1cm}p{1.1cm}}
         \hline
         \normalfont N & Metric1 (\%) & Metric2 (\%) & Metric3 (Hz) & Metric4 & Metric5 \\
         \hline
            1 & 93.55 & 10.79 & 4.19 & -1.05 & 8.22 \\
            2 & 96.53 & 25.91 & 2.72 & 3.13 & 16.16 \\
            3 & 93.13 & 17.34 & 3.98 & 1.65 & 16.85 \\
            4 & 90.44 & 24.20 & 4.87 & -1.49 & 5.44 \\
            5 & 93.23 & 18.67 & 3.14 & 1.83 & 14.55 \\
            6 & 91.16 & 25.44 & 3.89 & 0.77 & 7.04 \\
            7 & 91.54 & 28.41 & 4.18 & 3.36 & 18.99 \\
            8 & 96.03 & 32.06 & 2.79 & 1.93 & 4.66 \\
            9 & 93.97 & 19.92 & 3.46 & 1.64 & 9.31 \\
            10 & 91.55 & 32.16 & 4.14 & 2.52 & 7.72 \\
            11 & 90.80 & 40.47 & 3.16 & 1.00 & 3.78 \\
            12 & 91.52 & 67.09 & 2.88 & 5.85 & 50.38 \\
            13 & 90.25 & 37.93 & 3.76 & 3.30 & 17.84 \\
         \hline
            Avg & $92.59\pm1.10$ & $29.26\pm7.66$ & $3.63\pm0.36$ & $1.88\pm1.04$ & $13.92\pm6.63$ \\
         \hline
      \end{tabular}
      \label{table2}
\end{table}

答案1

由于您已指出表格的宽度不得超过 8.55 厘米,因此我建议您从环境切换tabulartabular*环境并将其整体宽度设置为 8.55 厘米。

我还会使用该siunitx包及其S列类型将数据列中的数字与各自的小数点对齐,并使用该booktabs包的宏绘制间距合适的水平线。在标题材料上施加一些结构并提供一些视觉分组(例如在第 5 行和第 10 行后插入一些额外的空白)似乎也是值得探索的想法。

在此处输入图片描述

\documentclass{article} % or some other suitable document class
\usepackage{newtxtext,newtxmath}
\usepackage{booktabs} % for well-spaced horizontal lines
\usepackage{siunitx}  % for 'S' column type
\usepackage{array}    % for '\newcolumntype' macro
\newcolumntype{T}[1]{S[table-format=#1]}

\begin{document}
\begin{table}[ht]
\centering
\caption{Table caption\strut}\label{table2}
% make LaTeX figure out amount of intercolumn whitespace:
\setlength\tabcolsep{0pt} 
\begin{tabular*}{8.55cm}{@{\extracolsep{\fill}} % width: 8.55cm
    T{2.0} T{2.2} T{2.2} T{2.2} T{-1.2} T{2.2} }
\toprule
{N} & {Metric1} & {Metric2} & {Metric3} & {Metric4} & {Metric5} \\
    & {(\%)}    & {(\%)}    & {(Hz)} \\
\midrule
             1 & 93.55 & 10.79 & 4.19 & -1.05 &  8.22 \\
             2 & 96.53 & 25.91 & 2.72 &  3.13 & 16.16 \\
             3 & 93.13 & 17.34 & 3.98 &  1.65 & 16.85 \\
             4 & 90.44 & 24.20 & 4.87 & -1.49 &  5.44 \\
             5 & 93.23 & 18.67 & 3.14 &  1.83 & 14.55 \\
             \addlinespace
             6 & 91.16 & 25.44 & 3.89 &  0.77 &  7.04 \\
             7 & 91.54 & 28.41 & 4.18 &  3.36 & 18.99 \\
             8 & 96.03 & 32.06 & 2.79 &  1.93 &  4.66 \\
             9 & 93.97 & 19.92 & 3.46 &  1.64 &  9.31 \\
            10 & 91.55 & 32.16 & 4.14 &  2.52 &  7.72 \\
            \addlinespace
            11 & 90.80 & 40.47 & 3.16 &  1.00 &  3.78 \\
            12 & 91.52 & 67.09 & 2.88 &  5.85 & 50.38 \\
            13 & 90.25 & 37.93 & 3.76 &  3.30 & 17.84 \\
\midrule
{Avg} & 92.59 &   29.26 &    3.63 &    1.88 &   13.92 \\
    & \pm1.10 & \pm7.66 & \pm0.36 & \pm1.04 & \pm6.63 \\
\bottomrule
\end{tabular*}
\end{table}
\end{document}

答案2

在编写表格时,我会使用用于列(其中数字在小数点对齐)和表格水平规则的siunitx包和“makecell”(用于列标题)。Sbooktabs

编辑: 我忽略了表格宽度应小于或等于 8.55 厘米的要求。这要求表格的字体应该是\footnotesize,然后删除表格开头和结尾的列分隔符并使用tight-spacing列选项S

\documentclass{article}
\usepackage{booktabs, makecell}
\usepackage{siunitx}
\usepackage[skip=1ex]{caption}

\begin{document}
\begin{table}[ht]
    \centering
    \footnotesize
\caption{Table caption}
\label{table2}

\sisetup{separate-uncertainty,
         tight-spacing}      % <---   
    \begin{tabular}{@{} r *{2}{S[table-format=2.2(3)]}
                          *{3}{S[table-format=1.2(3)]}
                    @{} }
    \toprule
N   & {\makecell{Metric1\\ (\%)}} 
           & {\makecell{Metric2\\ (\%)}} 
                   & {\makecell{Metric3\\ (Hz)}}
                          & {\makecell{Metric4}}
                                  & {\makecell{Metric5}} \\
    \midrule
 1  & 93.55 & 10.79 & 4.19  & -1.05 &  8.22 \\
 2  & 96.53 & 25.91 & 2.72  &  3.13 & 16.16 \\
 3  & 93.13 & 17.34 & 3.98  &  1.65 & 16.85 \\
 4  & 90.44 & 24.20 & 4.87  & -1.49 &  5.44 \\
 5  & 93.23 & 18.67 & 3.14  &  1.83 & 14.55 \\
 6  & 91.16 & 25.44 & 3.89  &  0.77 &  7.04 \\
 7  & 91.54 & 28.41 & 4.18  &  3.36 & 18.99 \\
 8  & 96.03 & 32.06 & 2.79  &  1.93 &  4.66 \\
 9  & 93.97 & 19.92 & 3.46  &  1.64 &  9.31 \\
10  & 91.55 & 32.16 & 4.14  &  2.52 &  7.72 \\
11  & 90.80 & 40.47 & 3.16  &  1.00 &  3.78 \\
12  & 91.52 & 67.09 & 2.88  &  5.85 & 50.38 \\
13  & 90.25 & 37.93 & 3.76  &  3.30 & 17.84 \\
    \midrule
 Avg 
    & 92.59(110)
           & 29.26(766)
                   & 3.63(036) 
                          & 1.88(104)
                                  & 13.92(663)    \\
    \bottomrule
\end{tabular}
 \end{table}
\end{document}

在此处输入图片描述

表格宽度现在小于 8.55 厘米。

答案3

您只需更改所有p{1.1cm}内容p{1.5cm}并减少一些空间即可获得所需的内容(总宽度= 7.9 厘米):

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\begin{document}
    \begin{table}[ht]
        \centering
        \footnotesize %\onehalfspacing
        \caption{Table caption}
        \begin{tabular}{p{0.4cm}p{1.5cm}p{1.5cm}p{1.5cm}p{1.5cm}p{1.5cm}}
            \hline
            \normalfont N & Metric1 (\%) & Metric2 (\%) & Metric3 (Hz) & Metric4 & Metric5 \\
            \hline
            1 & 93.55 & 10.79 & 4.19 & -1.05 & 8.22 \\
            2 & 96.53 & 25.91 & 2.72 & 3.13 & 16.16 \\
            3 & 93.13 & 17.34 & 3.98 & 1.65 & 16.85 \\
            4 & 90.44 & 24.20 & 4.87 & -1.49 & 5.44 \\
            5 & 93.23 & 18.67 & 3.14 & 1.83 & 14.55 \\
            6 & 91.16 & 25.44 & 3.89 & 0.77 & 7.04 \\
            7 & 91.54 & 28.41 & 4.18 & 3.36 & 18.99 \\
            8 & 96.03 & 32.06 & 2.79 & 1.93 & 4.66 \\
            9 & 93.97 & 19.92 & 3.46 & 1.64 & 9.31 \\
            10 & 91.55 & 32.16 & 4.14 & 2.52 & 7.72 \\
            11 & 90.80 & 40.47 & 3.16 & 1.00 & 3.78 \\
            12 & 91.52 & 67.09 & 2.88 & 5.85 & 50.38 \\
            13 & 90.25 & 37.93 & 3.76 & 3.30 & 17.84 \\
            \hline
            Avg & $92.59\!\pm\!1.10\!$ & $29.26\!\pm\!7.66$ & $3.63\!\pm\!0.36$ & $1.88\!\pm\!1.04\!$ & $13.92\!\pm\!6.63$ \\
            \hline
        \end{tabular}
        \label{table2}
    \end{table}
\end{document}

输出: 在此处输入图片描述

相关内容