我正在尝试创建一个表,其中除一行之外的所有行都有两列,我使用的代码是这样的
\begin{table}[h]
\centering
\begin{tabular}{c|c|c}
\toprule
\multicolumn{2}{c}{Model Parameters} \\ \midrule
Hidden layers & 1 & 2 \\
Units in hidden layer & \multicolumn{2}{c}{100-1000} \\
Input Scaling & \multicolumn{2}{c}{0.5-1.0} \\
Spectral Radius & \multicolumn{2}{c}{0.1-0.99} \\
Leaky & \multicolumn{2}{c}{0.1-1.0} \\
Connectivity Input & \multicolumn{2}{c}{$\approx units$} \\
Connectivity Recurrent & \multicolumn{2}{c}{10} \\
Learning rate & \multicolumn{2}{c}{0.01, 0.001, 0.0001}\\
\bottomrule
\end{tabular}
\end{table}
但是从图中可以看出,顶行和隐藏层行没有居中。将文本居中的正确方法是什么?
答案1
对于这个答案,我假设“隐藏层”行中的“1”和“2”单元格应该等宽。如果这个假设是正确的,那么下面的答案可能会让你感兴趣。它采用了“内部”环境,tabular
并使用包\widthof
的宏calc
来确定两个隐藏单元格的可用宽度。
\documentclass{article}
\usepackage{newpxtext,newpxmath} \useosf % optional: Palatino-clone fonts
\usepackage{booktabs}
\usepackage{calc} % for \widthof macro
\usepackage{array} % for 'w' column type
\newlength\mylen
\setlength\mylen{(\widthof{0.01, 0.001, 0.0001}-2\tabcolsep-\arrayrulewidth)/2}
\begin{document}
\begin{table}[h]
\centering
\begin{tabular}{c|c} % start the outer 'tabular' env.
\toprule
\multicolumn{2}{c}{Model Parameters} \\
\midrule
Hidden layers &
%% start the inner 'tabular' environment
\begin{tabular}{ @{} wc{\mylen} | wc{\mylen}@{} }
1 & 2
\end{tabular} \\
Units in hidden layer & 100--1000 \\
Input Scaling & 0.5--1.0 \\
Spectral Radius & 0.1--0.99 \\
Leaky & 0.1--1.0 \\
Connectivity Input & $\approx \mathit{units}$ \\
Connectivity Recurrent & 10 \\
Learning rate & 0.01, 0.001, 0.0001 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
答案2
如果我正确理解了您的需求......
\documentclass{article}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{c|c|c}
\toprule
% \multicolumn{2}{c}{Model Parameters} \\ \midrule
\multicolumn{3}{c}{Model Parameters} \\ \midrule
Hidden layers & 1 & 2 \\
Units in hidden layer & \multicolumn{2}{c}{100-1000} \\
Input Scaling & \multicolumn{2}{c}{0.5-1.0} \\
Spectral Radius & \multicolumn{2}{c}{0.1-0.99} \\
Leaky & \multicolumn{2}{c}{0.1-1.0} \\
Connectivity Input & \multicolumn{2}{c}{$\approx units$} \\
Connectivity Recurrent & \multicolumn{2}{c}{10} \\
Learning rate & \multicolumn{2}{c}{0.01, 0.001, 0.0001}\\
\bottomrule
\end{tabular}
Or rather
\begin{tabular}{c|c}
\toprule
\multicolumn{2}{c}{Model Parameters} \\ \midrule
Hidden layers & 1 \\ %2 seems to be only a marker
Units in hidden layer & {100-1000} \\
Input Scaling &{0.5-1.0} \\
Spectral Radius & {0.1-0.99} \\
Leaky & {0.1-1.0} \\
Connectivity Input & {$\approx units$} \\
Connectivity Recurrent & {10} \\
Learning rate & {0.01, 0.001, 0.0001}\\
\bottomrule
\end{tabular}
\end{document}
编译的结果很明显,所以我省略了一张图片。
为了将来:请在此处放置一份可编译的文档,从 开始\documentclass
等等。现在我只是猜测您正在使用booktabs
包。