如何在科学表格中将标题和数字居中以及将数字之间的连字符

如何在科学表格中将标题和数字居中以及将数字之间的连字符

我刚开始使用 LaTex,它是一款很棒的工具。鉴于我对代码开发的了解有限,有人能为我的问题提供解决方案吗?我需要在年龄间隔之间添加连字符。年龄和 VO2 应位于男性和女性下方的中心。此外,我需要所有数字都位于年龄和 VO2 下方的中心,并且最后一列中的数字不要合并。有 LaTex 编码知识的人能告诉我如何做到这一点吗?代码在文本下方显示。提前致谢

问候 Svein

\documentclass[a4paper,10pt]{article} 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{babel}
\usepackage{mathtools,amssymb}
\usepackage{booktabs, makecell, 
            threeparttable}       % <---
\setlength\heavyrulewidth{2pt}    % <---
\setlength\lightrulewidth{1.2pt}  % <---

\setcellgapes{3pt}
\renewcommand\theadfont{\normalsize\bfseries}
\renewcommand\theadgape{}

\usepackage{isotope}
\usepackage[table]{xcolor}
\arrayrulecolor{teal}
\usepackage[skip=0.5ex]{caption}
    \DeclareCaptionLabelFormat{uc}{\MakeUppercase{#1}~#2}                % <===
    \DeclareCaptionFont{teal}{\color{teal}\fontfamily{iwona}\selectfont} % <===
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
\usepackage{hyphenat}
\usepackage{siunitx,booktabs}
\usepackage{dcolumn}
\newcommand{\splitcell}[1]{%
  \begin{tabular}[t]{@{}c@{}}\strut#1\strut\end{tabular}}

\begin{document}

\begin{table}[htp]
\centering
\renewcommand\thetable{2}
\caption{Cadirorespiratory fitness stratified by age}

\medskip
\begin{tabular}{
    c                   % Column 1
    S[table-format=1.3] % Column 2
    S[table-format=2.3] % Column 3
    S[table-format=1.6] % Column 4
    S[table-format=1.5] % Column 5
}

\toprule
& \multicolumn{2}{c}{Male} 
& \multicolumn{2}{c}{Female} \\
\cmidrule(lr){2-3} \cmidrule(l){4-5}
 &  
{Age}  & {\splitcell{VO2{\footnotesize \(^b\)(\(mL/kg/min\)})\\}} &   
{Age} & {\splitcell{VO2{\footnotesize \(^b\)(\(mL/kg/min\)})}}\\
\midrule
& 20 29 & 46.1 & 20 29 & 38.2\\

& \numrange{30}{39} & 38.5 & \numrange{30}{39} & 43\\

& \numrange{40}{49}  & 26.7 & \numrange{40}{49} & 21.6\\

& \numrange{50}{59}  & 29.1 & \numrange{50}{59}  & 30.6\\

& \geq 60 & 28.3 & \geq 60 & 27.3\\

\bottomrule

\end{tabular}
\end{table}
\end{document}

答案1

另一种解决方案,经过了一些修改。特别是,我使用了\makecell来自同名包的命令,该命令旨在允许在标准单元格中换行。至于数值范围的结束符,它是一个siunitx由键设置的选项range-phrase=

\documentclass[a4paper,10pt]{article}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{babel}
\usepackage{mathtools,amssymb}
\usepackage{booktabs, makecell, threeparttable} % <---
\setlength\heavyrulewidth{2pt} % <---
\setlength\lightrulewidth{1.2pt} % <---

\setcellgapes{3pt}
\renewcommand\theadfont{\normalsize\bfseries}
\renewcommand\theadgape{}

\usepackage{isotope}
\usepackage[table]{xcolor}
\arrayrulecolor{teal}
\usepackage[skip=0.5ex]{caption}
    \DeclareCaptionLabelFormat{uc}{\MakeUppercase{#1}~#2} % <===
    \DeclareCaptionFont{teal}{\color{teal}\fontfamily{iwona}\selectfont} % <===
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{hyphenat}
\usepackage{siunitx,booktabs, makecell}
\usepackage{dcolumn}

\begin{document}

\begin{table}[htp]
\sisetup{range-phrase= --}
\centering
\renewcommand\thetable{2}
\caption{Cardiorespiratory fitness stratified by age}

\medskip
\begin{tabular}{
    c % Column 1
    S[table-format=1.3] % Column 2
    S[table-format=2.3] % Column 3
    S[table-format=1.6] % Column 4
    S[table-format=2.1] % Column 5
}

\toprule
& \multicolumn{2}{c}{Men}
& \multicolumn{2}{c}{Female} \\
\cmidrule(lr){2-3} \cmidrule(l){4-5}
 &
{Age} & {\makecell{VO2\(^b\)\\ (\unit{mL/kg/min})\\}}
 &
{Age} & {\makecell{VO2\(^b\)\\(\unit{mL/kg/min})}}\\
\midrule
& \numrange{20}{29} & 46.1 & \numrange{20}{29} & 38.2\\

& \numrange{30}{39} & 51.5 & \numrange{30}{39} & 33\\

& \numrange{40}{49} & 35.7 & \numrange{40}{49} & 31.6\\

& \numrange{50}{59} & 39.1 & \numrange{50}{59} & 29.6\\

& {60--\phantom{90}} & 30.3 & {60--\phantom{90}} & 27.3\\

\bottomrule
\end{tabular}
\end{table}

\end{document} 

在此处输入图片描述

编辑:修改代码,使“男性”和“女性”更好地相对于“年龄”和“VO2”居中:

\begin{table}[htp]
\sisetup{range-phrase= --}
\centering
\renewcommand\thetable{2}
\caption{Cardiorespiratory fitness stratified by age}

\medskip
\begin{tabular}{
    c % Column 1
    S[table-format=1.3] % Column 2
    S[table-format=2.3] % Column 3
    S[table-format=1.6] % Column 4
    S[table-format=2.1] % Column 5
}

\toprule
& \multicolumn{2}{c}{Men\hskip1.5em\mbox{}}
& \multicolumn{2}{c}{Female\hskip1.5em\mbox{}} \\
\cmidrule(lr){2-3} \cmidrule(l){4-5}
 &
{Age} & {\makecell{VO2\(^b\)\\ \footnotesize(\unit{mL/kg/min})\\}}
 &
{Age} & {\makecell{VO2\(^b\)\\ \footnotesize(\unit{mL/kg/min})}}\\
\midrule
& \numrange{20}{29} & 46.1 & \numrange{20}{29} & 38.2\\

& \numrange{30}{39} & 51.5 & \numrange{30}{39} & 33\\

& \numrange{40}{49} & 35.7 & \numrange{40}{49} & 31.6\\

& \numrange{50}{59} & 39.1 & \numrange{50}{59} & 29.6\\

& {60--\phantom{90}} & 30.3 & {60--\phantom{90}} & 27.3\\

\bottomrule
\end{tabular}
\end{table}

在此处输入图片描述

答案2

  • 第一列是空的,删除它
  • 答案是考虑siunitx版本 3
  • 选择具有范围编号的列作为c类型
\documentclass[a4paper,10pt]{article}

%\usepackage{babel}                % which one?
\usepackage{mathtools,amssymb}
\usepackage{booktabs, makecell,
            threeparttable}       % <---
%\setlength\heavyrulewidth{2pt}    % <--- delete % if you like to have thicker rule
%\setlength\lightrulewidth{1.2pt}  % <--- delete % if you like to have thicker 
\renewcommand\theadfont{\normalsize\bfseries}
\renewcommand\theadgape{}
\NewExpandableDocumentCommand\mcc{O{1}m}{\multicolumn{#1}{c}{#2}}

\usepackage{isotope}
\usepackage[table]{xcolor}
\arrayrulecolor{teal}
\usepackage[skip=0.5ex]{caption}
    \DeclareCaptionLabelFormat{uc}{\MakeUppercase{#1}~#2}                % <===
    \DeclareCaptionFont{teal}{\color{teal}\fontfamily{iwona}\selectfont} % <===
\usepackage{siunitx} % considered v3
\usepackage{booktabs, makecell}
\usepackage{hyphenat}

\begin{document}
    \begin{table}[htp]
\centering
\sisetup{table-format=2.1,
         range-phrase= {\,--\,}
         }
    \begin{threeparttable}
\caption{Cadirorespiratory fitness stratified by age}
\begin{tabular}{
    c                   % Column 1
    S[table-format=2.1] % Column 2
    c                   % Column 3
    S[table-format=2.1] % Column 4
                }
    \toprule
\mcc[2]{Male}
            &   \mcc[2]{Female}                     \\
    \cmidrule(r){1-2} \cmidrule(l){3-4}
Age     &   {\makecell{VO2\tnote{b}\\  
                      (\unit{mL/kg/min})}}
            &   Age   
                &   {\makecell{VO2\tnote{b}\\
                              (\unit{mL/kg/min})}}  \\
    \midrule
\numrange{20}{29}    
        & 46.1  & \numrange{20}{29} & 38.2          \\
\numrange{30}{39} 
        & 51.5  & \numrange{30}{39} & 33            \\
\numrange{40}{49}  
        & 35.7  & \numrange{40}{49} & 31.6          \\
\numrange{50}{59}  
        & 39.1  & \numrange{50}{59} & 29.6          \\
$\geq$ 60   & 30.3  & $\geq 60$     & 27.3          \\
    \bottomrule
\end{tabular}
    \begin{tablenotes}[flushleft]\footnotesize
\item[b]    some description
    \end{tablenotes}
\end{threeparttable}
\end{table}
\end{document}

在此处输入图片描述

答案3

该表本身犯了一些错误:从不重复数据!

我在这里提出了两种认识,对第一种认识做了一些调整,与你的认识类似。

\documentclass[a4paper,10pt]{article} 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{booktabs}
\usepackage{threeparttable}
\usepackage[table]{xcolor}
\usepackage{siunitx}
\usepackage[skip=0.5ex]{caption}

\setlength\heavyrulewidth{2pt}    % <---
\setlength\lightrulewidth{1.2pt}  % <---

\arrayrulecolor{teal}
\DeclareCaptionLabelFormat{uc}{\MakeUppercase{#1}~#2}                % <===
\DeclareCaptionFont{teal}{\color{teal}\fontfamily{iwona}\selectfont} % <===
\captionsetup[table]{position=top}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
\newcommand{\splitcell}[1]{%
  \begin{tabular}[t]{@{}c@{}}\strut#1\strut\end{tabular}}

\begin{document}

\begin{table}[htp]
\centering
\setcounter{table}{1}% just for the example, isn't it?

\caption{Cardiorespiratory fitness stratified by age}

\begin{tabular}{
    c                   % Column 1
    S[table-format=2.1] % Column 2
    c                   % Column 3
    S[table-format=2.1] % Column 4
}

\toprule
\multicolumn{2}{c}{Male} &
\multicolumn{2}{c}{Female} \\
\cmidrule(lr){1-2} \cmidrule(l){3-4}
Age & {\splitcell{VO2\textsuperscript{\itshape b} \\ (\si{mL/kg/min})}} &   
Age & {\splitcell{VO2\textsuperscript{\itshape b} \\ (\si{mL/kg/min})}} \\
\midrule
20--29    & 46.1 & 20--29    & 38.2 \\
30--39    & 38.5 & 30--39    & 43   \\
40--49    & 26.7 & 40--49    & 21.6 \\
50--59    & 29.1 & 50--59    & 30.6 \\
$\geq 60$ & 28.3 & $\geq 60$ & 27.3 \\
\bottomrule
\end{tabular}

\bigskip

\caption{Isn't this better?}

\begin{tabular}{
    @{}
    c                   % Column 1
    S[table-format=2.1] % Column 2
    S[table-format=2.1] % Column 3
    @{}
}

\toprule
Age & \multicolumn{2}{c}{VO2\textsuperscript{\itshape b} (\si{mL/kg/min})} \\
\cmidrule(l){2-3}
& {\makebox[5em]{Male}} & {\makebox[5em]{Female}} \\
\midrule
20--29    & 46.1 & 38.2 \\
30--39    & 38.5 & 43   \\
40--49    & 26.7 & 21.6 \\
50--59    & 29.1 & 30.6 \\
$\geq 60$ & 28.3 & 27.3 \\
\bottomrule
\end{tabular}

\end{table}

\end{document}

在此处输入图片描述

使用threeparttable

\documentclass[a4paper,10pt]{article} 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{booktabs}
\usepackage{threeparttable}
\usepackage[table]{xcolor}
\usepackage{siunitx}
\usepackage[skip=0.5ex]{caption}

\setlength\heavyrulewidth{2pt}    % <---
\setlength\lightrulewidth{1.2pt}  % <---

\arrayrulecolor{teal}
\DeclareCaptionLabelFormat{uc}{\MakeUppercase{#1}~#2}                % <===
\DeclareCaptionFont{teal}{\color{teal}\fontfamily{iwona}\selectfont} % <===
\captionsetup[table]{position=top}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
\newcommand{\splitcell}[1]{%
  \begin{tabular}[t]{@{}c@{}}\strut#1\strut\end{tabular}%
}
\renewcommand{\TPTtagStyle}{\textit}

\begin{document}

\begin{table}[htp]
\centering
\setcounter{table}{1}% just for the example, isn't it?

\begin{threeparttable}

\caption{Cardiorespiratory fitness stratified by age}

\begin{tabular}{
    @{}
    c                   % Column 1
    S[table-format=2.1] % Column 2
    S[table-format=2.1] % Column 3
    @{}
}

\toprule
Age & \multicolumn{2}{c}{VO2 (\si{mL/kg/min})\tnote{b}} \\
\cmidrule(l){2-3}
& {\makebox[5em]{Male}} & {\makebox[5em]{Female}} \\
\midrule
20--29    & 46.1 & 38.2 \\
30--39    & 38.5 & 43   \\
40--49    & 26.7 & 21.6 \\
50--59    & 29.1 & 30.6 \\
$\geq 60$ & 28.3 & 27.3 \\
\bottomrule
\end{tabular}

\begin{tablenotes}
\item[b] VO2 means…
\end{tablenotes}
\end{threeparttable}

\end{table}

\end{document}

在此处输入图片描述

相关内容