코드는 다음과 같습니다.
\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}|}}
.
주제에서 벗어남: - in 은 일반적으로 그 안에 threeparttable
있습니다 . - 열 유형 정의에서 , , 를 사용할 필요는 없습니다 .\caption{...}
\Raggedleft
\RaggedRight
Centering
\arraybackslash
MWE:
\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}