[Tex/LaTex] Tabularx and \hsize column tuning

tabularx

In this answer : Tabularx table with multirows and multicolumns, I have discovered the \hsize possibility to tune X column width of tabularx table.

The documentation says :

Make sure that the sum of the widths of all the X columns is unchanged.

But the answer says :

\begin{tabularx}{\textwidth}{|>{\hsize=0.5\hsize}X|
                    *{3}{ >{\hsize=1.5\hsize}X|}
                        }

that makes a sum of 5 for 4 columns, and it leeds to problems if one not fill columns enought.

Questions :

  • Why it works in the example and not in the following example ?

  • What is the correct method to calculate the \hsizes ?

enter image description here

\documentclass{article}
\usepackage{tabularx}
\begin{document}
\begin{table}

\begin{tabularx}{\linewidth}{|>{\hsize=0.5\hsize}X|
                        *{3}{ >{\hsize=1.5\hsize}X|}
                            }\hline
1&2&3&4\\\hline
\end{tabularx}

\bigskip

% My proposition if correctly understood the manuel
\begin{tabularx}{\linewidth}{|>{\hsize=0.5\hsize}X|
                        *{3}{ >{\hsize=1.16667\hsize}X|}
                            }\hline
1&2&3&4\\\hline
\end{tabularx}
\end{table}
\end{document}

Best Answer

It seems you want three equal-width columns (let's denote their width by x), plus another column that's one-third as wide as the other three; denote its width by y. The combined width of the four columns should equal \linewidth.

The computation is that of barycentric coordinates: you have to solve the linear system:

3x + y = 4 % 4: number of columns of type "X"
x = 3y

It is easy to find this yields y=0.4 and x=1.2, whence the code:

\documentclass{article}
\usepackage{tabularx}
%% define a custom col. type to simplify expressing relative col. widths
\newcolumntype{H}[1]{>{\hsize=#1\hsize\arraybackslash}X}

\begin{document}

\begin{table}
\begin{tabularx}{\linewidth}{| H{0.4} | *{3}{H{1.2}|} } % 0.4+3*1.2=4
\hline
1&2&3&4\\
\hline
\end{tabularx}
\end{table}

\end{document} 

enter image description here

Related Question