我创建了一个表格https://www.tablesgenerator.com/latex_tables
我尝试用限制列宽
|l| p{1cm} |p{2cm} |p{1cm} |p{1cm} |p{2cm} |p{1cm} |p{1cm}|
但似乎没有任何效果。
生成的表
\begin{table}[]
\begin{tabular}{llllllll}
\hline
\multicolumn{1}{|l|}{} & \multicolumn{1}{l|}{Compute} & \multicolumn{1}{l|}{Pay As You Go} & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{Storage} & \multicolumn{1}{l|}{Pay As You Go} & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{Trail} \\ \cline{2-3} \cline{5-6} \cline{8-8}
\multicolumn{1}{|l|}{\multirow{-2}{*}{Provider}} & \multicolumn{1}{l|}{Terminology} & \multicolumn{1}{l|}{Unit} & \multicolumn{1}{l|}{\multirow{-2}{*}{Other Plans*}} & \multicolumn{1}{l|}{Terminology} & \multicolumn{1}{l|}{Unit} & \multicolumn{1}{l|}{\multirow{-2}{*}{Other Plans*}} & \multicolumn{1}{l|}{Period or Value} \\ \hline
\multicolumn{1}{|l|}{Windows Azure} & \multicolumn{1}{l|}{\cellcolor[HTML]{9AFF99}Virtual Server} & \multicolumn{1}{l|}{\cellcolor[HTML]{9AFF99}/hr} & \multicolumn{1}{l|}{\cellcolor[HTML]{9AFF99}1} & \multicolumn{1}{l|}{\cellcolor[HTML]{9AFF99}Azure Storage} & \multicolumn{1}{l|}{\cellcolor[HTML]{9AFF99}/GB month} & \multicolumn{1}{l|}{\cellcolor[HTML]{9AFF99}1} & \multicolumn{1}{l|}{\cellcolor[HTML]{9AFF99}90 day} \\ \hline
\end{tabular}
\end{table}
删除后\multicolumn
,我收到错误
\multirow{-2}{p{1cm}}{Other Plans*}
main.tex, line 37
Missing number, treated as zero.
<to be read again>
p
l.37 ... & \multirow{-2}{p{1 cm}}{Other Plans*}
& Termin...
A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)
! Illegal unit of measure (pt inserted).
<to be read again>
p
l.37 ... & \multirow{-2}{p{1 cm}}{Other Plans*}
& Termin...
Dimensions can be in units of em, ex, in, pt, pc,
cm, mm, dd, cc, nd, nc, bp, or sp; but yours is a new one!
请参阅完整示例的第 2 页 https://www.overleaf.com/read/ywhjzprxmrgj
答案1
有几种可能性不会生成overfull \hbox
标准article
文档类:
\documentclass{article}
\usepackage{array}
\renewcommand{\arraystretch}{2}
\usepackage{multirow}
\usepackage[table]{xcolor}
\usepackage{makecell}
\begin{document}
\begin{table}\scriptsize\centering
\begin{tabular}{llllllll}
\hline
& Compute & \makecell[l]{Pay As\\ You Go} & & Storage & \makecell[l]{Pay As\\ You Go} & & Trail \\
\cline{2-3} \cline{5-6} \cline{8-8}
\multirow{-2}{*}{Provider} & Terminology & Unit & \multirow{-2}{*}{\makecell[l]{Other\\ Plans*}} & Terminology & Unit & \multirow{-2}{*}{\makecell[l]{Other\\ Plans*}} & \makecell[l]{Period\\ or Value} \\
\hline
\makecell[l]{Windows\\ Azure} & \cellcolor[HTML]{9AFF99}{\renewcommand{\arraystretch}{1}\begin{tabular}{@{}l@{}}Virtual\\ Server\end{tabular}} & \cellcolor[HTML]{9AFF99}/hr & \cellcolor[HTML]{9AFF99}1 & \cellcolor[HTML]{9AFF99}{\renewcommand{\arraystretch}{1}\begin{tabular}{@{}l@{}}Azure\\Storage\end{tabular}} & \cellcolor[HTML]{9AFF99}{\renewcommand{\arraystretch}{1}\begin{tabular}{@{}l@{}}/GB\\month\end{tabular}} & \cellcolor[HTML]{9AFF99}1 & \cellcolor[HTML]{9AFF99}90 day \\
\hline
\end{tabular}
\end{table}
\begin{table}\tiny\centering
\begin{tabular}{|l|l|l|l|l|l|l|l|}
\hline
& Compute & \makecell[l]{Pay As\\ You Go} & & Storage & \makecell[l]{Pay As\\ You Go} & & Trail \\
\cline{2-3} \cline{5-6} \cline{8-8}
\multirow{-2}{*}{Provider} & Terminology & Unit & \multirow{-2}{*}{\makecell[l]{Other\\ Plans*}} & Terminology & Unit & \multirow{-2}{*}{\makecell[l]{Other\\ Plans*}} & \makecell[l]{Period\\ or Value} \\
\hline
\makecell[l]{Windows\\ Azure} & \cellcolor[HTML]{9AFF99}{\renewcommand{\arraystretch}{1}\begin{tabular}{@{}l@{}}Virtual\\ Server\end{tabular}} & \cellcolor[HTML]{9AFF99}/hr & \cellcolor[HTML]{9AFF99}1 & \cellcolor[HTML]{9AFF99}{\renewcommand{\arraystretch}{1}\begin{tabular}{@{}l@{}}Azure\\Storage\end{tabular}} & \cellcolor[HTML]{9AFF99}{\renewcommand{\arraystretch}{1}\begin{tabular}{@{}l@{}}/GB\\month\end{tabular}} & \cellcolor[HTML]{9AFF99}1 & \cellcolor[HTML]{9AFF99}90 day \\
\hline
\end{tabular}
\end{table}
\end{document}
请注意,对于彩色单元格,我使用了适当的tabular
而不是\makecell
。
我更喜欢第一个,没有垂直规则:
答案2
- 由于您没有提供任何有关文档页面布局的信息,因此无法看到表格在右侧文本区域边框上溢出的程度
tabular*
将表格宽度限制为文本宽度是最好的,使用tabularx
或其他类似的表格环境,并为它们规定所需的表格宽度- 在这种环境下,您需要选择适当的列类型,例如
p{<width>}
或X
- 不要
makecell
与彩色表格一起使用。你可能会遇到意想不到的麻烦
看看以下建议是否能解决您的问题:
\documentclass{article}
\usepackage{geometry}
\usepackage[table]{xcolor}
\usepackage{cellspace, multirow, tabularx}
\setlength\cellspacetoplimit{4pt}
\setlength\cellspacebottomlimit{4pt}
\renewcommand\tabularxcolumn[1]{m{#1}}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\addparagraphcolumntypes{L}
%-------------------------------------- only for show page layout
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.25pt}
\renewcommand*\ShowFrameColor{\color{red}}
\begin{document}
\begin{table}[htb]
\setlength\extrarowheight{2pt}
\begin{tabularx}{\linewidth}{L
*{2}{>{\hsize=1.2\hsize}L L >{\hsize=0.8\hsize}L}
L}
\hline
& Compute
& Pay As You Go
& & Storage
& Pay As You Go
& & Trail \\
\cline{2-3} \cline{5-6} \cline{8-8}
\multirow{-2.8}{=}{Provider}
& Terminology
& Unit
& \multirow{-2.8}{=}{Other Plans*}
& Terminology
& Unit
& \multirow{-2.8}{=}{Other\\ Plans*}
& Period or Value \\
\hline
\rowcolor{green!30}
\cellcolor{white}{Windows Azure}
& Virtual Server
& /hr
& 1 & Azure Storage
& /GB mont
& 1 & 90 day \\
\hline
\end{tabularx}
\end{table}
\end{document}
它给:
(红线表示页面布局)
如果您的文本区域较窄,请查看减小字体大小是否对您有帮助。
答案3
我从原始站点找到了一个选项,可以将表格缩放到文本宽度。
我最终这样做了:
% required packages to your document preamble:
\usepackage{graphicx}
\resizebox{\textwidth}{!}{
\begin{tabular}
% original table
\end{tabular}
}