![在 tabularx 中使用多列或多行很困难](https://linux22.com/image/447465/%E5%9C%A8%20tabularx%20%E4%B8%AD%E4%BD%BF%E7%94%A8%E5%A4%9A%E5%88%97%E6%88%96%E5%A4%9A%E8%A1%8C%E5%BE%88%E5%9B%B0%E9%9A%BE.png)
我刚开始学latex,需要一种像上传图片那样的表格,代码是用Excel2LaTex生成的,试过一些方法,但做不到。
表格左下角几个单元格合并了,命令\tabular
无法让表格自适应调整宽度到整行,如果用\resizebox
,表格高度也会一起增加,很不美观,于是就用 ,\tabularx
但总是出错...虽然知道\tabular
不能简单用 代替\tabularx
,但就是不知道怎么处理...
所以才来求助的,顺便说一句,如果实在难的话,大格子里的大叉子是不能用的,毕竟这个是用Excel画的,那里很简单。
\begin{table}[htbp]
\centering
\caption{Add caption}
\begin{tabular}{|c|c|c|c|c|c|}
\hline
order & number & weight & order & number & weight \\
\hline
1 & 1 & 1 & 1 & 1 & 1 \\
\hline
2 & 2 & 2 & 2 & 2 & 2 \\
\hline
3 & 3 & 3 & 3 & 3 & 3 \\
\hline
... & ... & ... & ... & ... & ... \\
\hline
118 & 118 & 118 & 118 & 118 & 118 \\
\hline
119 & 119 & 119 & 119 & 119 & 119 \\
\hline
120 & 120 & 120 & 120 & 120 & 120 \\
\hline
\multicolumn{3}{|c|}{\multirow{4}[8]{*}{}} & ... & ... & ... \\
\cline{4-6} \multicolumn{3}{|c|}{} & 138 & 138 & 138 \\
\cline{4-6} \multicolumn{3}{|c|}{} & 139 & 139 & 139 \\
\cline{4-6} \multicolumn{3}{|c|}{} & 140 & 140 & 140 \\
\hline
\end{tabular}%
\label{tab:addlabel}%
\end{table}%
我也试过tablesgenerator 网站。它给了我以下代码:
% Please add the following required packages to your document preamble:
% \usepackage{multirow}
\begin{table}[]
\caption{}
\label{tab:my-table}
\begin{tabular}{|ccc|c|c|c|}
\hline
\multicolumn{1}{|c|}{order} & \multicolumn{1}{c|}{number} & weight & order & number & weight \\ \hline
\multicolumn{1}{|c|}{1} & \multicolumn{1}{c|}{1} & 1 & 1 & 1 & 1 \\ \hline
\multicolumn{1}{|c|}{2} & \multicolumn{1}{c|}{2} & 2 & 2 & 2 & 2 \\ \hline
\multicolumn{1}{|c|}{3} & \multicolumn{1}{c|}{3} & 3 & 3 & 3 & 3 \\ \hline
\multicolumn{1}{|c|}{...} & \multicolumn{1}{c|}{...} & ... & ... & ... & ... \\ \hline
\multicolumn{1}{|c|}{118} & \multicolumn{1}{c|}{118} & 118 & 118 & 118 & 118 \\ \hline
\multicolumn{1}{|c|}{119} & \multicolumn{1}{c|}{119} & 119 & 119 & 119 & 119 \\ \hline
\multicolumn{1}{|c|}{120} & \multicolumn{1}{c|}{120} & 120 & 120 & 120 & 120 \\ \hline
\multicolumn{3}{|c|}{\multirow{4}{*}{}} & ... & ... & ... \\ \cline{4-6}
\multicolumn{3}{|c|}{} & 138 & 138 & 138 \\ \cline{4-6}
\multicolumn{3}{|c|}{} & 139 & 139 & 139 \\ \cline{4-6}
\multicolumn{3}{|c|}{} & 140 & 140 & 140 \\ \hline
\end{tabular}
\end{table}
@Mico:我不知道如何在注释中插入大量代码,所以我在这里回答了我自己的问题。这只是一个信息表,我希望它看起来更好。以下代码已集成 Mico 的代码。
\documentclass{article}
\usepackage[english]{babel}
\usepackage[letterpaper,top=2cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage[colorlinks=true, allcolors=blue]{hyperref}
\usepackage{gensymb}
\usepackage{multirow,tabularx,ragged2e,booktabs,longtable}
\newcolumntype{C}{>{\Centering}X} % centered version of X col. type
\usepackage[skip=0.333\baselineskip]{caption}
\begin{document}
\begin{table}[htbp]
\caption{Fast Hankel Transform} \label{FHT}
\begin{tabularx}{\linewidth}{|*{6}{C|}}
\hline
Order & Abscissa of $J_0$ & Weight of $J_0$ & Order & Abscissa of $J_1$ & Weight of $J_1$ \\\hline
1 & $-19.32$ & $9.63\times10^{-7}$ & 1 & $-18.21$ & $-6.77\times10^{-14}$ \\\hline
2 & $-19.11$ & $-5.02\times10^{-6}$ & 2 & $-18.01$ & $3.40\times10^{-13}$ \\\hline
3 & $-18.90$ & $1.25\times10^{-5}$ & 3 & $-17.81$ & $-7.43\times10^{-13}$ \\\hline
$\vdots$ & $\vdots$ & $\vdots$ & $\vdots$ & $\vdots$ & $\vdots$ \\\hline
118 & $5.04$ & $6.23\times10^{-6}$ & 118 & $5.49$ & $7.33\times10^{-6}$ \\\hline
119 & $5.25$ & $-1.12\times10^{-6}$ & 119 & $5.69$ & $-3.76\times10^{-6}$ \\\hline
120 & $5.46$ & $1.04\times10^{-7}$ & 120 & $5.89$ & $1.86\times10^{-6}$ \\\hline
\multicolumn{3}{|c|} {\multirow{4}{*}{}} & $\vdots$ & $\vdots$ & $\vdots$ \\
\cline{4-6} \multicolumn{3}{|c|}{} & 138 & $9.54$ & $-1.36\times10^{-9}$ \\
\cline{4-6} \multicolumn{3}{|c|}{} & 139 & $9.74$ & $3.53\times10^{-10}$ \\
\cline{4-6} \multicolumn{3}{|c|}{} & 140 & $9.94$ & $-4.54\times10^{-11}$ \\ \hline
\end{tabularx}
\end{table}
\end{document}
答案1
这里有两种可能的解决方案。两者都占据了文本块的整个宽度。第一种使用tabularx
环境(带有居中版本的X
列类型),第二种使用tabular*
-inspiredlongtable
环境。
这两个表格之间的主要区别在于:第二个表格 (a) 删除了所有垂直线和大部分水平线,以使表格看起来更加开放、更具吸引力;(b) 允许根据需要分页,这可能很有用,因为实际表格可能有多达 140 行。
\documentclass{article}
\usepackage{tabularx,ragged2e,booktabs,longtable}
\newcolumntype{C}{>{\Centering}X} % centered version of 'X' col. type
\usepackage[skip=0.333\baselineskip]{caption}
\begin{document}
\begin{table}[htbp]
\caption{A ``prison window''-look table} \label{tab:addlabel1}
\begin{tabularx}{\textwidth}{|*{6}{C|}}
\hline
order & number & weight & order & number & weight \\
\hline
1 & 1 & 1 & 1 & 1 & 1 \\
\hline
2 & 2 & 2 & 2 & 2 & 2 \\
\hline
3 & 3 & 3 & 3 & 3 & 3 \\
\hline
\dots & \dots & \dots & \dots & \dots & \dots \\
\hline
118 & 118 & 118 & 118 & 118 & 118 \\
\hline
119 & 119 & 119 & 119 & 119 & 119 \\
\hline
120 & 120 & 120 & 120 & 120 & 120 \\
\hline
&&& \dots & \dots & \dots \\ \cline{4-6}
&&& 138 & 138 & 138 \\ \cline{4-6}
&&& 139 & 139 & 139 \\ \cline{4-6}
&&& 140 & 140 & 140 \\
\hline
\end{tabularx}
\end{table}
\begingroup % localize scope of next 4 instructions
\setlength\tabcolsep{0pt}
\setlength\LTcapwidth{\textwidth}
\setlength\LTleft{0pt} % see p. 7 of user guide of longtable package
\setlength\LTright{0pt}
\begin{longtable}{@{\extracolsep{\fill}} *{6}{c} }
%% information about headers and footers
\caption{A table with a much more open ``look''} \label{tab:addlabel2}\\
\toprule
order & number & weight & order & number & weight \\
\midrule
\endfirsthead
\multicolumn{6}{l}{\tablename\ \thetable, cont'd}\\
\addlinespace
\toprule
order & number & weight & order & number & weight \\
\midrule
\endhead
\midrule
\multicolumn{6}{r@{}}{\footnotesize \em cont'd on following page}\\
\endfoot
\bottomrule
\endlastfoot
%% body of longtable
1 & 1 & 1 & 1 & 1 & 1 \\
2 & 2 & 2 & 2 & 2 & 2 \\
3 & 3 & 3 & 3 & 3 & 3 \\
$\vdots$ & $\vdots$ & $\vdots$ & $\vdots$ & $\vdots$ & $\vdots$ \\
118 & 118 & 118 & 118 & 118 & 118 \\
119 & 119 & 119 & 119 & 119 & 119 \\
120 & 120 & 120 & 120 & 120 & 120 \\
&&& $\vdots$ & $\vdots$ & $\vdots$ \\
&&& 138 & 138 & 138 \\
&&& 139 & 139 & 139 \\
&&& 140 & 140 & 140 \\
\end{longtable}
\endgroup
\end{document}
附录合并 OP 提供的附加信息:这是tabularx
基于 的解决方案的变体,其(a)允许第 1 列和第 4 列比第 2、3、5 和 6 列窄,并且(b)允许后四列中的数字在小数点标记上对齐,借助包S
提供的列类型siunitx
。
\documentclass{article}
\usepackage[english]{babel}
\usepackage[letterpaper,vmargin=2cm,hmargin=3cm,
marginparwidth=1.75cm]{geometry}
\usepackage{amsmath,graphicx,gensymb}
\usepackage[colorlinks=true, allcolors=blue]{hyperref}
\usepackage{tabularx,ragged2e}
\newcolumntype{C}{>{\Centering}X} % centered version of X col. type
\usepackage[skip=0.333\baselineskip]{caption}
\usepackage{siunitx}
\newcommand\mC[1]{\multicolumn{1}{C|}{#1}} % handy shortcut macro
\begin{document}
\begin{table}[htbp]
\setlength\extrarowheight{2pt} % for a slightly more open "look"
\caption{Fast Hankel Transform} \label{FHT}
\begin{tabularx}{\linewidth}{| *{2}{ c | S[table-format=-2.2] |S[table-format=-1.2e-2]| } }
\hline
Order & \mC{Abscissa of $J_0$} & \mC{Weight of $J_0$} &
Order & \mC{Abscissa of $J_1$} & \mC{Weight of $J_1$} \\
\hline
1 & -19.32 & 9.63e-7 & 1 & -18.21 & -6.77e-14 \\ \hline
2 & -19.11 & -5.02e-6 & 2 & -18.01 & 3.40e-13 \\ \hline
3 & -18.90 & 1.25e-5 & 3 & -17.81 & -7.43e-13 \\ \hline
{$\vdots$} & {$\vdots$} & {$\vdots$} & {$\vdots$} & {$\vdots$} & {$\vdots$} \\ \hline
118 & 5.04 & 6.23e-6 & 118 & 5.49 & 7.33e-6 \\ \hline
119 & 5.25 & -1.12e-6 & 119 & 5.69 & -3.76e-6 \\ \hline
120 & 5.46 & 1.04e-7 & 120 & 5.89 & 1.86e-6 \\ \hline
\multicolumn{3}{|c|}{} & {$\vdots$} & {$\vdots$} & {$\vdots$} \\ \cline{4-6}
\multicolumn{3}{|c|}{} & 138 & 9.54 & -1.36e-9 \\ \cline{4-6}
\multicolumn{3}{|c|}{} & 139 & 9.74 & 3.53e-10 \\ \cline{4-6}
\multicolumn{3}{|c|}{} & 140 & 9.94 & -4.54e-11 \\ \hline
\end{tabularx}
\end{table}
\end{document}