1

I have a problem with a table in Latex, that i just cant get to work. I found online that i can put a table into a multicolumn environment, to change from 4 columns to 3, and that seems to work well if i go from 4 to 2, but not 4 to 3.

I'm not sure where the problem is, but i guess I have to define the width of the table inside multicolumn somehow.

I have included the code as well as the generated table. It should hopefully be obvious how I want it to look when done correctly :)

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}

\usepackage{tabularx}
\setlength{\arrayrulewidth}{1mm}
\setlength{\tabcolsep}{18pt}
\begin{document}

\begin{table}[]
    \centering
    \begin{tabular}{|c|c|c|c|}
        \hline
       \multicolumn{4}{|c|}{Site 3 freeboard statistics}\\
       \hline
        Data & Mean ($\mu$) & Median & Spread ($\sigma$) \\
        \hline
        ALS mid line & 1.018 & 0.947 & 0.539\\
        ALS mid full width & 0.481 & 0.389  & 0.296 \\
        ALS at CryoSat width & 0.664 & 0.561 & 0.500\\
        Karen & 1.047 & 1.017 & 0.432 \\
        CryoSat w/o. snow & 0.200 & 0.200 & 0.080\\
        CryoSat w. snow & 0.401 & 0.401 & 0.101\\
        \hline
        \multicolumn{4}{|c|}{Averaged to CryoSat resolution}\\
        \hline
        \multicolumn{4}{|c|}{\begin{tabular}{c|c|c} &  First point & Second point  \\
        \hline
        ALS mid line & 0.826 & 1.211 \\
        ALS mid full width & 0.545 & 0.413 \\
        ALS at CryoSat width & &  \\
        KAREN & 0.877 & 1.220 \\
        CryoSat w/o. snow & 0.12 & 0.28 \\
        CryoSat w. snow & 0.502 & 0.300
        \end{tabular}}\\
        \hline
    \end{tabular}
    \caption{Snow statistics when fitting the in situ data only along latitude.}
    \label{tab:site2_stat}
\end{table}
\end{document}

Image of produced table. Lower table should have same width as upper table, while still have centered columns

5
  • Why not just go for a single table with a total of 6 columns instead of repeating the first column?
    – leandriis
    Dec 19, 2019 at 6:44
  • Could you please make your example code compilable by adding the documentclass as well as the relevant packages?
    – leandriis
    Dec 19, 2019 at 6:45
  • @leandriis That is a good point :D could be a solution. The text just get kinda small then. And yes wil do that
    – Mathias
    Dec 19, 2019 at 7:04
  • What is the expected outcome? Are you looking to keep the same first column, yet have the rest spread out to match the upper table?
    – Werner
    Dec 19, 2019 at 7:16
  • @Werner Yea i was looking for something like \multicolumn{1.5}{|c|}{<stuff>}
    – Mathias
    Dec 19, 2019 at 8:08

2 Answers 2

3

Your table I would write on the following way:

\documentclass[12pt,a4paper]{article}
\usepackage{booktabs, makecell}
\usepackage{siunitx}

\begin{document}
    \begin{table}[htb]
    \centering
    \setlength\tabcolsep{0pt}
\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}} 
                l *{5}{S[table-format=1.3]} }
    \toprule
    &   \multicolumn{3}{c}{\makecell{Site 3\\ freeboard statistics}}
        &  \multicolumn{2}{c}{\makecell{Averaged to\\ 
                                        resolution}}        \\
    \cmidrule(l){2-4}\cmidrule(l){5-6}
Data                    & {\makecell[t]{Mean\\ ($\mu$)}}   
                                & {\makecell[t]{Median}} 
                                        & {\makecell[t]{Spread\\ ($\sigma$)}}
                                                & {\makecell[t]{First\\ point}}
                                                        & {\makecell[t]{Second\\ point}}   \\
    \midrule
ALS mid line            & 1.018 & 0.947 & 0.539 & 0.826 & 1.211 \\
ALS mid full width      & 0.481 & 0.389 & 0.296 & 0.545 & 0.413 \\
ALS at CryoSat width    & 0.664 & 0.561 & 0.500 &       &       \\
Karen                   & 1.047 & 1.017 & 0.432 & 0.877 & 1.220 \\
CryoSat w/o. snow       & 0.200 & 0.200 & 0.080 & 0.12  & 0.28  \\
CryoSat w. snow         & 0.401 & 0.401 & 0.101 & 0.502 & 0.300 \\
    \bottomrule
\end{tabular*}
    \caption{Snow statistics when fitting the in situ data only along latitude.}
    \label{tab:site2_stat}
\end{table}
\end{document}

enter image description here

  • in table design comments below your question are considered
  • for numbers are used S column type defined in the siunitx package
  • table's vertical lines are omitted
  • for horizontal lines are used rules defined in the booktabs package
  • to my taste the suggested design of the table has more professional look
3
  • Exactly what I had in mind...
    – leandriis
    Dec 19, 2019 at 10:16
  • @leandriis, thank you! I have a hard time understanding why so many people jailed data in a prison with strong bars. Obviously the flavors of what's aesthetic are very different ... :-)
    – Zarko
    Dec 19, 2019 at 10:24
  • Thank you that is pretty neat i must say :)
    – Mathias
    Dec 19, 2019 at 13:09
2

And here is the tabular OP asks for, with fences and thick bars. As you will see, I defined three new column types, 4/10, 3/10 and 2/10 of the line width. The ‘clue’ here is the line defining the width of the second tabular:

\multicolumn{4}{@{}c@{}}{\begin{tabular}{|N|M|M|}

Here, I remove the side bearing and vertical lines from the multicolumn cell. Then the nested tabular will be as wide as the ‘main’ tabular, and it is easy to calculate the column widths.

If you use tabularx in combination with a fixed width w-column from array, the calculation is much simpler. First, you set a fixed width column, thereafter three X-columns for the ‘main’ tabular and two for the nested tabular. tabularx does all the complicated maths for you. I define a C column, which only is a centred X-column. See example 2 below

NB! @Zarko’s tabular is much nicer, I recommend that you build your tabulars according his recommendation.

Example 1 – tabular

enter image description here

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}


\usepackage{tabularx, array}
\setlength{\arrayrulewidth}{1mm}
\setlength{\tabcolsep}{18pt}
\setlength{\extrarowheight}{2pt}

\newcolumntype{N}{wc{\dimexpr((\linewidth/10)*4-(2\tabcolsep+2\arrayrulewidth))\relax}}
\newcolumntype{O}{wc{\dimexpr((\linewidth/10)*2-(2\tabcolsep+\arrayrulewidth))\relax}}
\newcolumntype{M}{wc{\dimexpr((\linewidth/10)*3-(2\tabcolsep+\arrayrulewidth))\relax}}


\begin{document}

\begin{table}[th!]
    \centering
    \begin{tabular}{|N|*{3}{O|}}
        \hline
       \multicolumn{4}{|c|}{Site 3 freeboard statistics}\\
       \hline
        Data & Mean ($\mu$) & Median & Spread ($\sigma$) \\
        \hline
        ALS mid line & 1.018 & 0.947 & 0.539\\
        ALS mid full width & 0.481 & 0.389  & 0.296 \\
        ALS at CryoSat width & 0.664 & 0.561 & 0.500\\
        Karen & 1.047 & 1.017 & 0.432 \\
        CryoSat w/o. snow & 0.200 & 0.200 & 0.080\\
        CryoSat w. snow & 0.401 & 0.401 & 0.101\\
        \hline
        \multicolumn{4}{|c|}{Averaged to CryoSat resolution}\\
        \hline
        \multicolumn{4}{@{}c@{}}{\begin{tabular}{|N|M|M|}
         &  First point & Second point  \\
        \hline
        ALS mid line & 0.826 & 1.211 \\
        ALS mid full width & 0.545 & 0.413 \\
        ALS at CryoSat width & &  \\
        KAREN & 0.877 & 1.220 \\
        CryoSat w/o. snow & 0.12 & 0.28 \\
        CryoSat w. snow & 0.502 & 0.300
        \end{tabular}}\\
        \hline
    \end{tabular}
    \caption{Snow statistics when fitting the in situ data only along latitude.}
    \label{tab:site2_stat}
\end{table}
\end{document}

Example 2 – tabularx

enter image description here

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularx, array}
\setlength{\arrayrulewidth}{1mm}
\setlength{\tabcolsep}{18pt}
\setlength{\extrarowheight}{2pt}

\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}

\begin{table}[th!]
    \centering
    \begin{tabularx}{\linewidth}{|wc{4cm}|*{3}{C|}}
        \hline
       \multicolumn{4}{|c|}{Site 3 freeboard statistics}\\
       \hline
        Data & Mean ($\mu$) & Median & Spread ($\sigma$) \\
        \hline
        ALS mid line & 1.018 & 0.947 & 0.539\\
        ALS mid full width & 0.481 & 0.389  & 0.296 \\
        ALS at CryoSat width & 0.664 & 0.561 & 0.500\\
        Karen & 1.047 & 1.017 & 0.432 \\
        CryoSat w/o. snow & 0.200 & 0.200 & 0.080\\
        CryoSat w. snow & 0.401 & 0.401 & 0.101\\
        \hline
        \multicolumn{4}{|c|}{Averaged to CryoSat resolution}\\
        \hline
        \multicolumn{4}{@{}c@{}}{{\begin{tabularx}{\linewidth}{|wc{4cm}|C|C|}
         &  First point & Second point  \\
        \hline
        ALS mid line & 0.826 & 1.211 \\
        ALS mid full width & 0.545 & 0.413 \\
        ALS at CryoSat width & &  \\
        KAREN & 0.877 & 1.220 \\
        CryoSat w/o. snow & 0.12 & 0.28 \\
        CryoSat w. snow & 0.502 & 0.300
        \end{tabularx}}}\\
        \hline
    \end{tabularx}
    \caption{Snow statistics when fitting the in situ data only along latitude.}
    \label{tab:site3_stat}
\end{table}
\end{document}
4
  • Thank you that makes sense. But as you say yourself @Zarko made a better table than i thought of :)
    – Mathias
    Dec 19, 2019 at 13:10
  • @Mathias Still, you can learn some tricks studying my solution, for example how to set fixed width columns and how to fit them between the margins. :)
    – Sveinung
    Dec 19, 2019 at 13:12
  • Tabular in a multicolumn. +1 for out of the box thinking. Dec 19, 2019 at 16:45
  • @JohnKormylo Thank you for nice words :)
    – Sveinung
    Dec 19, 2019 at 19:22

You must log in to answer this question.

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