![当使用“m{width}”指定列宽时,如何强制水平居中?](https://linux22.com/image/357790/%E5%BD%93%E4%BD%BF%E7%94%A8%E2%80%9Cm%7Bwidth%7D%E2%80%9D%E6%8C%87%E5%AE%9A%E5%88%97%E5%AE%BD%E6%97%B6%EF%BC%8C%E5%A6%82%E4%BD%95%E5%BC%BA%E5%88%B6%E6%B0%B4%E5%B9%B3%E5%B1%85%E4%B8%AD%EF%BC%9F.png)
以下是代码:
\documentclass{report}
\usepackage{amsmath}
\usepackage{amssymb, amsfonts}
\usepackage[flushleft]{threeparttable}
\usepackage{makecell}
\usepackage{ragged2e}
\usepackage{tabularx}
\newcolumntype{L}{ >{ \arraybackslash \RaggedRight } X } % left justified
\newcolumntype{C}{ >{ \arraybackslash \Centering } X } % centered
\newcolumntype{R}{ >{ \arraybackslash \RaggedLeft } X } % right justified
\newcolumntype{J}{ >{ \arraybackslash \justifying } X } % justified
\renewcommand \tabularxcolumn [1] { >{ \centering } m{#1} }
\usepackage{booktabs}
\begin{document}
\begin{table*}[]
% Table options
\caption{Elasticity Values}
\label{table:PV_mod_STC_comp2}
% Center the table
\centering
% Stretch the table (vertically)
\renewcommand{\arraystretch}{1.3}
% t: puts the table in two columns format
\begin{threeparttable}
\begin{tabularx}{1\textwidth}{m{2.5cm}|C|C|m{1.2cm}|m{1.2cm}}
\toprule
\textbf{Customer type} &
\multicolumn{2}{c|}{\textbf{Set 1}}
&
\multicolumn{2}{c}{\textbf{Set 2}}
\\
\Xhline{0.2 mm}
\textbf{Residential} &
$-0.09$ to $-0.5$ &
$-0.12$ to $-1.56$ & \Centering $1$ & \Centering $2$
\\
\Xhline{0.2 mm}
\textbf{Commercial} & $-0.019$ to $-0.25$ & $-0.44$ to $-0.97$ & \Centering $3$ & \Centering $4$
\\
\Xhline{0.2 mm}
\textbf{Industrial} & $-0.005$ to $-0.14$ & $-0.34$ to $-5.48$ & \Centering $5$ &
\Centering $6$
\\
\bottomrule
\end{tabularx}
% Table notes
\begin{tablenotes}
\item[a] This is a table note
\end{tablenotes}
\end{threeparttable}
\end{table*}
\end{document}
结果:
\Centering
这是一个很好的结果,但是反复使用该命令是多余的。有没有办法强制所有单元格自动居中?
答案1
只需使用>{\centering\arraybackslash}m{some width}}
。
我借此机会稍微改进了您的表格,将其放置\caption
在threeparttable
环境中并扩大第一列以使所有单元格都位于一行中。我还提出了一种更好看的布局(在我看来),没有垂直线,水平线更少,用垂直填充代替。
\documentclass{report}
\usepackage{amsmath}
\usepackage{amssymb, amsfonts}
\usepackage[flushleft]{threeparttable}
\usepackage{makecell}
\usepackage{ragged2e}
\usepackage{tabularx}
\newcolumntype{L}{ >{ \arraybackslash \RaggedRight } X } % left justified
\newcolumntype{C}{ >{ \arraybackslash \Centering } X } % centered
\newcolumntype{R}{ >{ \arraybackslash \RaggedLeft } X } % right justified
\newcolumntype{J}{ >{ \arraybackslash \justifying } X } % justified
\renewcommand \tabularxcolumn [1] { >{ \centering } m{#1} }
\usepackage{booktabs}
\begin{document}
\begin{table*}[!htb]
% Table options
% Center the table
\centering
% Stretch the table (vertically)
\renewcommand{\arraystretch}{1.3}
\setlength\tabcolsep{4pt}
% t: puts the table in two columns format
\begin{threeparttable}
\caption{Elasticity Values}
\label{table:PV_mod_STC_comp2}
\begin{tabularx}{1\textwidth}{@{}m{3cm}|C|C| >{\centering}m{1.2cm}|>{\centering\arraybackslash}m{1.2cm}@{}}
\toprule
\textbf{Customer type} &
\multicolumn{2}{c|}{\textbf{Set 1}}
&
\multicolumn{2}{c}{\textbf{Set 2}}
\\
\Xhline{0.2 mm}
\textbf{Residential} &
$-0.09$ to $-0.5$ &
$-0.12$ to $-1.56$ & $1$ & $2$
\\
\Xhline{0.2 mm}
\textbf{Commercial} & $-0.019$ to $-0.25$ & $-0.44$ to $-0.97$ & $3$ & $4$
\\
\Xhline{0.2 mm}
\textbf{Industrial} & $-0.005$ to $-0.14$ & $-0.34$ to $-5.48$ & $5$ & $6$
\\
\bottomrule
\end{tabularx}
% Table notes
\begin{tablenotes}\footnotesize
\item[a] This is a table note
\end{tablenotes}
\end{threeparttable}
\end{table*}
\begin{table*}[!htb]
% Table options
% Center the table
\centering
% Stretch the table (vertically)
\renewcommand{\arraystretch}{1.3}
\setlength\tabcolsep{4pt}
% t: puts the table in two columns format
\begin{threeparttable}
\caption{Elasticity Values}
\label{table:PV_mod_STC_comp2}
\begin{tabularx}{1\textwidth}{m{3cm}C@{\quad}C >{\centering}m{1.2cm}>{\centering\arraybackslash}m{1.2cm}}
\toprule
\textbf{Customer type} &
\multicolumn{2}{c}{\textbf{Set 1}}
&
\multicolumn{2}{c}{\textbf{Set 2}}
\\
\cmidrule(r){1-1}\cmidrule(lr){2-3}\cmidrule(lr){4-5}
\textbf{Residential} &
$-0.09$ to $-0.5$ &
$-0.12$ to $-1.56$ & $1$ & $2$
\\
\addlinespace
\textbf{Commercial} & $-0.019$ to $-0.25$ & $-0.44$ to $-0.97$ & $3$ & $4$
\\
\addlinespace
\textbf{Industrial} & $-0.005$ to $-0.14$ & $-0.34$ to $-5.48$ & $5$ & $6$
\\
\bottomrule
\end{tabularx}
% Table notes
\begin{tablenotes}\footnotesize
\item[a] This is a table note
\end{tablenotes}
\end{threeparttable}
\end{table*}
\end{document}
答案2
在序言中删除
\renewcommand \tabularxcolumn [1] { >{ \centering } m{#1} }
。这是错误的。也许你喜欢这样\newcolumntype{M}[1]{>{\Centering} m{#1} }
在列类型定义
\begin{tabularx}{1\textwidth}{m{2.5cm}|C|C|m{1.2cm}|m{1.2cm}}
中用替换\begin{tabularx}{1\textwidth}{m{2.5cm}|C|C|*{2}{>{\centering\arraybackslash}m{1.2cm}|}}
。
题外话: - 在threeparttable
通常其中-在列类型定义中\caption{...}
使用\Raggedleft
,,没有必要使用。\RaggedRight
Centering
\arraybackslash
梅威瑟:
\documentclass{report}
\usepackage{amsmath, amssymb, amsfonts}
\usepackage[flushleft]{threeparttable}
\usepackage{ragged2e}
\usepackage{booktabs, makecell, tabularx}
\newcolumntype{L}{ >{\RaggedRight } X } % left justified
\newcolumntype{C}{ >{\Centering } X } % centered
\newcolumntype{R}{ >{\RaggedLeft } X } % right justified
\newcolumntype{J}{ >{\justifying\arraybackslash } X } % justified
\begin{document}
\begin{table*}[]
% Table options
% Center the table
\centering
% Stretch the table (vertically)
\renewcommand{\arraystretch}{1.3}
% t: puts the table in two columns format
\begin{threeparttable}
\caption{Elasticity Values}
\label{table:PV_mod_STC_comp2}
\begin{tabularx}{\textwidth}{m{2.5cm}|C|C|
>{\centering\arraybackslash}m{1.2cm}| % changed
>{\centering\arraybackslash}m{1.2cm}} % changed
% or
% \begin{tabularx}{\textwidth}{m{2.5cm}|C|C| M{1.2cm}| M{1.2cm}}
% if you have in preamble definition for M as showed above
\Xhline{0.4mm}
\textbf{Customer type} &
\multicolumn{2}{c|}{\textbf{Set 1}} &
\multicolumn{2}{c|}{\textbf{Set 2}} \\
\Xhline{0.2 mm}
\textbf{Residential} &
$-0.09$ to $-0.5$ &
$-0.12$ to $-1.56$ & $1$ & $2$ \\
\Xhline{0.2 mm}
\textbf{Commercial} &
$-0.019$ to $-0.25$ &
$-0.44$ to $-0.97$ & $3$ & $4$ \\
\Xhline{0.2 mm}
\textbf{Industrial} &
$-0.005$ to $-0.14$ &
$-0.34$ to $-5.48$ & $5$ & $6$ \\
\bottomrule
\end{tabularx}
% Table notes
\begin{tablenotes}
\item[a] This is a table note
\end{tablenotes}
\end{threeparttable}
\end{table*}
\end{document}