我有一个多列表格,但最后三列的宽度是浮动的。我希望当文本超出表格长度或一行写不完时,它能自动换行。
以下是图片和代码:
其实我从第二行开始就有4列,最后一行也想有3列,但是我忙着做前几行,还没处理最后一行。
代码:
\begin{table}[H] % added on 12172013 for caption and label
\centering
\caption{Table}
\label{tb32}
\begin{tabular}{|p{5em}|c|c|l|}
\rowcolor{c1}
\textcolor{white}{Measure}
& \multicolumn{2}{l|}{\textcolor{white}{Description}}
& \textcolor{white}{NO.} \\
\hline % multicolumn{2}{?|} can be set to 'c' or 'l'.
\arrayrulecolor{white}
\rowcolor{c2}
Mfadfads Number & $N_i$ & $N_i$ is the number of detected fdasfas of
the $i^th$ asdfas. & $M_1$ \\
\hline
\rowcolor{c2}
Mean value of FT of fdsafa & $mean(MFT_i)$ & $MFT_i$ is the Fourier transform of a linear combination of 3 components of dsafasdfadafa \\
\hline
\rowcolor{c2}
DFDF & fdsafdas & asdfasdf \\
\hline
\rowcolor{c2}
DFDF & fdsafdas & asdfasdf \\
\hline
\rowcolor{c1}
DFDF & fdsafdas & asdfasdf \\
\hline
\end{tabular}
\end{table}
答案1
其他答案演示了一种将表格的总宽度限制为给定宽度的方法,例如\linewidth
。如果您不需要这样做,您可以对第三列执行与第一列相同的操作——使其成为一p
列。因此,将表格定义从
\begin{tabular}{|p{5em}|c|c|l|}
到
\begin{tabular}{|p{5em}|c|p{5cm}|l|}
当然,你应该改成5cm
合适的长度。与此相反,其他解决方案的好处是你不必猜测宽度。
根据列中的文本,您可能需要设置文本\raggedright
(或居中,如 karlkoeller 的答案中所示)。然后添加\usepackage{array}
到序言中,并将表定义更改为
\begin{tabular}{|p{5em}|c|>{\raggedright\arraybackslash}p{5cm}|l|}
替换\raggedright
为\centering
使文本居中。
答案2
tabularx
同名的包和环境可以提供帮助。
列类型X
相当于,l
但其内容分布在多行上。
为了获得等价于c
你可以定义一个新的列类型
\newcolumntype{Y}{>{\centering\arraybackslash}X}
例子:
\documentclass{article}
\usepackage{tabularx}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\usepackage[table]{xcolor}
\definecolor{c1}{rgb}{0.30980, 0.50588, 0.73725}
\definecolor{c2}{rgb}{0.82353, 0.87843, 0.92941}
\begin{document}
\begin{table}[h] % added on 12172013 for caption and label
\centering
\caption{Table}
\label{tb32}
\begin{tabularx}{\linewidth}{|p{5em}|c|Y|l|}
\arrayrulecolor{white}
\rowcolor{c1}
\textcolor{white}{Measure}
& \multicolumn{2}{l|}{\textcolor{white}{Description}}
& \textcolor{white}{NO.} \\
\hline % multicolumn{2}{?|} can be set to 'c' or 'l'.
\rowcolor{c2}
Mfadfads Number & $N_i$ & $N_i$ is the number of detected fdasfas of
the $i^th$ asdfas. & $M_1$ \\
\hline
\rowcolor{c2}
Mean value of FT of fdsafa & $mean(MFT_i)$ & $MFT_i$ is the Fourier transform of a linear combination of 3 components of dsafasdfadafa & \\
\hline
\rowcolor{c2}
DFDF & fdsafdas & asdfasdf & \\
\hline
\rowcolor{c2}
DFDF & fdsafdas & asdfasdf & \\
\hline
\rowcolor{c1}
DFDF & fdsafdas & asdfasdf & \\
\hline
\end{tabularx}
\end{table}
\end{document}
输出:
答案3
使用tabularx
:
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tabularx}
\usepackage{ragged2e}
%\renewcommand\tabularxcolumn[1]{>{\Centering}p{#1}}% for a centerd column
\begin{document}
\noindent
\arrayrulecolor{white}
\begin{tabularx}{\linewidth}{|X|c|X|l|}
\rowcolor{blue!80}
\textcolor{white}{Measure}
& \multicolumn{2}{l|}{\textcolor{white}{Description}}
& \textcolor{white}{NO.} \\\hline % multicolumn{2}{?|} can be set to 'c' or 'l'.
\rowcolor{blue!40!white!90}
Mfadfads Number & $N_i$ & $N_i$ is the number of detected fdasfas of
the $i^th$ asdfas. & $M_1$ \\\hline
\rowcolor{blue!40!white!90}
Mean value of FT of fdsafa & $mean(MFT_i)$ & $MFT_i$ is the Fourier transform of a linear combination of 3 components of dsafasdfadafa & \\\hline
\rowcolor{blue!40!white!90}
DFDF & fdsafdas & asdfasdf & \\\hline
\rowcolor{blue!40!white!90}
DFDF & fdsafdas & asdfasdf & \\\hline
\rowcolor{blue!80}
DFDF & fdsafdas & asdfasdf & \\\hline
\end{tabularx}
\end{document}