這是代碼:
\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}