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?
2 Answers
One option will be to use tabulary. It provides LCRJ column types and you can control the minimum and maximum column widths be setting \tymin and \tymax like
\tymin=20pt
\tymax=\maxdimen
These can be set using \setlength also.
Code (using the linked answer):
\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{multirow,tabulary}
\newcolumntype{Y}{>{\centering\arraybackslash}J}
\renewcommand{\arraystretch}{2}
\tymin=20pt
\tymax=\maxdimen
\begin{document}\pagestyle{empty}
\begin{tabulary}{\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{tabulary}
\end{document}

-
I have it working by incorporating the \setlength{\tabcolsep}{1.2pt} to your answer, thanks – user1935724 Jul 20 '14 at 02:15
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 by 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 environment contains only a single column of type X, its width is fully determined as a residual, viz., as the difference between the overall or target width and the sum of the widths of the other columns and intercolumn spaces.)
The following example shows how this idea may be applied to create a table in which the usable widths of the first and fourth columns are 50% larger than the usable widths of the two middle columns. Observe that 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, type Y, where Y is a version of X that has been modified to center-set the cell contents).

\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}
- 506,678
-
Thanks a lot, I think this is a more elegant way cos it actually allows you to set the width for each of the column, I will have a try! – user1935724 Jul 21 '14 at 06:02
\documentclass{...}and ending with\end{document}. – cfr Jul 19 '14 at 23:57tabular-type environments have variable column width *by default*. So if yours are fixed, you are doing something to fix 'em.... the tabularx package to automatically generate four columns of equal width.... Please at least read the answers in threads you link to. If you don't want this part of the solution, don't use it. – cfr Jul 19 '14 at 23:58I just want to create a table with multiple rows and columns...withvariable... column width. That was all you told us. For that,tabularis the perfect solution. – cfr Jul 20 '14 at 01:16