在乳膠中建立具有不同行長度的特殊表

在乳膠中建立具有不同行長度的特殊表

我怎樣才能在乳膠中得到如下圖所示的表格? 在此輸入影像描述

答案1

一個可能的解決方案。我建議不使用垂直線,而是booktabs包裹。對於水平對齊,20您可以利用multirow包,而對於垂直對齊,您可以透過以下方式計​​算第一列的最大條目的寬度calc包裹。

在此輸入影像描述

\documentclass{article}
\usepackage{booktabs,calc,multirow}

\newlength\wdx% for vertical alignment
\setlength{\wdx}{\widthof{66}}

\begin{document}

\begin{table}
\centering
\addtolength{\tabcolsep}{10pt}% increase distance between cells 
\begin{tabular}{cccccc}
\toprule
\textsf{x} & \multicolumn{5}{c}{\textsf{y}}\\
\cmidrule(l{\wdx}r{\wdx}){1-1}\cmidrule(l){2-6}
10 & 22 & 33 \\
\multirow{2}[2]{\wdx}{20} & 45 & 34 & 88 & 77 & 80 \\
 & 34 & 67\\
66 & 12 & 10 & 45 & 66\\ 
\bottomrule
\end{tabular}
\end{table}

\end{document}

答案2

如果您必須在表格中使用垂直線,我建議您也插入“struts”,以便在 繪製的水平線上方和下方獲得更好的垂直間距\hline。但總的來說,我發現建立表格更好沒有垂直規則並使用該booktabs包獲取由\toprule\midrule等繪製的間隔良好的水平線。

若要將第二個條目在第一列垂直居中,請使用\multirow指令。

您也可以自動執行使第一列始終以粗體顯示的工作。

最後,如果您想使用該tabularx包,比如說因為您想要實現預先指定的表格寬度,並且如果您希望內容居中(這似乎是您發布的屏幕截圖中的情況),您還需要定義包X提供的列類型的特殊“居中”版本tabularx

下面的第一個表使用tabular環境,第二個表使用tabularx總寬度設定為 的環境0.7\textwidth。第二個表格也採用無襯線字體。

在此輸入影像描述

\documentclass[10pt,a4paper]{article}
\usepackage{multirow,array}
% define "struts" as suggested by Claudio Beccari in
%    a piece in TeX and TUG News, Vol. 2, 1993.
\newcommand\T{\rule{0pt}{2.6ex}}       % = `top' strut
\newcommand\B{\rule[-0.9ex]{0pt}{0pt}} % = `bottom' strut
\newcommand\TB{\T\B} % top-and-bottom strut 

\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\begin{document}

\begin{tabular}{>{\bfseries}c|ccccc}
x & \multicolumn{5}{c}{\bfseries y\B}\\
\hline
10 & 22 & 33\TB\\
\hline
\multirow{2}{*}{20} & 45 & 34 & 88 & 77 & 80\T\\
 & 34 & 67\B \\
\hline
66 & 12 & 10 & 45 & 66\T\\
\end{tabular}

\bigskip

\textsf{ % switch to sans-serif for the second table
\begin{tabularx}{0.7\textwidth}{>{\bfseries}C|CCCCC}
x & \multicolumn{5}{c}{\bfseries y\B}\\
\hline
10 & 22 & 33\TB\\
\hline
\multirow{2}{*}{20} & 45 & 34 & 88 & 77 & 80\T\\
 & 34 & 67\B \\
\hline
66 & 12 & 10 & 45 & 66\T\\
\end{tabularx}}
\end{document}

答案3

除了 之外array,還僅使用makecell套件的解決方案。一個優點是可以使用該\makecell*命令來獲得不太緊的行(它添加了\jot 對稱地與單元格的頂部和底部不同\renewcommand{\arraystretch}{…}。另一個是改變線條粗細的可能性(如booktabs,但保留使用垂直線的可能性)。

    \documentclass{article}

    \usepackage[utf8]{inputenc}
    \usepackage{array, makecell}

    \begin{document}

    \begin{table}
    \centering\sffamily
    \addtolength{\tabcolsep}{10pt}% increase distance between cells
    \begin{tabular}{c!{\vline width1pt}*{5}{c}}
    \makecell*{x} & \multicolumn{5}{c}{y}\\
    \Xhline{1pt}
    \makecell*{10} & 22 & 33 \\
    \hline
    \makecell{20\\[-2.5ex]}& \makecell*[tc]{45\\34} & \makecell[tc]{34\\67} & 88 & 77 & 80 \\
    \hline
    \makecell*{66} & 12 & 10 & 45 & 66
    \end{tabular}
    \end{table}

    \end{document} 

在此輸入影像描述

相關內容