2

tabularx with a \newcolumntype and \multicolumn adds another incomplete column to the right side

\documentclass{article}
\usepackage{tabularx}

\newcolumntype{s}{>{\hsize=0.5\hsize}  X}

\begin{document}
  \begin{tabularx}{0.5\textwidth}{ |s|X|}
    \hline
    \multicolumn{2}{|X|}{Item}\\
    \hline
    a & b\\
    \hline 
    c & d \\
    \hline
  \end{tabularx}
\end{document}

where does this come from and how can I remove it?

table

4

To have two X columns, one of them being half as wide as the other, it is not a coefficient of 0.5you have to choose, but 2 coefficients such that their sum is 2, one of them being half the other. In other words $2/3$ and $4/3$.

For the multicolumn (which is not recommended here), use the l specifier, not X. The X specifier leads to a multicolumn that is half the table wide.

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{multirow, tabularx}
\newcolumntype{s}{>{\hsize=0.667\hsize\arraybackslash} X}
\newcolumntype{Y}{>{\hsize=1.333\hsize\arraybackslash} X}

\begin{document}

\vspace*{1cm}
\noindent \begin{tabularx}{0.5\textwidth}{ |s| >{\arraybackslash}Y|}
    \hline
 \multicolumn{2}{|l|}{Item}\\
    \hline
    a & b\\
    \hline
    c & d \\
    \hline
  \end{tabularx}

\end{document} 

enter image description here

| improve this answer | |
  • if I had 3 columns, would the sum have to be 3, too? and so on? also why is multicolumn not recomended and what alterantiv would be better? – Max Feb 29 '16 at 21:02
  • Yes, the sum of the coefficients must be equal to the number of columns. \multicolumn is not recommended in case you have X type columns of different widths, as here. I don't know why, I suppose calculations of the package might be perturbated. It's written in § 4.3, p. 3 of the documentation, which adds this rule may be broken if you know what you're doing. The alternative would be to use a p{…} column, with a width approximately.determined. – Bernard Feb 29 '16 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.