[Tex/LaTex] Table with multiple rows and columns

tablestabularx

I just want to create a table with multiple rows and columns, here is a very suitable solution:Table rowspan and colspan, but can anyone help me to get a variable column width rather than the equal column width?

Best Answer

Assuming you want to stay with the tabularx table type that was used in my answer to the posting Table rowspan and colspan, you may achieve your objective using the methods set forth in section 4.3 of the user guide of the tabularx package. The method described there works by adjusting the relative widths of the columns of type X. (Naturally, if your tabularx table contains only a single column of type X, its width is fully determined as a residual, viz., as the difference between the overall text width and the sum of the widths of the other columns and intercolumn spaces.)

The following example shows how to this may be done to create a table in which the first and fourth columns are 50% wider than the two middle columns. Note how the four \hsize values -- 1.2, 0.8, 0.8, and 1.2 -- sum to 4, which is the number of columns of type X (or, to be even more precise, Y, where Y is a modified form of X).

enter image description here

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{multirow,tabularx}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\renewcommand{\arraystretch}{2}
\begin{document}

\emph{Original form: All columns are equally wide.}

\noindent
\begin{tabularx}{\textwidth}{|*{4}{Y|}}
\hline
\multirow{2}{*}{State of Health} 
&\multicolumn{2}{c|}{Fasting Value}&After Eating\\
\cline{2-4}
&Minimum       &Maximum &2 hours after eating\\
\hline
Healthy      &70            &100     &Less than 140\\
\hline
Pre-Diabetes &101           &126     &140 to 200\\
\hline
Diabetes     &More than 126 &N/A     &More than 200\\
\hline
\end{tabularx}

\bigskip
\emph{Modified form: Columns 1 and 4 are 50\% wider than columns 2 and 3.}

\smallskip\noindent
\begin{tabularx}{\textwidth}{|
 >{\hsize=1.2\hsize}Y| 
 >{\hsize=0.8\hsize}Y|
 >{\hsize=0.8\hsize}Y|
 >{\hsize=1.2\hsize}Y|}
\hline
\multirow{2}{*}{State of Health} 
&\multicolumn{2}{c|}{Fasting Value}&After Eating\\
\cline{2-4}
&Minimum       &Maximum &2 hours after eating\\
\hline
Healthy      &70            &100     &Less than 140\\
\hline
Pre-Diabetes &101           &126     &140 to 200\\
\hline
Diabetes     &More than 126 &N/A     &More than 200\\
\hline
\end{tabularx}
\end{document}
Related Question