我有一个包含数学公式的表格,我需要使用命令来增加单元格尺寸。然而,这会妨碍表格外部边框的形成。我怎样才能在这些表格上获得完美的边框?
表格1
\begin{table}[h!]
\centering
\rowcolors{2}{white}{gray!20}
\resizebox{\textwidth}{!}{
\begin{tabular}{|cccc|}
\toprule
\rowcolor{white}
\textbf{AAAA} & \makecell{ \textbf{BBB} \\ \textbf{BBBB}} & \makecell{\textbf{CCCC} \\ \textbf{CCCCCC}} & \textbf{DDDDD} \\
\midrule
AAAAAAAAA & 324 & 1400 or 2100 & AAAAAAA \\
BBBBBBB & 324 & 1400 or 2100 & BBBBBBB \\
\makecell{CCCCC \\ CCCCCC} & 380 & 1400 ou 2100 & \makecell{DDDDDDDDDD} \\
\midrule
\end{tabular}
}
\end{table}
表 2
\bgroup
\def\arraystretch{2.7}%
\begin{table}[!h]
\centering
\caption{Har}
\rowcolors{2}{white}{gray!20}
\resizebox{\textwidth}{!}{%
\begin{tabular}{|ll|}
%\toprule
\hline
\multicolumn{1}{|l}{\textbf{BBBBBBBBBB}} &
\multicolumn{1}{l|}{\textbf{AAAAAAAAAA}} \\
\toprule
AAAAAAA
& $ f_1 = \sum_{i}\sum_{j} \left\{p(i,j)\right\}^2 $ \\
AAAAAAAA
& \vspace{.5ex}\makecell[|l]{$f_2 = \sum\limits_{n=0}^{N_g - 1} n^2 \left\{ \sum\limits_{i=1}^{N_g}\sum\limits_{j=1}^{N_g}p(i,j)\right\}$, where
$n = |i-j| $.}\\
AAAAAAAA
& \makecell[l]{$f_3 = \dfrac{\sum_{i}\sum_{j}(ij)p(i,j)-\mu_{x}\mu_{y}}{\sigma_{x}\sigma_{y}} $, where $\mu_{x}$, $\mu_{y}$, $\sigma_{x}$ \\ $\sigma_{y}$.}\\
AAAAAAAA
& $f_4 = \sum_{i}\sum_{j}(i-\mu)^2 p(i,j)$\\
AAAAAAAA
& \vspace{.5ex} $f_5 = \sum_{i}\sum_{j}\dfrac{p(i,j)}{1+(i-j)^2}$\\
AAAAAAAAA
& $f_6 = \sum\limits_{i=2}^{2N_g}ip_{x+y}(i)$\\
AAAAAAAAA
& \vspace{.5ex} $f_7 = \sum\limits_{i=2}^{2N_g}(i-f_8)^2 p_{x+y}(i)$\\
AAAAAAAAA
& \vspace{.5ex} $f_8 = -\sum\limits_{i=2}^{2N_g}p_{x+y}(i)log\left\{p_{x+y}(i)\right\}$ \\
AAAAAAAAA
& $f_9 = -\sum_{i}\sum_{j}p(i,j)log\left\{p(i,j)\right\}$\\
AAAAAAAAA
& \vspace{.5ex} $f_{10} = - \sum\limits_{i=0}^{N_g-1}(i-f_{11})^2 p_{x-y}(i)$ \\
AAAAAAAAA
& \vspace{.5ex} $f_{11} = -\sum\limits_{i=0}^{N_g-1}p_{x-y}(i)log\left\{p_{x-y}(i)\right\}$\\
AAAAAAAAA
& \makecell[l]{$f_{14} = \sqrt{\text{...}}$\\
$Q(i,j)=\sum_{k}\dfrac{p(i,k)p(j,k)}{p_x(i)p_y(k)}$}\\
\midrule
\end{tabular}
}
\end{table}
\egroup
更新:使用 tabularx 时,我遇到了这个问题。我需要一个更紧凑的表格,我不想在最后一列中有很多行。我不在乎是否需要减小字体大小。我需要一种类似于使用时的效果,\resizebox
列之间有很大的空间。可以减少这个空间,以免在这么多行中打断最后一列的文本。
\usepackage[table]{xcolor}
\usepackage{boldline, cellspace, makecell, tabularx}
\setlength\cellspacetoplimit{5pt}
\setlength\cellspacebottomlimit{5pt}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\addparagraphcolumntypes{C}
\begin{table}[h!]
\centering
\caption{Características dos conjuntos de dados formados.}
\label{quadro:conjuntosdados}
\rowcolors{2}{white}{gray!20}
\begin{tabularx}{\linewidth}{|*{4}{SC}|}
\hlineB{2}
\rowcolor{white}
\textbf{Método} & \makecell{ \textbf{N. de} \\ \textbf{variáveis}} & \makecell{\textbf{N. de} \\ \textbf{amostras}} & \textbf{Descrição} \\
\hlineB{2}
Valores dos pixels s/ equalização & 324 & 1400 ou 2100 & Nível de intensidade de cada pixel \\
Valores dos pixels c/ equalização & 324 & 1400 ou 2100 & Nível de intensidade de cada pixel \\
Segmentação & 324 & 1400 ou 2100 & Estado do pixel (0 ou 1) \\
Segmentação (maior região) & 324 & 1400 ou 2100 & Estado do pixel (0 ou 1) \\
Máscara elíptica & 126 & 1400 ou 2100 & Nível de intensidade de cada pixel \\
Histograma & 64 & 1400 ou 2100 & Quant. de pixels em cada nível de cinza\\
Haralick & 56 & 1400 ou 2100 & Valores dos descritores de textura \\
Haralick reduzido & 14 & 1400 ou 2100 & Valores dos descritores de textura \\
Haralick duplo & 112 & 1400 ou 2100 & Valores dos descritores de textura \\
\makecell{Haralick e valores dos pixels \\com equalização} & 380 & 1400 ou 2100 & \makecell{Valores dos descritores de textura \\e nível de intensidade de cada pixel} \\
\hlineB{2}
\end{tabularx}
\end{table}
答案1
编辑: 您没有提供 MWE(最小工作示例),这是一个小而完整的文档,我们可以用它来测试您的代码片段。此外,问题不是很清楚,所以表格的设计大多符合我的口味。因此,请编辑您的问题并澄清,您的表格中存在哪些问题。
\limits
现在我添加了解释如何处理单元格内容周围的垂直空间,从第二个表的代码中删除虚假的左侧并添加\nolimits (which you can remove, if you liked such form of
\sum`)
- 包中定义的规则
booktabs
不适用于带有垂直线的表格。相反,您应该使用那些规则,\hline
或者当您希望使用不同粗细的表格规则时,请在包中hlineB{<factor>}
定义boldline
(用于第一个表格)或Xhline{<thicknes>}
在包中定义makecell
(用于第二个表格)。 - 如果需要单元格内容周围有更多垂直空间,可以使用包
cellspace
。此空间定义为
\setlength\cellspacetoplimit{5pt}
\setlength\cellspacebottomlimit{5pt}
- 如果您发现
5pt
太多,可以将其减少到所需数量(在 MWE 中现在减少到 2pt) \resizebox
不建议在表格中使用,因为使用它会导致表格中使用的字体大小丢失。更好的方法是使用表格环境,尽管您可以规定宽度,例如tabular*
或tabularx
如以下两个 MWE 中所使用的。
第一个表:
\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage[table]{xcolor}
\usepackage{boldline, cellspace, makecell, tabularx}
\setlength\cellspacetoplimit{5pt}
\setlength\cellspacebottomlimit{5pt}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\addparagraphcolumntypes{C}
\begin{document}
\begin{table}[ht]
\centering
\rowcolors{2}{white}{gray!20}
\begin{tabularx}{\linewidth}{|*{4}{SC}|}
\hlineB{2}
\textbf{AAAA}
& \makecell{\textbf{BBB} \\ \textbf{BBBB}}
& \makecell{\textbf{CCCC} \\ \textbf{CCCCCC}}
& \textbf{DDDDD} \\
\hlineB{1.5}
AAAAAAAAA & 324 & 1400 or 2100 & AAAAAAA \\
BBBBBBB & 324 & 1400 or 2100 & BBBBBBB \\
\makecell{CCCCC \\ CCCCCC}
& 380 & 1400 or 2100 & DDDDDDDDDD \\
\hlineB{2}
\end{tabularx}
\end{table}
\end{document}
第二张表:
\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage[table]{xcolor}
\usepackage{cellspace, makecell, tabularx}
\setlength\cellspacetoplimit{2pt}
\setlength\cellspacebottomlimit{2pt}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\addparagraphcolumntypes{L}
\begin{document}
\begin{table}[ht]
\centering
\rowcolors{2}{white}{gray!20}
\begin{tabularx}{\linewidth}{|Sl >{$\displaystyle}SL<{$}|}
\Xhline{1.2pt}
\textbf{BBBBBBBB}
& \textbf{AAAAAAAAAA} \\
\Xhline{0.8pt}
AAAAAAA & f_1 = \sum\nolimits_{i}\sum\nolimits_{j} \{p(i,j)\}^2 \\
AAAAAA & f_2 = \sum_{n=0}^{N_g - 1} n^2
\left\{\sum_{i=1}^{N_g}\sum\limits_{j=1}^{N_g}p(i,j)\right\},
\text{ where }
n = |i-j|. \\
AAAAAA & f_3 = \frac{\sum_{i}\sum_{j}(ij)p(i,j)-\mu_{x}\mu_{y}}{\sigma_{x}\sigma_{y}},
\text{ where }
\mu_{x}, \mu_{y}, \sigma_{x}, \sigma_{y} ? \\
AAAAAA & f_4 = \sum_{i}\sum_{j}(i-\mu)^2 p(i,j) \\
AAAAAA & f_5 = \sum\nolimits_{i}\sum\nolimits_{j}\frac{p(i,j)}{1+(i-j)^2} \\
AAAAAA & f_6 = \sum_{i=2}^{2N_g}ip_{x+y}(i) \\
AAAAAA & f_7 = \sum_{i=2}^{2N_g}(i-f_8)^2 p_{x+y}(i) \\
AAAAAA & f_8 = -\sum_{i=2}^{2N_g}p_{x+y}(i)\log\{p_{x+y}(i)\} \\
AAAAAA & f_9 = -\sum\nolimits_{i}\sum\nolimits_{j}p(i,j)\log\{p(i,j)\} \\
AAAAAA & f_{10} = - \sum_{i=0}^{N_g-1}(i-f_{11})^2 p_{x-y}(i) \\
AAAAAA & f_{11} = - \sum_{i=0}^{N_g-1}p_{x-y}(i)\log\{p_{x-y}(i)\} \\
AAAAAA & \begin{aligned}
f_{14} & = \sqrt{\text{...}} \\
Q(i,j) & =\sum_{k}\frac{p(i,k)p(j,k)}{p_x(i)p_y(k)}
\end{aligned} \\
\Xhline{1.2pt}
\end{tabularx}
\end{table}
\end{document}
附录:
- 首先我要指出的是,由于您不帮助我们,因此我们很难帮助您。尽管我多次要求您将这些片段扩展为完整的小文档,但您仍然只提供了代码片段。我们仍然对您的文档布局和使用的软件包一无所知。
- 请熟悉 LaTeX,特别是表格的编写。可能存在关于 LaTeX 的介绍性文字(例如迪基莫书籍或者LaTeX 的简单介绍) 以及使用 LaTeX 编写表格 (维基/表格)。
- 阅读包装文档也有很大帮助。
关于您的真实表格:
- 现在显然,您不需要所有列都有相同的宽度,因此您不需要所有
X
类型。 - 列
X
会自动将单元格中的文本拆分成更多行,因此无需\makecell
在内部手动拆分文本。此外,它们的使用会给您带来麻烦,因为您手动拆分文本不够充分——因此文本会溢出表格。 - 如果第二列中的数字右对齐且居中,则(就我个人而言)会更好看。这可以通过使用包
S
中定义的列类型来实现siunitx
。 - 使用
siunitx
包要求将包中定义的列说明符cellspace
从默认S
o 重新定义为其他。在下面的 MWE 中,我选择了O
此项。
对于这个新表的建议 MWE 为:
\documentclass[a4paper]{article}
\usepackage[spanish]{babel}
\usepackage{amsmath}
\usepackage[table]{xcolor}
\usepackage[column=O]{cellspace}
\setlength\cellspacetoplimit{2pt}
\setlength\cellspacebottomlimit{2pt}
\usepackage{makecell, tabularx}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadgape{}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash\hsize=#1\hsize}X}
\addparagraphcolumntypes{L}
\usepackage{siunitx}
\begin{document}
\begin{table}[ht]
\centering
\caption{Características dos conjuntos de dados formados.}
\label{quadro:conjuntosdados}
\renewcommand\tabularxcolumn[1]{m{#1}}
\rowcolors{2}{white}{gray!20}
\begin{tabularx}{\linewidth}{|O{L{0.8}}
S[table-format=3.0]
c
O{L{1.2}}|}
\Xhline{1.2pt}
\rowcolor{white}
\thead{Método}
& {\thead{N. de\\ variáveis}}
& \thead{N. de\\ amostras}
& \thead{Descrição} \\
\Xhline{0.8pt}
Valores dos pixels s/ equalização
& 324 & 1400 ou 2100 & Nível de intensidade de cada pixel \\
Valores dos pixels c/ equalização
& 324 & 1400 ou 2100 & Nível de intensidade de cada pixel \\
Segmentação
& 324 & 1400 ou 2100 & Estado do pixel (0 ou 1) \\
Segmentação (maior região)
& 324 & 1400 ou 2100 & Estado do pixel (0 ou 1) \\
Máscara elíptica
& 126 & 1400 ou 2100 & Nível de intensidade de cada pixel \\
Histograma
& 64 & 1400 ou 2100 & Quant. de pixels em cada nível de cinza \\
Haralick
& 56 & 1400 ou 2100 & Valores dos descritores de textura \\
Haralick reduzido
& 14 & 1400 ou 2100 & Valores dos descritores de textura \\
Haralick duplo
& 112 & 1400 ou 2100 & Valores dos descritores de textura \\
Haralick e valores dos pixels com equalização
& 380 & 1400 ou 2100 & Valores dos descritores de textura e nível de intensidade de cada pixel \\
\Xhline{1.2pt}
\end{tabularx}
\end{table}
\end{document}
答案2
以下是您可以使用 做的{NiceTabular}
事情nicematrix
。
\documentclass{article}
\usepackage{xcolor}
\usepackage{booktabs}
\usepackage{nicematrix}
\begin{document}
\begin{table}[h!]
\centering
\begin{NiceTabular}{|cccc|}[code-before=\rowcolors{2}{}{gray!20}]
\toprule
\textbf{AAAA} & \Block{}{ \textbf{BBB} \\ \textbf{BBBB}} & \Block{}{\textbf{CCCC} \\ \textbf{CCCCCC}} & \textbf{DDDDD} \\
\midrule
AAAAAAAAA & 324 & 1400 or 2100 & AAAAAAA \\
BBBBBBB & 324 & 1400 or 2100 & BBBBBBB \\
\Block{}{CCCCC \\ CCCCCC} & 380 & 1400 ou 2100 & DDDDDDDDDD \\
\midrule
\end{NiceTabular}
\end{table}
\begin{table}[!ht]
\centering
\caption{Har}
\begin{NiceTabular}{|ll|}[code-before = \rowcolors{2}{}{gray!20},cell-space-top-limit=1ex,cell-space-bottom-limit=1ex]
%\toprule
\hline
\Block[l]{}{\textbf{BBBBBBBBBB}} &
\Block[l]{}{\textbf{AAAAAAAAAA}} \\
\toprule
AAAAAAA
& $ f_1 = \sum_{i}\sum_{j} \left\{p(i,j)\right\}^2 $ \\
AAAAAAAA
&\Block[l]{}{$f_2 = \sum\limits_{n=0}^{N_g - 1} n^2 \left\{ \sum\limits_{i=1}^{N_g}\sum\limits_{j=1}^{N_g}p(i,j)\right\}$, where
$n = |i-j| $.}\\
AAAAAAAA
& \Block[l]{}{$f_3 = \dfrac{\sum_{i}\sum_{j}(ij)p(i,j)-\mu_{x}\mu_{y}}{\sigma_{x}\sigma_{y}} $, \\
where $\mu_{x}$, $\mu_{y}$, $\sigma_{x}$ and $\sigma_{y}$ are...}\\
AAAAAAAA
& $f_4 = \sum_{i}\sum_{j}(i-\mu)^2 p(i,j)$\\
AAAAAAAA
& $f_5 = \sum_{i}\sum_{j}\dfrac{p(i,j)}{1+(i-j)^2}$\\
AAAAAAAAA
& $f_6 = \sum\limits_{i=2}^{2N_g}ip_{x+y}(i)$\\
AAAAAAAAA
& $f_7 = \sum\limits_{i=2}^{2N_g}(i-f_8)^2 p_{x+y}(i)$\\
AAAAAAAAA
& $f_8 = -\sum\limits_{i=2}^{2N_g}p_{x+y}(i)\log\left\{p_{x+y}(i)\right\}$ \\
AAAAAAAAA
& $f_9 = -\sum_{i}\sum_{j}p(i,j)\log\left\{p(i,j)\right\}$\\
AAAAAAAAA
& $f_{10} = - \sum\limits_{i=0}^{N_g-1}(i-f_{11})^2 p_{x-y}(i)$ \\
AAAAAAAAA
& $f_{11} = -\sum\limits_{i=0}^{N_g-1}p_{x-y}(i)\log\left\{p_{x-y}(i)\right\}$\\
AAAAAAAAA
& \Block[l]{}{$f_{14} = \sqrt{\text{...}}$\\
$Q(i,j)=\sum_{k}\dfrac{p(i,k)p(j,k)}{p_x(i)p_y(k)}$}\\
\midrule
\end{NiceTabular}
\end{table}
\end{document}
您需要多次编译(因为nicematrix
使用 PGF/Tikz 节点)。