![无法勾勒出多列表格标题](https://linux22.com/image/433913/%E6%97%A0%E6%B3%95%E5%8B%BE%E5%8B%92%E5%87%BA%E5%A4%9A%E5%88%97%E8%A1%A8%E6%A0%BC%E6%A0%87%E9%A2%98.png)
我正在尝试制作一个包含四列的表格。标题是包含的特征、标准化、RMSE、数字。我当前的代码:
\begin{table}[htpb]
\caption{Tested models}
\begin{tabular}{cccc}
\toprule
Included features & \multicolumn{3}{c}{Normalization}{RMSE}{Number} \\
\midrule
Close, volume & None & 18153.33 & 1 \\
\midrule
Close, volume, rsi, sma, ema, $stoch_k$, $stoch_d$ & None & 24292.86 & 2 \\
\midrule
Close, volume, rsi, sma, ema, $stoch_k$, $stoch_d$ & L2 & 15596.44 & 3\\
\midrule
\shortstack{Close, volume, rsi, sma, ema, $stoch_k$, \\ $stoch_d$, ad, cci, macd} & L2 & 18290.07 & 4\\
\midrule
\shortstack{\textbf{Close, volume, quote av, trades, tb base av,} \\ \textbf{tb quote av, rsi, sma, ema, $stoch_k$, $stoch_d$}} & \textbf{L2} & \textbf{12835.51} & \textbf{5} \\
\midrule
\shortstack{Close, volume, quote av, trades, tb base a, \\ tb quote av, rsi, sma, ema} & L2 & 13390.82 & 6\\
\midrule
\shortstack{Close, volume, quote av, trades, tb base av, \\ tb quote av, rsi, sma, ema, $stoch_k$, $stoch_d$, ad, cci, macd} & L2 & 13042.53 & 7\\
\midrule
\shortstack{Close, volume, quote av, trades, tb base av, \\ tb quote av, rsi, sma, ema, $stoch_k$, $stoch_d$, ad, cci, macd} & None & 22602.08 & 8\\
\bottomrule
\end{tabular}
\bigskip
\end{table}
如您所见,Normalization、RMSE 和 Number 的轮廓很奇怪。我对 latex 的使用经验不多,所以不太清楚这是怎么回事。我是不是误解了 \multicolumn 的工作原理?
答案1
我不确定为什么multicolumn
你的表格有 4 列,但multicolumn
结果却变成了 1+3 列。你的代码(在article
类中)生成的表格对于文本宽度来说太宽了。
在下面的 MWE 中,我已丢弃multicolumn
4 个常规列。我注意到短语“收盘价,成交量”出现在第一列的每个条目中,因此我认为最好将其删除并记录在其他地方,以简化外观。
% multiprob.tex SE 597728
\documentclass{article}
\usepackage{booktabs}
\begin{document}
\newcommand{\cvol}{Close, volume,}
\renewcommand{\cvol}{}
\begin{table}[htpb]
\caption{Tested models (with all features including Close, volume)}
\begin{tabular}{cccc}
\toprule
%Included features & \multicolumn{3}{c}{Normalization}{RMSE}{Number} \\
Included features & Normalization & RMSE & Number \\
\midrule
%Close, volume
\cvol & None & 18153.33 & 1 \\
\midrule
%Close, volume,
\cvol rsi, sma, ema, $stoch_k$, $stoch_d$ & None & 24292.86 & 2 \\
\midrule
%Close, volume,
\cvol rsi, sma, ema, $stoch_k$, $stoch_d$ & L2 & 15596.44 & 3\\
\midrule
\shortstack{\cvol rsi, sma, ema, $stoch_k$, \\ $stoch_d$, ad, cci, macd} & L2 & 18290.07 & 4\\
\midrule
\shortstack{\textbf{%Close, volume,
\cvol quote av, trades, tb base av,} \\ \textbf{tb quote av, rsi, sma, ema,} \\ \textbf{$stoch_k$, $stoch_d$}} & \textbf{L2} & \textbf{12835.51} & \textbf{5} \\
\midrule
\shortstack{\cvol quote av, trades, tb base a, \\ tb quote av, rsi, sma, ema} & L2 & 13390.82 & 6\\
\midrule
\shortstack{\cvol quote av, trades, tb base av, \\ tb quote av, rsi, sma, ema, $stoch_k$, \\ $stoch_d$, ad, cci, macd} & L2 & 13042.53 & 7\\
\midrule
\shortstack{\cvol quote av, trades, tb base av, \\ tb quote av, rsi, sma, ema, $stoch_k$, \\ $stoch_d$, ad, cci, macd} & None & 22602.08 & 8\\
\bottomrule
\end{tabular}
\bigskip
\end{table}
\begin{table}[htpb]
\caption{Tested models}
\begin{tabular}{cccc}
\toprule
Included features & \multicolumn{3}{c}{Normalization}{RMSE}{Number} \\
\midrule
Close, volume & None & 18153.33 & 1 \\
\midrule
Close, volume, rsi, sma, ema, $stoch_k$, $stoch_d$ & None & 24292.86 & 2 \\
\midrule
Close, volume, rsi, sma, ema, $stoch_k$, $stoch_d$ & L2 & 15596.44 & 3\\
\midrule
\shortstack{Close, volume, rsi, sma, ema, $stoch_k$, \\ $stoch_d$, ad, cci, macd} & L2 & 18290.07 & 4\\
\midrule
\shortstack{\textbf{Close, volume, quote av, trades, tb base av,} \\ \textbf{tb quote av, rsi, sma, ema, $stoch_k$, $stoch_d$}} & \textbf{L2} & \textbf{12835.51} & \textbf{5} \\
\midrule
\shortstack{Close, volume, quote av, trades, tb base a, \\ tb quote av, rsi, sma, ema} & L2 & 13390.82 & 6\\
\midrule
\shortstack{Close, volume, quote av, trades, tb base av, \\ tb quote av, rsi, sma, ema, $stoch_k$, $stoch_d$, ad, cci, macd} & L2 & 13042.53 & 7\\
\midrule
\shortstack{Close, volume, quote av, trades, tb base av, \\ tb quote av, rsi, sma, ema, $stoch_k$, $stoch_d$, ad, cci, macd} & None & 22602.08 & 8\\
\bottomrule
\end{tabular}
\bigskip
\end{table}
\end{document}
有些shortstack
结果有点太宽,所以我对它们做了一些修改(缩短了一些行,增加了行数)。结果是一个适合文本宽度的表格。
我觉得有一种比使用更方便(自动?)的方法来获取多个输入行shortstack
(我对您使用这种方法印象深刻);希望其他人可以对此提出建议。
答案2
由于“标准化”、“RMSE”和“数字”似乎是三个单独列的标题,因此只需替换\multicolumn{3}{c}{Normalization}{RMSE}{Number}
即可Normalization & RMSE & Number
获得预期的输出。
在以下 MWE 中,我还使用了该tabularx
包,以便允许在第一列自动换行,同时确保表格适合文本宽度。第二个示例显示的表格行数较少,但有一些空白,并且第一列的对齐方式也不同。
\documentclass{article}
\usepackage{booktabs}
\usepackage{tabularx}
\begin{document}
\begin{table}[htpb]
\caption{Tested models}
\begin{tabularx}{\linewidth}{>{\centering\arraybackslash}Xccc}
\toprule
Included features & Normalization & RMSE & Number \\
\midrule
Close, volume & None & 18153.33 & 1 \\
\midrule
Close, volume, rsi, sma, ema, $stoch_k$, $stoch_d$ & None & 24292.86 & 2 \\
\midrule
Close, volume, rsi, sma, ema, $stoch_k$, $stoch_d$ & L2 & 15596.44 & 3\\
\midrule
Close, volume, rsi, sma, ema, $stoch_k$, $stoch_d$, ad, cci, macd & L2 & 18290.07 & 4\\
\midrule
\textbf{Close, volume, quote av, trades, tb base av,tb quote av, rsi, sma, ema, $stoch_k$, $stoch_d$} & \textbf{L2} & \textbf{12835.51} & \textbf{5} \\
\midrule
Close, volume, quote av, trades, tb base a, tb quote av, rsi, sma, ema & L2 & 13390.82 & 6\\
\midrule
Close, volume, quote av, trades, tb base av, tb quote av, rsi, sma, ema, $stoch_k$, $stoch_d$, ad, cci, macd & L2 & 13042.53 & 7\\
\midrule
Close, volume, quote av, trades, tb base av, tb quote av, rsi, sma, ema, $stoch_k$, $stoch_d$, ad, cci, macd & None & 22602.08 & 8\\
\bottomrule
\end{tabularx}
\end{table}
\begin{table}[htpb]
\caption{Tested models}
\begin{tabularx}{\linewidth}{>{\raggedright\arraybackslash}Xccc}
\toprule
Included features & Normalization & RMSE & Number \\
\midrule
Close, volume & None & 18153.33 & 1 \\
\addlinespace
Close, volume, rsi, sma, ema, $stoch_k$, $stoch_d$ & None & 24292.86 & 2 \\
\addlinespace
Close, volume, rsi, sma, ema, $stoch_k$, $stoch_d$ & L2 & 15596.44 & 3\\
\addlinespace
Close, volume, rsi, sma, ema, $stoch_k$, $stoch_d$, ad, cci, macd & L2 & 18290.07 & 4\\
\addlinespace
\textbf{Close, volume, quote av, trades, tb base av,tb quote av, rsi, sma, ema, $stoch_k$, $stoch_d$} & \textbf{L2} & \textbf{12835.51} & \textbf{5} \\
\addlinespace
Close, volume, quote av, trades, tb base a, tb quote av, rsi, sma, ema & L2 & 13390.82 & 6\\
\addlinespace
Close, volume, quote av, trades, tb base av, tb quote av, rsi, sma, ema, $stoch_k$, $stoch_d$, ad, cci, macd & L2 & 13042.53 & 7\\
\addlinespace
Close, volume, quote av, trades, tb base av, tb quote av, rsi, sma, ema, $stoch_k$, $stoch_d$, ad, cci, macd & None & 22602.08 & 8\\
\bottomrule
\end{tabularx}
\end{table}
\end{document}