![使表格中的值居中](https://linux22.com/image/423949/%E4%BD%BF%E8%A1%A8%E6%A0%BC%E4%B8%AD%E7%9A%84%E5%80%BC%E5%B1%85%E4%B8%AD.png)
以下是我完成格式化的方式:
\begin{table}[htbp]
\centering
\begin{tabularx}{\linewidth}{|l|X|X|X|X|X|X|X|}
\hline
\multicolumn{1}{|c|}{\textit{\textbf{Parameters}}} &
\multicolumn{1}{c|}{\textit{\textbf{Test (WOE)}}} &
\multicolumn{1}{c|}{\textit{\textbf{Test (WRE)}}} &
\multicolumn{1}{c|}{\textit{\textbf{G}}} &
\multicolumn{1}{c|}{\textit{\textbf{B}}} &
\multicolumn{1}{c|}{\textit{\textbf{W}}}
\tabularnewline \hline
0.05\_0.025\_0.05\_0.2 & 24.86 & 37.67 & & &
\tabularnewline \hline
0.05\_0.05\_0.05\_0.1 & 36.15 & 52.53 & & &
\tabularnewline \hline
0.05\_0.075\_0.05\_0.2 & 24.23 & 35.30 & & &
\tabularnewline \hline
0.1\_0.05\_0.2\_0.2 & 15.58 & 41.2 & & &
\tabularnewline \hline
0.2\_0.05\_0.05\_0 & 18.78 & 52.98 & & &
\tabularnewline \hline
0.2\_0.05\_0.1\_0.05 & 22.91 & 54.06 & & &
\tabularnewline \hline
\end{tabularx}
\caption{To add}
\label{table:to add}
\end{table}
似乎居中并不能使代码正常工作
任何建议都将不胜感激
答案1
我认为您希望采用环境的原因tabularx
是为了确保五个数据列都具有完全相同的宽度。
我建议您定义一个居中版本的X
列类型并删除\multicolumn
包装器,以防止出现其他合理的换行符。
\documentclass{article}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X} % centered version of 'X'
\begin{document}
\begin{table}[htbp]
%\centering %% redundant
\begin{tabularx}{\linewidth}{ |l| *{5}{C|} }
\hline
\textit{\textbf{Parameters}} &
\textit{\textbf{Test (WOE)}} &
\textit{\textbf{Test (WRE)}} &
\textit{\textbf{G}} &
\textit{\textbf{B}} &
\textit{\textbf{W}} \\
\hline
0.05\_0.025\_0.05\_0.2 & 24.86 & 37.67 & & & \\
\hline
0.05\_0.05\_0.05\_0.1 & 36.15 & 52.53 & & & \\
\hline
0.05\_0.075\_0.05\_0.2 & 24.23 & 35.30 & & & \\
\hline
0.1\_0.05\_0.2\_0.2 & 15.58 & 41.2\phantom{0} & & & \\
\hline
0.2\_0.05\_0.05\_0 & 18.78 & 52.98 & & & \\
\hline
0.2\_0.05\_0.1\_0.05 & 22.91 & 54.06 & & &\\
\hline
\end{tabularx}
\caption{To add}
\label{table:to add}
\end{table}
\end{document}
答案2
假设除第一列外,所有列都是十进制数,其中有两个整数和两个小数位。在这种情况下,以下siunitx
和siunitx
包的组合会很方便。对于列标题,定义了两个新命令:\mcx
和\mr
作为\multirow{2}{*}{ ... }
(用于单元格跨越两行文本的列标题中):
\documentclass{article}
\usepackage{multirow, tabularx}
\newcommand\mcx[1]{\multicolumn{1}{>{\itshape\bfseries%
\centering\arraybackslash}X|}{#1}}
\newcommand\mr[1]{\multirow{2}{*}{{#1}}}
\usepackage{siunitx}
\begin{document}
\begin{table}[htb]
\begin{tabularx}{\linewidth}{ |l| *{5}{S[table-format=2.2]|} }
\hline
\mr{\textit{\textbf{Parameters}}}
& \mcx{Test (WOE)}
& \mcx{Test (WRE)}
& \mcx{\mr{G}}
& \mcx{\mr{B}}
& \mcx{\mr{W}} \\
\hline
0.05\_0.025\_0.05\_0.2 & 24.86 & 37.67 & & & \\
\hline
0.05\_0.05\_0.05\_0.1 & 36.15 & 52.53 & & & \\
\hline
0.05\_0.075\_0.05\_0.2 & 24.23 & 35.30 & & & \\
\hline
0.1\_0.05\_0.2\_0.2 & 15.58 & 41.2 & & & \\
\hline
0.2\_0.05\_0.05\_0 & 18.78 & 52.98 & & & \\
\hline
0.2\_0.05\_0.1\_0.05 & 22.91 & 54.06 & & &\\
\hline
\end{tabularx}
\caption{To add (table captions are usually at top of tables)}
\label{table:to add}
\end{table}
\end{document}