0

I am trying to use tabularx to create a table that doesn't overflow the page horizontally, but I am now having issues with the values in columns overflowing.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multirow}
\usepackage{tabularx,ragged2e,booktabs}

\begin{document}

\begin{table}[h]
\begin{tabularx}{\textwidth}{c*{12}{{\RaggedRight \arraybackslash}X}}
\toprule
\multirow{2}[4]{*}{Statistics} & \multicolumn{3}{c}{Speed} & \multicolumn{3}{c}{Heading}  & \multicolumn{3}{c}{Depth} & \multicolumn{3}{c}{Distance to coast}\\
\cmidrule(lr){2-4} \cmidrule(lr){5-7} \cmidrule(lr) {8-10} \cmidrule(lr) {11-13}
 & OTB & PS & DRB & OTB & PS & DRB & OTB & PS & DRB & OTB & PS & DRB \\
\midrule
mean & 142.87 & 137.72 & 444 & 179.45 & 177.63 & 444 & -65.76 & -60.18 & 444 & 15021.19 & 6061.46 & 444 \\
STD & 102.94 & 190.15 & 444 & 109.92 & 104.11 & 444 & 42.37 &  36.11 & 444 & 14990.39 & 7907.20 & 444 \\
lower quartile & 102 & 3 & 444 & 91 & 95 & 444 & -84.76 & -81.74 & 444 & 4526.24 & 1377.05 & 444 \\
median & 113 & 12 & 444 & 175 & 175 & 444 & -56.71 & -61.33 & 444 & 8883.02 & 3657.02 & 444 \\
upper quartile & 145 & 381 & 444 & 277 & 270 & 444 & -38.18 & -33.4 & 444 & 212 & 7605.9 & 444 \\
\bottomrule
\end{tabularx}
\caption{Statistics of speed, heading and depth for OTB & PS fishing vessel types}
\label{table_statistics}
\end{table}


\end{document}

Image below shows my issue with values in columns overflowing as well as the horizontal lines not going to the end of the table:

enter image description here How to fix this?

7
  • 1
    Please add a full preamle to your example code. Otherwise we don't know the font size, margins etc.
    – marv
    Jun 20, 2022 at 12:20
  • @marv Ok I added preamle. Jun 20, 2022 at 13:15
  • Please make sure that your example does not depend on data we do not have access to otherwise we cannot test this. It seems that most of the settings from your example is irrelevant for your issue. Please make the example as short as possible (this is also a valuable debugging tool, reducing to the smallest example)
    – daleif
    Jun 20, 2022 at 13:36
  • @daleif Alright, I edited my post and included the full latex code with the preamle that I copied into a new project. It only includes the table that i am having issues with. So you should be able to replicate my problem now. Jun 20, 2022 at 13:57
  • Please make sure that the example actually compiles. The array package is missing, {\RaggedRight \arraybackslash} should be >{\RaggedRight \arraybackslash} and there is an unescaped & in the caption
    – daleif
    Jun 20, 2022 at 14:14

2 Answers 2

0

After correcting issues, your table simply did not fit the available space. If you don't want to change font, decrease margin, or use another package (such as tabularray which I'd also recommend) etc., you could just move two other groups in the bottom part

enter image description here

\documentclass{article}
\usepackage{multirow}
\usepackage{tabularx,ragged2e,booktabs}


\begin{document}

\begin{table}[h]
    \begin{tabularx}{\textwidth}{X *6{r}}
        \toprule \addlinespace[6pt]
        Statistics &
        \multicolumn{3}{c}{Speed} &
        \multicolumn{3}{c}{Heading} \\
        \cmidrule(lr){2-4}
        \cmidrule(lr){5-7}
        &
        \multicolumn{1}{c}{OTB} &
        \multicolumn{1}{c}{PS} &
        \multicolumn{1}{c}{DRB} &
        \multicolumn{1}{c}{OTB} &
        \multicolumn{1}{c}{PS} &
        \multicolumn{1}{c}{DRB} \\
        \midrule
        mean            & 142.87 & 137.72 & 444 &   179.45 &  177.63 & 444 \\
        STD             & 102.94 & 190.15 & 444 &   109.92 &  104.11 & 444 \\
        lower quartile  & 102    &   3    & 444 &    91    &   95    & 444 \\
        median          & 113    &  12    & 444 &   175    &  175    & 444 \\
        upper quartile  & 145    & 381    & 444 &   277    &  270    & 444 \\
        \addlinespace[3pt]
        \toprule \addlinespace[6pt]
        Statistics &
        \multicolumn{3}{c}{Depth} &
        \multicolumn{3}{c}{Distance to coast}\\
        \cmidrule(lr){2-4}
        \cmidrule(lr){5-7}
        &
        \multicolumn{1}{c}{OTB} &
        \multicolumn{1}{c}{PS} &
        \multicolumn{1}{c}{DRB} &
        \multicolumn{1}{c}{OTB} &
        \multicolumn{1}{c}{PS} &
        \multicolumn{1}{c}{DRB} \\
        \midrule
        mean            & -65.76 & -60.18 & 444 & 15021.19 & 6061.46 & 444 \\
        STD             &  42.37 &  36.11 & 444 & 14990.39 & 7907.20 & 444 \\
        lower quartile  & -84.76 & -81.74 & 444 &  4526.24 & 1377.05 & 444 \\
        median          & -56.71 & -61.33 & 444 &  8883.02 & 3657.02 & 444 \\
        upper quartile  & -38.18 & -33.4  & 444 &   212    & 7605.9  & 444 \\
        \bottomrule
    \end{tabularx}
    \caption{Statistics of speed, heading and depth for OTB \& PS fishing vessel types}
    \label{table_statistics}
\end{table}

\end{document}
1
  • your table code has error in caption. Instead of & should be \&
  • table is quite wide, so probably you will have problem to fit it on page. In MWE below I assume, that page margins are 20mm and that you will agree with use of the \small font size in table.
  • If this is not a case, than you have two options:
    • rotate table in landscape orientation
    • locally extend \textwidh with use of the changepage caption or use \newgeometry for page with this table

For table I would use tabularray (last version 2022B) instead tabularx and siunit package for S columns, which enable aligning numbers at decimal points.

\documentclass{article}
\usepackage[margin=20mm]{geometry}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx}

\begin{document}
    \begin{table}[h]
    \sisetup{tight-spacing}
    \small
\begin{tblr}{colsep=2pt,
             colspec = {@{} c *{3}{X[c, si={table-format=3.2}]
                               X[c, si={table-format=3.2}]
                               X[c, si={table-format=3.0}]}
                            %
                               X[1.2, c, si={table-format=5.2}]
                               X[1.2, c, si={table-format=5.2}]
                               X[c, si={table-format=3.0}]
                        @{}},
             row{1,2} ={guard}
             }
    \toprule
\SetCell[r=2]{c}  Statistics
    &\SetCell[c=3]{c}   Speed       &   &   
    &\SetCell[c=3]{c}   Heading     &   &
    &\SetCell[c=3]{c}   Depth       &   &
    &\SetCell[c=3]{c}   Distance to coast
                                    &   &   \\
    \cmidrule[lr]{2-4} 
    \cmidrule[lr]{5-7} 
    \cmidrule[lr] {8-10} 
    \cmidrule[lr] {11-13}
        & OTB       & PS        & DRB
        & OTB       & PS        & DRB
        & OTB       & PS        & DRB
        & OTB       & PS        & DRB       \\
    \midrule
mean    & 142.87    & 137.72    & 444 
        & 179.45    & 177.63    & 444 
        & -65.76    & -60.18    & 444 
        & 15021.19  & 6061.46   & 444       \\
STD     & 102.94    & 190.15    & 444 
        & 109.92    & 104.11    & 444 
        & 42.37     &  36.11    & 444 
        & 14990.39  & 7907.20   & 444       \\
lower quartile 
        & 102       & 3         & 444 
        & 91        & 95        & 444 
        & -84.76    & -81.74    & 444 
        & 4526.24   & 1377.05   & 444       \\
median  & 113       & 12        & 444 
        & 175       & 175       & 444 
        & -56.71    & -61.33    & 444 
        & 8883.02   & 3657.02   & 444       \\
upper quartile 
        & 145       & 381       & 444 
        & 277       & 270       & 444 
        & -38.18    & -33.4     & 444 
        & 212       & 7605.9    & 444       \\
    \bottomrule
\end{tblr}
\caption{Statistics of speed, heading and depth for OTB \& PS fishing vessel types}
\label{table_statistics}
    \end{table}
\end{document}

enter image description here

You must log in to answer this question.

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