我有以下代码:
\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[portuguese]{babel}
\usepackage{color}
\usepackage[table]{xcolor}
\usepackage{makecell}
\usepackage{array}
\usepackage{tabularx}
\usepackage{multirow,multicol}
\usepackage{booktabs}
%\usepackage{adjustbox}
\usepackage{scalerel,amssymb}
\setlength\parindent{0pt}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\begin{document}
\begin{table}[htbp]\centering
\begin{tabularx}{\textwidth}{|C|*{12}{>{\centering\arraybackslash}m{0.1cm}|}}
\hline
& \multicolumn{12}{c|}{Months} \\ \cline{2-13}
& 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 &10 &11 &12 \\ \hline
Activity 1 & \cellcolor{gray} & \cellcolor{gray} & \cellcolor{gray} & & & & & & & & & \\ \hline
Activity 2 & & \cellcolor{gray} & \cellcolor{gray} & & & & & & & & & \\ \hline
Activity 3 & & & \cellcolor{gray} & \cellcolor{gray} & \cellcolor{gray} & & & & & & & \\ \hline
Activity 4 & & & & & \cellcolor{gray} & \cellcolor{gray} & & & & & & \\ \hline
Activity 4 & & & & & & & \cellcolor{gray} & \cellcolor{gray} & \cellcolor{gray} & & & \\ \hline
Activity 4 & & & & & & & & & \cellcolor{gray} & \cellcolor{gray} & \cellcolor{gray} & \cellcolor{gray} \\ \hline
\end{tabularx}
\caption{Cronogram}
\end{table}
\end{document}
它返回一条警告,提示“Overfull \hbox...”。有什么办法可以修复这个问题吗?
另外,有没有办法用“\cellcolor{gray}”替代命令?谢谢!
答案1
你似乎认为如果将列类型指定为m{1mm}
,则列的宽度将为1mm
。事实上,这只是它的可用的宽度;它的满的宽度为1mm+2\tabcolsep
;长度参数\tabcolsep
(默认值6pt
:)控制插入到左侧的空格量和每列右侧。由于 72.27 TeX 点 = 1 英寸,1 TeX 点约等于 0.3515 毫米,全部的每列的宽度m{1mm}
实际上是1mm+12*0.3515mm=5.22mm
。
如果字体是 Computer Modern,字体大小是11pt
,则每个数字的宽度是5.475pt
。假设您已指定可用的列宽仅为1mm=2.84pt
,毫不奇怪的是,您会收到许多关于框过满以及两位数突出到以下单元格的警告消息。
我认为 (a) 您希望将月份列保持尽可能窄,并且 (b) 所有 12 个月的列应等宽。如果是这种情况,我建议您采取以下方法:设置\tabcolsep
为一个非常小的值,例如,0.5pt
并将每个月份列的可用宽度设置为标题单元格中最宽材料的宽度——此处为数字12
。数字的宽度12
为 2*5.475pt=10.95pt;单元格的总宽度将为10.95pt+2*0.5pt=11.95pt=4.2mm
——大约 1mm较少的比初始设置中要大。继续使用tabularx
环境来最大化第一列的可用宽度。
\documentclass[a4paper,11pt]{article}
% I've simplified and streamlined the preamble
\usepackage[utf8]{inputenc}
\usepackage[portuguese]{babel}
\usepackage[table]{xcolor}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
% Some new code
\newlength\mylength
\settowidth\mylength{12}
\newcommand\cg{\cellcolor{gray}} % handy shortcut macro
\begin{document}
\begin{table}[htbp]
\begin{tabularx}{\textwidth}{|C|
*{12}{>{\centering\arraybackslash}m{0.1cm}|}}
\hline
& \multicolumn{12}{c|}{Months} \\
\cline{2-13}
& 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 &10 &11 &12 \\
\hline
Activity 1 & \cellcolor{gray} & \cellcolor{gray} & \cellcolor{gray} & & & & & & & & & \\
\hline
Activity 2 & & \cellcolor{gray} & \cellcolor{gray} & & & & & & & & & \\
\hline
Activity 3 & & & \cellcolor{gray} & \cellcolor{gray} & \cellcolor{gray} & & & & & & & \\
\hline
Activity 4 & & & & & \cellcolor{gray} & \cellcolor{gray} & & & & & &\\
\hline
Activity 4 & & & & & & & \cellcolor{gray} & \cellcolor{gray} & \cellcolor{gray} & & & \\
\hline
Activity 4 & & & & & & & & & \cellcolor{gray} & \cellcolor{gray} & \cellcolor{gray} & \cellcolor{gray} \\
\hline
\end{tabularx}
\caption{Original form}
\end{table}
\setlength\tabcolsep{0.5pt} %default is 6pt
\begin{table}[htbp]
\begin{tabularx}{\textwidth}{|C|
*{12}{>{\centering\arraybackslash}m{\mylength}|}}
\hline
& \multicolumn{12}{c|}{Months} \\
\cline{2-13}
& 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 &10 &11 &12 \\
\hline
Activity 1 & \cg & \cg & \cg & & & & & & & & & \\
\hline
Activity 2 & & \cg & \cg & & & & & & & & & \\
\hline
Activity 3 & & & \cg & \cg & \cg & & & & & & & \\
\hline
Activity 4 & & & & & \cg & \cg & & & & & &\\
\hline
Activity 4 & & & & & & & \cg & \cg & \cg & & & \\
\hline
Activity 4 & & & & & & & & & \cg & \cg & \cg & \cg \\
\hline
\end{tabularx}
\caption{Modified form}
\end{table}
\end{document}
答案2
不要固定最后 12 列的列宽,而是将内容设置在常规c
输入(非固定宽度)列中。然后使用collcell
捕获列的内容并将它们设置在自然适合最宽元素的框中。后者的 max-width-box-fixing 是使用eqparbox
的\eqmakebox[<tag>]{<stuff>}
。使用相同的元素<tag>
被设置在相同的、自然最宽的盒子中。
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tabularx,collcell,eqparbox}
\newcommand{\fixwidth}[1]{\eqmakebox[colhead]{#1}}
\newcolumntype{C}{>{\collectcell\fixwidth}c<{\endcollectcell}}
\newcommand{\graycell}{\cellcolor{gray}}
\begin{document}
\begin{table}
\begin{tabularx}{\textwidth}{ | >{\centering\arraybackslash}X | *{12}{C|} }
\hline
& \multicolumn{12}{c|}{Months} \\ \cline{2-13}
& 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 &10 &11 &12 \\ \hline
Activity 1 & \graycell & \graycell & \graycell & & & & & & & & & \\ \hline
Activity 2 & & \graycell & \graycell & & & & & & & & & \\ \hline
Activity 3 & & & \graycell & \graycell & \graycell & & & & & & & \\ \hline
Activity 4 & & & & & \graycell & \graycell & & & & & & \\ \hline
Activity 4 & & & & & & & \graycell & \graycell & \graycell & & & \\ \hline
Activity 4 & & & & & & & & & \graycell & \graycell & \graycell & \graycell \\ \hline
\end{tabularx}
\caption{Cronogram}
\end{table}
\end{document}
如果您想要多个具有不同固定宽度的表格,也可以使用手动插入\eqmakebox
适当的特定于表格来实现<tag>
。
如果您确实希望列为0.1cm
(或1mm
),则可以在序言中使用以下定义:
\newcommand{\fixwidth}{\makebox[0pt]}
\newcolumntype{C}{>{\centering\arraybackslash\collectcell\fixwidth}p{1mm}<{\endcollectcell}}
那就无需使用eqparbox
。