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
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
).
\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}
-
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! Jul 21, 2014 at 6:02
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 Jul 20, 2014 at 2:15
\documentclass{...}
and ending with\end{document}
.tabular
-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.