4

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?

  • Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – cfr Jul 19 '14 at 23:57
  • Did you do any research yourself at all? This is pretty common and there must be (at a conservative estimate) tens of ways of doing it just going by questions on this site. Indeed, most 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. – cfr Jul 19 '14 at 23:58
  • @of course I did the research this is how I find the previous relevant question the at least 10 ways you suggested is going to work with multicolumn or row that I required? If so, please show me – user1935724 Jul 20 '14 at 0:47
  • @cfr, also the whole reason I am using tabularx is to be compatible with the answer I find on the post, oh come on thanks very much to tell me I could have used the simplest tabular instead of tabularx but is it going to work with the customized column type Y, can you please at least understand what I am asking before making the comment. – user1935724 Jul 20 '14 at 0:57
  • 1
    Without an example or any clue what you are trying to do, how could anybody help? Now you say the problem is that you need the customised column type Y and now somebody has offered an answer. But you did not mention that (or any other criteria beyond needing variable widths) in your question. As far as I know, few people around here are mind readers. If you don't tell us, we are just whistling in the dark and likely to waste your time and ours on solutions which won't work for you. This is why all questions should include an MWE demonstrating the issue. – cfr Jul 20 '14 at 1:14
2

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}
  • 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 6:02
2

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}

enter image description here

  • I have it working by incorporating the \setlength{\tabcolsep}{1.2pt} to your answer, thanks – user1935724 Jul 20 '14 at 2:15

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.