当使用“m{width}”指定列宽时,如何强制水平居中?

当使用“m{width}”指定列宽时,如何强制水平居中?

以下是代码:

\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}}

我借此机会稍微改进了您的表格,将其放置\captionthreeparttable环境中并扩大第一列以使所有单元格都位于一行中。我还提出了一种更好看的布局(在我看来),没有垂直线,水平线更少,用垂直填充代替。

\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,,没有必要使用。\RaggedRightCentering\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}

在此处输入图片描述

相关内容