1

After searching for hours I could not solve my problem. I have a large table with means and sd separated in three columns. I need to three below columns to be centered on the +-.

I tried using S tables but when I use S type tables I receive an error message with the column header on two lines. Any other simple solution keeping the 3 column form of the mean +- sd.

Here is a short extract of the table.

Thank you for your help. enter image description here

\begin{table}[]
\begin{tabular}{|l|r|c|l|}
\textit{Acides gras (\%)} & \multicolumn{3}{c}{\begin{tabular}[c]{@{}c@{}}My long three column and two lines header to be centered \\ (n = 91)\end{tabular}} \\
\textit{Saturés}          &                               &                             &                               \\
14:0                      & 2.6                           & ±                           & 0.7                          
\end{tabular}
\end{table}

! Edit

I am also there thank you ! Now my problem is that I need some rows to be in bold and when I use the bfseries in tblr only the first column is actually bold.

    \begin{table}
\sisetup{mode=text,           % <--
         separate-uncertainty % <--
        }
    \centering
\begin{tblr}{colspec = { X[l] *{6}{X[c, si={table-format=2.1(2)}]} },  % < ---
             colsep = 1pt,
             hspan  = minimal,
             row{1} = {guard},
             row{3} = {font=\bfseries}
            }
    \toprule
Acides gras (\%) 
        &   \SetCell[c=3]{c}    {My long three column and two lines header to be centered\\  ($n = 91$)}
            &   &   &   \SetCell[c=3]{c} {A shorter title\\ ($n = 91$)}
                        &   &               \\
        \cmidrule[lr]{2-4}
        \cmidrule[l]{5-7}
Saturés & & & & & & \\
14:0 & 2.6(0.7) & 1.8(0.7) & 2.2(0.6) & 6.9(0.7) & 4.3(1.7) & 3(0.8) \\
\end{tblr}
    \end{table}   

enter image description here

4
  • 1
    Since the \multicolumn is larger than the three columns, the extra space is added to the last column. Mar 29 at 12:17
  • 1
    I think you should ask a new question (maybe with a link to it). The reason you are not seeing the third header because it is overridden by the 2nd one. You should include two more column separators (&) between these two headers as your 2nd header spans three columns.
    – Imran
    Mar 30 at 9:12
  • 1
    Also AFAIK, resizing a table isn't recommended as it messes up the font. There was a post about this, but I couldn't find it.
    – Imran
    Mar 30 at 9:18
  • It is not entirely clear, what you after. Table on edited part of question seems to be written on different way as you try to achieve in showed code fragments. See my answer.
    – Zarko
    Mar 30 at 17:34

3 Answers 3

0
  • I suspect that your table contain values with uncertainty. This should be written by help of siunitx package and not how you try to do.
  • At use of tabularray package is advisable to strict use its syntax (no \newline, to each multi column cells should follows all ampersands of spanned columns).
  • MWE below try to reproduce part of table shown in the second image in question:
\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx}

\begin{document}
    \begin{table}
\sisetup{mode=text,           % <--
         separate-uncertainty % <--
        }
    \centering
\begin{tblr}{colspec = { X[l] *{6}{X[c, si={table-format=2.1(2)}]} },  % < ---
             colsep = 1pt,
             hspan  = minimal,
             row{1} = {guard}  % <--
            }
    \toprule
Acides gras (\%) 
        &   \SetCell[c=3]{c}    {My long three column and two lines header to be centered\\  ($n = 91$)}
            &   &   &   \SetCell[c=3]{c} {A shorter title\\ ($n = 91$)}
                        &   &               \\
        \cmidrule[lr]{2-4}
        \cmidrule[l]{5-7}
\textit{Saturés} 
        &   &   &   &   &   &               \\
14:0    & 2.6(0.7)  
            & 1.8(0.7)
                & 2.2(0.6)
                    & 6.9(0.7)
                        & 4.3(1.7)
                            & 3.0(0.8)      \\
16:0    & 6.6(1.6)
            & 5.4(2.0)
                & 6.9(1.7)
                    & 12.9(1.8)
                        & 12.9(5.6)
                            & 10.6(3.2)      \\
    \bottomrule
\end{tblr}
    \end{table}   
\end{document}

enter image description here

3

You could avoid the problem by using the tabularray package for your table:

\documentclass{article}

\usepackage{tabularray}

\begin{document}

\begin{table}[htbp]
\begin{tblr}{
  colspec={|X[l]|X[r]|c|X[l]|},
  vlines,
  cell{1}{1}={font=\itshape},
  hspan=minimal
}
Acides gras (\%) Saturés & \SetCell[c=3]{halign=c} My long three column and two lines header to be centered (n = 91) && \\
14:0 & 2.6 & ± & 0.7                          
\end{tblr}
\end{table}

\end{document}

enter image description here

2
  • Thank you. This looks good but as commented by John Kormylo it actually equalizes the three columns and minimizes the +- column. I actually have a large table with many means +- sd. With your solution the means and sd columns are very narrow and when I set tablewidth to pagewidth using an adjustbox I obtain my titles on many lines. I guess this is because of hspan=minimal but setting it to default or even does not solve the problem as it fits to header width or enlarges the +- colmn, respectively. Mar 29 at 16:32
  • @FrankDavid You can adjust the widths of all columns to whatever value you like. Can you update your question and include an example of such a problematic table? Using adjustbox on things which contain text is usually not a good idea. Mar 29 at 16:54
1

This equalizes the three columns by precomputing the widths needed, assuming that you want the three columns the same width.

I also threw in my (aesthetic) version.

\documentclass{article}
\begin{document}

\begin{table}[htp]
\sbox0{\begin{tabular}[c]{@{}c@{}}My long three column and
  two lines header to be centered \\ (n = 91) \end{tabular}}% measure width
\setlength{\dimen0}{\dimexpr \wd0-4\tabcolsep}% compute column width
\divide\dimen0 by 3
\begin{tabular}{|l|r|c|l|}
\textit{Acides gras (\%)} & \multicolumn{3}{c|}{\usebox0} \\
\textit{Saturés}          & \hspace{\dimen0} & \hspace{\dimen0} & \hspace{\dimen0} \\
14:0                      & 2.6              & ±                & 0.7                          
\end{tabular}
\end{table}

\begin{table}[htp]
\sbox0{My long three column and two lines header to be centered}% measure width
\sbox1{ $\pm$ }% measure width of @ divider
\setlength{\dimen0}{\dimexpr 0.5\wd0-0.5\wd1}% compute column width
\begin{tabular}{|l|r@{\usebox1}l|}
\textit{Acides gras (\%)} & \multicolumn{2}{c|}{\usebox0} \\
\textit{Saturés}          & \multicolumn{2}{c|}{(n = 91)} \\
14:0                      & \makebox[\dimen0][r]{2.6} & \makebox[\dimen0][l]{0.7}% only need \makebox in one row
\end{tabular}
\end{table}

\end{document}

demo

You must log in to answer this question.

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