50

I need to create similar table using LaTeX:

table

I have tried to use \tabular and \multicolumn, but every time I get something wrong.

I want:

  • Fixed width columns and fixed height rows;
  • Centered cells (vertically and horizontally)
  • Borders around each cell

Could someone help me?

Here is pure HTML markup:

<table border="1" cellpadding="0" cellspacing="0">
    <tr height="50">
        <td align="center" width="150" rowspan="2">State of Health</td>
        <td align="center" width="300" colspan="2">Fasting Value</td>
        <td align="center" width="150">After Eating</td>
    </tr>
    <tr height="50">
        <td align="center" width="150">Minimum</td>
        <td align="center" width="150">Maximum</td>
        <td align="center" width="150">2 hours after eating</td>
    </tr>
    <tr height="50">
        <td align="center" width="150">Healthy</td>
        <td align="center" width="150">70</td>
        <td align="center" width="150">100</td>
        <td align="center" width="150">Less than 140</td>
    </tr>
    <tr height="50">
        <td align="center" width="150">Pre-Diabetes</td>
        <td align="center" width="150">101</td>
        <td align="center" width="150">126</td>
        <td align="center" width="150">140 to 200</td>
    </tr>
    <tr height="50">
        <td align="center" width="150">Diabetes</td>
        <td align="center" width="150">More than 126</td>
        <td align="center" width="150">N/A</td>
        <td align="center" width="150">More than 200</td>
    </tr>
</table>
1

5 Answers 5

59

You could use the multirow package to organize the cell in the north-west corner of the table, and the tabularx package to automatically generate four columns of equal width. In the MWE below, the \newcolumntype instruction sets up a new column type, called "Y", that centers its contents. Adjust the value of the \arraystretch macro to get the amount of vertical stretching to your liking.

enter image description here

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{multirow,tabularx}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\renewcommand{\arraystretch}{2}
\begin{document}\pagestyle{empty}
\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}
\end{document}
2
  • @Mico, would it possible to adjust the width of each cell in the table if I don't want the equal length Commented Jul 19, 2014 at 21:06
  • @user1935724 - Just saw your question. I've posted a new answer to your new posting to explain how go about modifying the relative widths of the columns of type X (or Y).
    – Mico
    Commented Jul 20, 2014 at 5:47
8

The http://www.tablesgenerator.com/ site is helpful - it also knows about multicolumn.

More a comment than an answer, since this is likely to be aiming a clinical or scientific audience, tables should not have lines with a few exceptions. Referenced on Beautiful table samples I was very happy with https://www.inf.ethz.ch/personal/markusp/teaching/guides/guide-tables.pdf .

6

For comparison, this is how you can typeset the same table in ConTeXt. (Notice the similarity between the HTML markup (had you used CSS) and the TeX markup).

\setuppapersize[A4,landscape]

\startsetups table:layout
  \setupTABLE
      [
        width=150pt,           % Fixed column width
        height=2\lineheight,   % Fixed row height
        align={middle,lohi},   % Middle aligned cells
        frame=on,              % Border around cells (default)
        offset=none,           % Equivalent to cellpadding=0
      ]
\stopsetups

\starttext

\bTABLE[setups=table:layout]
  \bTR
      \bTD[ny=2] State of Health \eTD
      \bTD[nx=2] Fasting Value   \eTD
      \bTD After Eating          \eTD
  \eTR
  \bTR
      \bTD Minimum \eTD
      \bTD Maximum \eTD
      \bTD 2 hours after eating \eTD
  \eTR

  \bTR
    \bTD Healthy       \eTD
    \bTD 70            \eTD
    \bTD 100           \eTD
    \bTD Less than 140 \eTD
  \eTR

  \bTR
    \bTD Pre-Diabetes \eTD
    \bTD 101          \eTD
    \bTD 126          \eTD
    \bTD 140 to 200   \eTD
  \eTR

  \bTR
    \bTD Diabetes      \eTD
    \bTD More than 126 \eTD
    \bTD N/A           \eTD
    \bTD More than 200 \eTD
  \eTR
\eTABLE
\stoptext

enter image description here

2
  • 1
    @aditya context is soooo powerful that if i were 35 years younger and starting with tex, i can imagine it attracting me more than latex. unfortunately, it wasn't on the scene, back then (afaik), and i had hardly heard of it when we (uk tug) invited hans to give us a talk. ah, the accidents of history... Commented Nov 27, 2012 at 11:46
  • @wasteofspace: Well, ConTeXt started around early 90's, and by then LaTeX had already established itself as the dominant standard. But, I think that the fact that ConTeXt came to the scene later than LaTeX--when computers had become more powerful--is part of the reason that ConTeXt could provide a flexible key-value driven syntax. Had it started a few years earlier, it would have had to make the same compromises for speed vs flexibility that LaTeX made.
    – Aditya
    Commented Nov 27, 2012 at 16:47
3

A solution with tabularray package:

\documentclass{article}
\usepackage[margin=2mm]{geometry}
\usepackage{tabularray}
\begin{document}

\noindent
\begin{tblr}{
  hlines, vlines, % borders around each cell
  rowsep=0pt, colsep = 0pt,
  rows = {50pt}, columns = {150pt}, % fixed width columns and fixed height rows
  cells = {c,m}, % centered cells (vertically and horizontally)
  cell{1}{1} = {r=2}{}, % multirow
  cell{1}{2} = {c=2}{}, % multicolumn
}
State of Health & Fasting Value &         & After Eating         \\
                & Minimum       & Maximum & 2 hours after eating \\
Healthy         & 70            & 100     & Less than 140        \\
Pre-Diabetes    & 101           & 126     & 140 to 200           \\
Diabetes        & More than 126 & N/A     & More than 200        \\
\end{tblr}

\end{document}

enter image description here

0

A solution with {NiceTabular} of nicematrix.

\documentclass{article}
\usepackage[margin=2mm]{geometry}
\usepackage{nicematrix}

\begin{document}

\noindent
\begin{NiceTabular}[hvlines]{>{\rule[-6mm]{0pt}{15mm}}X[c]X[c]X[c]X[c]}
\Block{2-1}{State of Health} 
                & \Block{1-2}{Fasting Value} 
                                &         & After Eating         \\
                & Minimum       & Maximum & 2 hours after eating \\
Healthy         & 70            & 100     & Less than 140        \\
Pre-Diabetes    & 101           & 126     & 140 to 200           \\
Diabetes        & More than 126 & N/A     & More than 200        \\
\end{NiceTabular}

\end{document}

Output of the above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .