TransWikia.com

How to center header and numbers in a scientific table and hyphen between numbers

TeX - LaTeX Asked by Svein on August 11, 2021

I just started to use LaTex and it’s an amazing tool. Exposing my limited knowledge on code development, could someone please provide a solution to my problem? I need a hyphen between the age intervals. It is important that age and VO2 be centered under men and female. Additionally, I need all the numbers to be centered under age and VO2 and the numbers in the last column not to merge. Could someone with extended knowledge on LaTex-coding please show me how this is done? The code is presented under the text. Thanks in advance

Regards Svein

documentclass[a4paper,10pt]{article} 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
usepackage{babel}
usepackage{mathtools,amssymb}
usepackage{booktabs, makecell, 
            threeparttable}       % <---
setlengthheavyrulewidth{2pt}    % <---
setlengthlightrulewidth{1.2pt}  % <---

setcellgapes{3pt}
renewcommandtheadfont{normalsizebfseries}
renewcommandtheadgape{}

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#1strutend{tabular}}

begin{document}

begin{table}[htp]
centering
renewcommandthetable{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}

3 Answers

  • first column is empty, remove it
  • in answer is consider siunitx version 3
  • columns with range numbers are selected as c type
documentclass[a4paper,10pt]{article}

%usepackage{babel}                % which one?
usepackage{mathtools,amssymb}
usepackage{booktabs, makecell,
            threeparttable}       % <---
%setlengthheavyrulewidth{2pt}    % <--- delete % if you like to have thicker rule
%setlengthlightrulewidth{1.2pt}  % <--- delete % if you like to have thicker 
renewcommandtheadfont{normalsizebfseries}
renewcommandtheadgape{}
NewExpandableDocumentCommandmcc{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{VO2tnote{b}  
                      (unit{mL/kg/min})}}
            &   Age   
                &   {makecell{VO2tnote{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}

enter image description here

Answered by Zarko on August 11, 2021

Another solution, with some modifications. In particular, I used the makecell command, from the homonymous package, which is designed to allow line breaks in standard cells. As to the endash for numerical ranges, it is an option set with siunitx by the key range-phrase=.

documentclass[a4paper,10pt]{article}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
usepackage{babel}
usepackage{mathtools,amssymb}
usepackage{booktabs, makecell, threeparttable} % <---
setlengthheavyrulewidth{2pt} % <---
setlengthlightrulewidth{1.2pt} % <---

setcellgapes{3pt}
renewcommandtheadfont{normalsizebfseries}
renewcommandtheadgape{}

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
renewcommandthetable{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} 

enter image description here

Edit: a modification of the code to have ‘Men’ and ‘Female’ better centred w.r.t. ‘Age’ and ‘VO2’:

begin{table}[htp]
sisetup{range-phrase= --}
centering
renewcommandthetable{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}{Menhskip1.5emmbox{}}
& multicolumn{2}{c}{Femalehskip1.5emmbox{}} 
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}

enter image description here

Answered by Bernard on August 11, 2021

The table, as is, commits a few sins: never repeat data!

I present here two realizations, with a few adjustments for the first one which is like yours.

documentclass[a4paper,10pt]{article} 

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

setlengthheavyrulewidth{2pt}    % <---
setlengthlightrulewidth{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#1strutend{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{VO2textsuperscript{itshape b}  (si{mL/kg/min})}} &   
Age & {splitcell{VO2textsuperscript{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}{VO2textsuperscript{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}

enter image description here

Using threeparttable:

documentclass[a4paper,10pt]{article} 

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

setlengthheavyrulewidth{2pt}    % <---
setlengthlightrulewidth{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#1strutend{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}

enter image description here

Answered by egreg on August 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP