# tabularx and siunitx troubles

Last two columns in the table in the following file do not align correctly. I can't seem to figure out what is wrong.

\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{booktabs,tabularx,float}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcommand{\mcx}[2]{\multicolumn{#1}{>{\hsize=\dimexpr3\hsize+4\tabcolsep\relax}C}{#2}}
\newcommand{\mcone}[1]{\multicolumn{1}{C}{#1}}
\begin{document}

\begin{table}
\caption{\label{orgc39fb42}
Prevalence of Undernourishment estimated for India with different parameter estimates}
\begin{tabularx}{\textwidth}{lS[table-format=1.3,round-precision=3]S[table-format=1.3,round-precision=3]S[table-format=2,round-precision=0]S[table-format=2,round-precision=0]}
\toprule
Mean & \mcone{CV} & \mcone{Skew} & \mcx{2}{Prev of Unment and met} \\
&  &  & \mcone{MPEX=1791} & \mcone{MPEX=2037}\\
\midrule
2455 & 0.25 & 0.55 & 15 & 23\\
2455 & 0.294 & 0.528 & 18 & 26\\
2135 & 0.294 & 0.528 & 27 & 39\\
\bottomrule
\end{tabularx}
\end{table}

\end{document}

• try \multicolumn{2}{c}{Prev of Unment and met} inseat of the mcx you have – koleygr Sep 9 '17 at 3:57

the definition of \mcx has error. corrected can be on different way but for me the simplest is the following :-)

\newcommand{\mcx}[2]{\multicolumn{#1}{>{\hsize=\dimexpr#1\hsize
+ #1\tabcolsep + #1\tabcolsep\relax}C|}{#2}}


beside this i would remove siunitx set up as option at loading of package and write to locally needs adopted sisetup locally .

a complete mwe is:

\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{booktabs,tabularx,float}
\usepackage{siunitx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcommand{\mcx}[2]{\multicolumn{#1}{>{\hsize=\dimexpr#1\hsize
+ #1\tabcolsep + #1\tabcolsep\relax}C|}{#2}}
\newcommand{\mcone}[1]{\multicolumn{1}{C}{#1}}

\begin{document}
\begin{table}
\caption{\label{orgc39fb42}
Prevalence of Undernourishment estimated for India with different parameter estimates}
\begin{tabularx}{\textwidth}{l
S[table-format=1.3]
S[table-format=1.3]
S[table-format=2.0]
S[table-format=2.0]
}
\toprule
Mean    & \mcone{CV}    & \mcone{Skew}  & \mcx{2}{Prev of Unment and met}       \\
&               &               & \mcone{MPEX=1791} & \mcone{MPEX=2037} \\
\midrule
2455    & 0.25          & 0.55          & 15                & 23                \\
2455    & 0.294         & 0.528         & 18                & 26                \\
2135    & 0.294         & 0.528         & 27                & 39                \\
\bottomrule
\end{tabularx}
\end{table}


which gives

see, if the following table looks better also to you:

\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[skip=1ex]{caption}
\usepackage{booktabs,tabularx}
\usepackage{siunitx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcommand{\mcx}[2]{\multicolumn{#1}{>{\hsize=\dimexpr#1\hsize
+ #1\tabcolsep + #1\tabcolsep\relax}C}{#2}}
\newcommand{\mcone}[1]{\multicolumn{1}{C}{#1}}

\begin{document}
\begin{table}
\caption{\label{orgc39fb42}
Prevalence of Undernourishment estimated for India with different parameter estimates}
\begin{tabularx}{\textwidth}{l
S[table-format=1.3]
S[table-format=1.3]
S[table-format=2.0]
S[table-format=2.0]
}
\toprule
&               &               & \mcx{2}{Prev of Unment and met}       \\
\cmidrule(lr){4-5}
Mean    & \mcone{CV}    & \mcone{Skew}  & \mcone{MPEX=1791} & \mcone{MPEX=2037} \\
\midrule
2455    & 0.25          & 0.55          & 15                & 23                \\
2455    & 0.294         & 0.528         & 18                & 26                \\
2135    & 0.294         & 0.528         & 27                & 39                \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}


note: in defining \multicolumn in tabularx which consider column type X from tabularx the width of multi column cells consider only maximal width of cells contents and not the widths of cells. with other words

\multicolumn{2}{>{\hsize=2\hsize}X}{...}


not consider all tabcolsep spaces in these two columns, consequently multicolumn cell is narrower for four spaces of tabcolsep. therefore generalized solution for "\newcommand\mcx{...} (by which is simpler typesetting table) is defined in above mwe as:

\newcolumntype{C}{>{\centering\arraybackslash}X}% definition of C
\newcommand{\mcx}[2]{\multicolumn{#1}{>{\hsize=\dimexpr#1\hsize
+ #1\tabcolsep
+ #1\tabcolsep\relax}
C}{#2}}


or shortly:

\newcommand{\mcx}[2]{\multicolumn{#1}{>{\hsize=\dimexpr#1\hsize
+ #1\tabcolsep2 \relax}
C}{#2}}

• Why 4\tabcolsep for 2 columns? Is it the correct way? (Just to understand) – koleygr Sep 9 '17 at 4:44
• because column width of X type columns not consider \coltabsep, consequently with \multicolumn{2}{...}{...} you had add all \coltabsep in two columns. about this i have discussion with @Mico (if I remember correctly) a time ago when he help someone with similar problem. unfortunately now i,m not able to provide link to this answer that you can read origin of solution. – Zarko Sep 9 '17 at 4:55
• Nice, +2 since I already upvoted the old answer. I would suggest to edit your old post (that it's title is more close to the problem) and to give some explanation of your command. Also I would suggest to use \numexpr #1*2\relax since in binary systems it is just a bitwise operation and it is faster :P – koleygr Sep 9 '17 at 5:01
• @koleygr, i try last suggestion but for some reason it doesn't work. i would check original answer and make correction as well add short explanation asap. – Zarko Sep 9 '17 at 5:08
• just don't multiply with \tabcolsep. Use \newcommand{\mcx}[2]{\multicolumn{#1}{>{\hsize=\dimexpr#1\hsize + \numexpr #1*2\relax \tabcolsep \relax} – koleygr Sep 9 '17 at 5:14

You may be over-thinking the issue of how to typeset the string that spans the two header cells, especially as the string that needs to be typeset is fairly short. Instead of fiddling with the \mcx macro, I would use a simple \multicolumn{2}{c}{...} directive to typeset the string in question. (Yes, that's all it takes!)

To provide more visual structure to the header, I'd also provide a \cmidrule(l){4-5} directive.

\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{booktabs,tabularx,float}
round-integer-to-decimal,
round-mode = places,
round-precision=1]{siunitx}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\newcommand{\mcone}[1]{\multicolumn{1}{C}{#1}}
\begin{document}

\begin{table}
\caption{Prevalence of Undernourishment estimated for India with
different parameter estimates} \label{orgc39fb42}
\begin{tabularx}{\textwidth}{ l
S[table-format=1.3,round-precision=3]
S[table-format=1.3,round-precision=3]
S[table-format=2,round-precision=0]
S[table-format=2,round-precision=0]}
\toprule
Mean & \mcone{CV} & \mcone{Skew} &
\multicolumn{2}{c}{Prev of Unment and met} \\
\cmidrule(l){4-5} % <--- new
&  &  & \mcone{MPEX=1791} & \mcone{MPEX=2037}\\
\midrule
2455 & 0.25  & 0.55  & 15 & 23\\
2455 & 0.294 & 0.528 & 18 & 26\\
2135 & 0.294 & 0.528 & 27 & 39\\
\bottomrule
\end{tabularx}
\end{table}

\end{document}


Addendum: The preceding approach works because the string that's being typeset is relatively short -- specifically, it is less wide than 2 basic C columns. If you have another table with a longer string, or if the string needs to span 3 (or more!) columns, you may want to redefine your \mcx macro as follows:

\newcommand{\mcx}[2]{\multicolumn{#1}%
{>{\hsize=\dimexpr #1\hsize +\numexpr2*#1-2\relax\tabcolsep \relax}C}%
{#2}}


and then typeset the string as \mcx{2}{Prev of Unment and met}. (You may verify for yourself that the solutions generated by \mcx{2}{Prev of Unment and met} and \multicolumn{2}{c}{Prev of Unment and met} are identical.)

How (why) does this revised form of \mcx work? Suppose there are n columns to be spanned by the string that may be longer than the n columns (and may therefore require automatic line wrapping). The usable width of each underlying C column is \hsize, and its total width (including the whitespace padding on either side) is \hsize+2\tabcolsep. The total width of the n columns of type C is therefore n\hsize+2n\tabcolsep. Since the total width of the combined column should be equal to this length, the usable width of the combined columns is n\hsize+2(n-1)\tabcolsep. Expressing this calculation in TeX syntax produces

\hsize=\dimexpr #1\hsize +\numexpr2*#1-2\relax\tabcolsep \relax


and that's what's given as the "prefix" to C in the definition of \mxc. (Do verify for yourself that \numexpr2*#1-2\relax\tabcolsep evaluates to 2(n-1).)

• Out of curiosity, does table-format=1.3, round-precision=3 behave differently than only table-format=1.3? – Diaa Dec 21 '19 at 16:43
• @Diaa - In the code shown above, the defaults -- set at the time the siunitx package is loaded -- are round-precision=1 along with round-mode = places. I.e., the defaults for this sample document are (a) rounding is on and (b) rounding is performed to 1 digit after the decimal marker. To keep enabling rounding while also wanting to show 3 decimal digits, it is very necessary to specify round-precision=3. The table-format option needs to be set just to make sure that siunitx reserves the exact required amount of space. – Mico Dec 21 '19 at 16:59

See @Zarko's and @Mico's answers that are more correct. Just leaving it here until see one of these answers to be accepted

If you don't like the answer in my comment try this:

\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{booktabs,tabularx,float}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcommand{\mcx}[2]{\multicolumn{#1}{>{\hsize=\dimexpr#1\hsize+\numexpr#1-1\relax\tabcolsep\relax}C}{#2}}
\newcommand{\mcone}[1]{\multicolumn{1}{C}{#1}}
\begin{document}

\begin{table}
\caption{\label{orgc39fb42}
Prevalence of Undernourishment estimated for India with different parameter estimates}
\begin{tabularx}{\textwidth}{lS[table-format=1.3,round-precision=3]S[table-format=1.3,round-precision=3]S[table-format=2,round-precision=0]S[table-format=2,round-precision=0]}
\toprule
Mean & \mcone{CV} & \mcone{Skew} & \mcx{2}{Prev of Unment and met} \\
& &  & \mcone{MPEX=1791} & \mcone{MPEX=2037}\\
\midrule
2455 & 0.25 & 0.55 & 15 & 23\\
2455 & 0.294 & 0.528 & 18 & 26\\
2135 & 0.294 & 0.528 & 27 & 39\\
\bottomrule
\end{tabularx}
\end{table}

\end{document}


I have changed your mcx definition.

• You are using an answer that did not accepted for some reason and you have changed it but not in a efficient way. the number 3 there was about the columns (to use three columns of your previous question). I changed it to #1 to accept the value of the number of the columns you need. Please think about to accept that answer (may be not mine if you don't like it) to show people who use the community that answer there works. – koleygr Sep 9 '17 at 4:16
• @kolexgr, +1 for correcting definition of \mcx. i have filling that somehow i'm guilty for this op problem :-( ... – Zarko Sep 9 '17 at 4:45
• Thanks @Zarko... I show your answer there... But have a question on your answer now... – koleygr Sep 9 '17 at 4:48
• How is \oldhsize defined? – Mico Sep 9 '17 at 9:50
• @Mico if you see my first edit I had a \let\oldhsize=\hsize there that thought it helped for the solution I had. But then I change it as non-needed and forgot it there (it was just \hsize`) – koleygr Sep 9 '17 at 10:27