5

I want to produce the the following table:

enter image description here

This is my working(only data for Male)

\documentclass[11pt]{article}

\usepackage{amsmath}
\usepackage{multirow}

\begin{document}

\begin{tabular}{|p{1.5cm}|p{2cm}|p{1.5cm}|p{2.7cm}|}\\ \cline{1-4}
\multirow{2}{*}{Exact age}  &\multicolumn{3}{c|}{male}\\ \cline{2-4} 
                        & Death probability & Number of lives  & Life expectancy\\ \cline{1-4}
0                       & 0.007589      & 100,000       & 73.98\\ \cline{1-4}
1                       & 0.000543      & 99,241            & 73.54 \\ \cline{1-4}
2                       & 0.000376      & 99,187            & 72.58 \\ \cline{1-4}                      
\end{tabular}

And this is what I get:

enter image description here

Your help is greatly appreciated.

Note: I'm not interested in thick lines or footnotes.

4
  • 4
    The first \\ causes the stray lines at the top. Commented Jun 28 at 12:06
  • You're right. That was the first culprit. Thank you for the fast comment.
    – Rafiq
    Commented Jun 28 at 12:15
  • This is a side note. It is unrelated to the present post. To the nice people who keep upvoting me because I visit this golden website and its brother MSE so many times a day. If I do so, that's because it is part of the process of my learning. And I'm not fishing for any reputation. Anyway thanks a lot for your warm encouragements.
    – Rafiq
    Commented Jun 28 at 18:30
  • @Rafiq You completely changed your question... the answers obviously don't address this new question. Reverse to your inital question and create a new one.
    – JeT
    Commented Jun 29 at 11:43

6 Answers 6

8

If you don't mind using the tabularray package, things are really easy

enter image description here

\documentclass[11pt]{article}

\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{tabularray}

\begin{document}

\begin{tblr}{
  colspec = {Q[c] Q[c] Q[c] l},
  cell{1}{2} = {c=3}{c},
  hline{1,3} = {-}{},
  hline{2} = {2-4}{},
}
  {Exact\\age} & {male} & & \\
  & {Death\\probability} & {Number\\of lives} & {Life\\expectancy} \\
  0 & \num{0.007589} & \num{100000} & \num{73.98} \\
  1 & \num{0.000543} & \num{99241} & \num{73.54} \\
  2 & \num{0.000376} & \num{99187} & \num{72.58} \\
  \hline
\end{tblr}

\end{document}

and with the booktabs options with tabularray

enter image description here

\documentclass[11pt]{article}

\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}

\begin{tblr}{
  colspec = {Q[c] Q[c] Q[c] l},
  cell{1}{2} = {c=3}{c},
  hline{2} = {2-4}{},
}
\toprule
  {Exact\\age} & {male} & & \\
  & {Death\\probability} & {Number\\of lives} & {Life\\expectancy} \\
\midrule
  0 & \num{0.007589} & \num{100000} & \num{73.98} \\
  1 & \num{0.000543} & \num{99241} & \num{73.54} \\
  2 & \num{0.000376} & \num{99187} & \num{72.58} \\
\bottomrule
\end{tblr}

\end{document}
3
  • Thanks @Jet. Now I'm focusing to learn tables as best as I could.
    – Rafiq
    Commented Jun 28 at 17:18
  • 2
    then spend time on tabularray :)
    – JeT
    Commented Jun 28 at 17:33
  • 1
    I'll do. Be sure of that.
    – Rafiq
    Commented Jun 28 at 18:22
6

As commented, the first \\ causes the extra vertical lines. Also, \cline{1-4} is equivalent to \hline, which better describes how you're using it.

But I would redesign the table a bit. The first thing is to use booktabs and take its advice: vertical lines kill puppies, and horizontal lines are usually unnecessary (more seriously, lines should not be necessary to help interpret the data in a table, and can actually hinder interpretation). We can also use siunitx to have it format the numbers and line things up nicely. (And to keep the line breaks, we'll use \specialcell from https://tex.stackexchange.com/a/19678/107497). I end up with:

\documentclass[11pt]{article}

\usepackage{amsmath}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{siunitx}

\newcommand{\specialcell}[2][c]{%
  \begin{tabular}[#1]{@{}c@{}}#2\end{tabular}}

\begin{document}

\begin{tabular}{
S[table-format=1]
 S[table-format=1.6]
  S[table-format=6]
   S[table-format=2.2]}\toprule
{\multirow{2}{*}{\specialcell{Exact\\age}}} & \multicolumn{3}{c}{male}\\ \cmidrule(l){2-4}
    & {\specialcell{Death\\probability}}
    & {\specialcell{Number\\of lives}}
    & {\specialcell{Life\\expectancy}} \\ \midrule
0   & 0.007589      & 100000     & 73.98 \\
1   & 0.000543      & 99241      & 73.54 \\
2   & 0.000376      & 99187      & 72.58 \\\bottomrule
\end{tabular}

\end{document}

which renders as code output

When you go to include females, you'll want to change the \cmidrule(l) to (lr). And as a bonus, this table is a little more compact (although that is mostly due to new linebreaks in the first and last columns).

1
  • Thanks for your answer. I'll take your advice into account. I struggled a lot with this table.
    – Rafiq
    Commented Jun 28 at 16:20
4

One more solution using tabularray package. In it is employed siunitx package for defining column types and tblr-extras for defining caption style using caption package:

\documentclass{article}
\usepackage{tabularray}
\usepackage{tblr-extras}    %%%% new, it works with recent version   
\UseTblrLibrary{booktabs,
                caption,
                siunitx}
\usepackage{caption}
\captionsetup[table]{skip=1ex,
                     font = {small, sf},
                     labelfont = bf,
                     justification=centerlast
                    }

\begin{document}
    \begin{table}[ht]
    \centering
\begin{talltblr}[
caption = {This table is about something},
  label = {tblr:something},
 note{} = {DP: Death probability;\quad
           NL: Number of lives;\quad
           LE: Life expectancy}
                ]{colspec = {Q[c, si={table-format=1}]
                             *{2}{Q[c, si={table-format=1.6}] 
                                  Q[c, si={table-format=7}]
                                  Q[c, si={table-format=2.2}]}
                            },
                  cell{1}{1} = {r=2}{},
                  cell{1}{2,5} = {c=3}{},
                  row{1,2}  = {guard}
                  }
    \toprule
{Exact\\age} 
    &   Male    &           &           &   Female  &           &       \\
    \cmidrule[lr]{2-4}
    \cmidrule[lr]{5-7}
    &   DL      & NL        &   LE      &   DL      & NL        &   LE  \\
    \midrule
 0 & 0.007589   & 100000    & 73.98     & 0.007589  & 100000    & 73.98 \\
 1 & 0.000543   &  99241    & 73.54     & 0.000543  &  99241    & 73.54 \\
 2 & 0.000376   &  99187    & 72.58     & 0.000376  &  99187    & 72.58 \\
    \bottomrule
\end{talltblr}
    \end{table}
\end{document}

enter image description here

2
  • Thanks for promoting my small package
    – Mane32
    Commented 2 days ago
  • @Mane32, your are welcome. Your package is excellent. Now I use it at talltblr and longtblr regularly. Thank you for for it!
    – Zarko
    Commented 2 days ago
3

If you really like caged tables:

\documentclass[11pt]{article}

\usepackage{amsmath}
\usepackage{multirow}

\begin{document}

\begin{tabular}{|p{1.5cm}|p{2cm}|p{1.5cm}|p{2.7cm}|}\\ \cline{1-4}
\multirow{2}{*}{Exact age}  &\multicolumn{3}{c|}{male}\\ \cline{2-4} 
                        & Death probability & Number of lives  & Life expectancy\\ \cline{1-4}
0                       & 0.007589      & 100,000       & 73.98\\ \cline{1-4}
1                       & 0.000543      & 99,241            & 73.54 \\ \cline{1-4}
2                       & 0.000376      & 99,187            & 72.58 \\ \cline{1-4}                      
\end{tabular}

Using a legend, the size of the columns can be kept tight.

enter image description here

An open look is, in my opinion, much better.

\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{siunitx}
\usepackage{booktabs}

\sisetup{
  group-digits=integer,
  group-separator={,},
}

\begin{document}

\begin{table}[htp]
\centering

\begin{tabular}{
 @{}
 c
 S[table-format=1.6]
 S[table-format=6.0]
 S[table-format=2.2]
 S[table-format=1.6]
 S[table-format=6.0]
 S[table-format=2.2]
 @{}
}
\toprule
\smash{\begin{tabular}[t]{@{}c@{}}Exact \\ age\end{tabular}}
& \multicolumn{3}{c}{Male}
& \multicolumn{3}{c@{}}{Female} \\
\cmidrule(lr){2-4} \cmidrule(l){5-7}
& {DP} & {NL} & {LE} & {DP} & {NL} & {LE} \\
\midrule
0 & 0.007589 & 100000 & 73.98 & 0.007589 & 100000 & 73.98 \\
1 & 0.000543 &  99241 & 73.54 & 0.000543 &  99241 & 73.54 \\
2 & 0.000376 &  99187 & 72.58 & 0.000376 &  99187 & 72.58 \\
\midrule[\heavyrulewidth]
\multicolumn{7}{@{}l@{}}{%
  \footnotesize
  DP: Death probability;\quad
  NL: Number of lives;\quad
  LE: Life expectancy%
}
\end{tabular}

\caption{This table is about something}

\end{table}

\end{document}

enter image description here

1
  • Thank you so much for your invaluable help and for all your contributions.
    – Rafiq
    Commented Jun 29 at 13:19
3

A suggestion with {NiceTabular} of nicematrix.

\documentclass{article}
\usepackage{nicematrix,booktabs,siunitx}

\begin{document}

\begin{NiceTabular}{cccc}
\toprule
\Block[t]{2-1}{Exact\\ age} & \Block{1-3}{male} \\
\cmidrule(l){2-4}
& \Block{}{Death\\probability} & \Block{}{Number\\ of lives} & \Block{}{Life\\ expectancy} \\
\midrule
0 & \num{0.007589} & \num{100000} & \num{73.98} \\
1 & \num{0.000543} & \num{99241} & \num{73.54} \\
2 & \num{0.000376} & \num{99187} & \num{72.58} \\
\bottomrule
\end{NiceTabular}

\end{document}

Output of the above code

1
  • Thanks for sharing with me a little knowledge. I hope I could soon be able to learn fast, and absorb a great quantity.
    – Rafiq
    Commented 2 days ago
0

From all the answers I got, now I Know how a professional table must be made. That said, I kept asking how did my book made that original one. And I figured out.

\begin{tabular}{|l|l|l|l|l|l|l|}\cline{1-7}
&\multicolumn{3}{|c|}{male} & \multicolumn{3}{c|}{Female}\\ \cline{2-7}
Exact &Death      &Number & Life            &Death      &Number & Life\\ 
age   &probability & of lives  & expectancy & probability & of lives & expectancy   \\ \cline{1-7}
0      & 0.007589 & 100,000 & 73.98      & 0.006234  & 100,000 & 79.35      \\ \cline{1-7}
1     & 0.000543  & 99,241   & 73.54         & 0.000447  & 99,377   & 78.84         \\ \cline{1-7}
2    &  0.000376  & 99,187   & 72.58         & 0.000376  & 99,187   & 72.58          \\ \cline{1-7}
\end{tabular}

enter image description here

to get rid of some horizontal lines, some c lines must be deleted

\begin{tabular}{|l|l|l|l|l|l|l|}\cline{1-7}
&\multicolumn{3}{|c|}{male} & \multicolumn{3}{c|}{Female}\\ \cline{2-7}
Exact &Death      &Number & Life            &Death      &Number & Life\\ 
age   &probability & of lives  & expectancy & probability & of lives & expectancy   \\ \cline{1-7}
0      & 0.007589 & 100,000 & 73.98      & 0.006234  & 100,000 & 79.35      \\ 
1     & 0.000543  & 99,241   & 73.54         & 0.000447  & 99,377   & 78.84         \\ 
2    &  0.000376  & 99,187   & 72.58         & 0.000376  & 99,187   & 72.58          \\ \cline{1-7}
\end{tabular}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .