表格行跨度和列跨度

表格行跨度和列跨度

我需要使用 LaTeX 创建类似的表格:

桌子

我曾尝试\tabular使用\multicolumn,但每次都会出错。

我想:

  • 固定宽度的列和固定高度的行;
  • 居中单元格(垂直和水平)
  • 每个单元格周围的边框

有人能帮助我吗?

以下是纯 HTML 标记:

<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

您可以使用该multirow包来组织表格西北角的单元格,并使用tabularx包自动生成四列等宽的单元格。在下面的 MWE 中,\newcolumntype指令设置了一种新的列类型,称为“Y”,使其内容居中。调整宏的值\arraystretch以获得您喜欢的垂直拉伸量。

在此处输入图片描述

\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

http://www.tablesgenerator.com/网站很有帮助——它也了解多列。

更多的是评论而不是答案,因为这可能是针对临床或科学受众,表格不应该有线条,只有少数例外。参考漂亮的表格样本我很高兴https://www.inf.ethz.ch/personal/markusp/teaching/guides/guide-tables.pdf

答案3

为了进行比较,您可以这样在 ConTeXt 中排版同一个表格。(请注意 HTML 标记(如果您使用 CSS)和 TeX 标记之间的相似性)。

\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

在此处输入图片描述

答案4

{NiceTabular}的一个解决方案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}

上述代码的输出

相关内容