Как обеспечить горизонтальное центрирование при указании ширины столбца с помощью «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}}.

Я воспользовался возможностью немного улучшить вашу таблицу, поместив ее \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}

введите описание изображения здесь

Связанный контент