使用创建表格时,tabulary
我遇到的问题是表格的行太小,导致公式触及表格的边框。我使用的代码如下。
\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{tabulary}
\usepackage{booktabs}
\usepackage{tabularx}
\begin{document}
\centering
\begin{tabulary}{18cm}{R|L L L}
\toprule
\hline
Mean squared error & MSE &= &$\frac{1}{n}\sum_{t=1}^{n}e_t^2$ \\
\hline
Root mean squared error & RMSE &= &$\sqrt{\frac{1}{n}\sum_{t=1}^{n}e_t^2}$ \\
\hline
Mean absolute error & MAE &= &$\frac{1}{n}\sum_{t=1}^{n}|e_t|$ \\
\hline
Mean absolute percentage error & MAPE &= &$\frac{100\%}{n}\sum_{t=1}^{n}\left |\frac{e_t}{y_t}\right|$\\
\hline\bottomrule
\end{tabulary}
\end{document}
如何增加表格的垂直空间以使其看起来合适?
答案1
不要使用\hline
垂直规则,并且删除它,因为它不会增加任何信息,只会打断阅读流程。
\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{tabulary}
\usepackage{booktabs}
\usepackage{amsmath}
\begin{document}
\centering
\begin{tabulary}{18cm}{L R @{} >{${}}c<{{}$} @{} L}
\toprule
Mean squared error & MSE &= &$\displaystyle\frac{1}{n}\sum_{t=1}^{n}e_t^2$ \\
\midrule
Root mean squared error & RMSE &= &$\displaystyle\sqrt{\frac{1}{n}\sum_{t=1}^{n}e_t^2}$ \\
\midrule
Mean absolute error & MAE &= &$\displaystyle\frac{1}{n}\sum_{t=1}^{n}|e_t|$ \\
\midrule
Mean absolute percentage error & MAPE &= &$\displaystyle\frac{100\%}{n}\sum_{t=1}^{n}\left |\frac{e_t}{y_t}\right|$\\
\bottomrule
\end{tabulary}
\end{document}
我不确定您是否真的需要tabulary
:这会产生完全相同的结果。
\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{array}
\usepackage{booktabs}
\usepackage{amsmath}
\begin{document}
\centering
\begin{tabular}{l r @{} >{${}}c<{{}$} @{} l}
\toprule
Mean squared error & MSE &= &$\displaystyle\frac{1}{n}\sum_{t=1}^{n}e_t^2$ \\
\midrule
Root mean squared error & RMSE &= &$\displaystyle\sqrt{\frac{1}{n}\sum_{t=1}^{n}e_t^2}$ \\
\midrule
Mean absolute error & MAE &= &$\displaystyle\frac{1}{n}\sum_{t=1}^{n}|e_t|$ \\
\midrule
Mean absolute percentage error & MAPE &= &$\displaystyle\frac{100\%}{n}\sum_{t=1}^{n}\left |\frac{e_t}{y_t}\right|$\\
\bottomrule
\end{tabular}
\end{document}
答案2
不带tabulary
(也适用于tabulary
\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{array,booktabs}
\usepackage{amsmath}
\begin{document}
{\def\arraystretch{2.5}
\begin{tabular}{@{} l r @{} >{${}}c<{{}$} @{} l @{}}
\toprule
Mean squared error & MSE &= &$\dfrac{1}{n}\sum\limits_{t=1}^{n}e_t^2$ \\
\midrule
Root mean squared error & RMSE &= &$\sqrt{\dfrac{1}{n}\sum\limits_{t=1}^{n}e_t^2}$ \\
\midrule
Mean absolute error & MAE &= &$\dfrac{1}{n}\sum\limits_{t=1}^{n}|e_t|$ \\
\midrule
Mean absolute percentage error & MAPE &= &$\dfrac{100\%}{n}\sum\limits_{t=1}^{n}\left |\frac{e_t}{y_t}\right|$\\
\bottomrule
\end{tabular}}
\end{document}
答案3
该cellspace
封装可确保电池顶部与上方电池底部之间的间距最小,并且电池底部与下方电池顶部之间的间距也最小。这样,您就可以获得更好的电池垂直间距和单元格内容垂直居中,但如果您使用 则不会出现这种情况\arraystretch
。Makecell
使用命令可以允许使用不同粗细的水平线Xhline
。
另外我简化了原来的代码(少了1列):\documentclass{scrartcl} \usepackage[utf8]{inputenc} \usepackage{tabulary} \usepackage{booktabs} \usepackage{amsmath}
\usepackage{cellspace}
\setlength{\cellspacetoplimit}{6pt}
\setlength{\cellspacebottomlimit}{6pt}
\usepackage{makecell}
\newcommand*{\topdblline}{\Xhline{0.15ex}\\[-2.6ex]\hline}
\newcommand*{\botdblline}{\hline\\[-2.6ex]\Xhline{0.15ex}}
\begin{document}
\centering
\begin{tabulary}{18cm}{L @{\qquad}R @{${}={}$} >{$\displaystyle}SL<{$}}%{L R @{} >{${}}c<{{}$} @{} SL}
\topdblline
Mean squared error & MSE & \frac{1}{n}\sum_{t=1}^{n}e_t^2 \\
\hline
Root mean squared error & RMSE & \sqrt{\frac{1}{n}\sum_{t=1}^{n}e_t^2} \\
\hline
Mean absolute error & MAE & \frac{1}{n}\sum_{t=1}^{n}|e_t| \\
\hline
Mean absolute percentage error & MAPE & \frac{100\%}{n}\sum_{t=1}^{n}\left |\frac{e_t}{y_t}\right| \\
\botdblline
\end{tabulary}
\end{document}