Como posso impor a centralização horizontal ao especificar uma largura de coluna com “m{largura}”?

Como posso impor a centralização horizontal ao especificar uma largura de coluna com “m{largura}”?

Aqui está o código:

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

O resultado:

insira a descrição da imagem aqui

Este é um bom resultado, mas usar o comando \Centeringrepetidamente é redundante. Existe uma maneira de impor a centralização automaticamente para todas as células?

Responder1

Apenas use >{\centering\arraybackslash}m{some width}}.

Aproveitei para melhorar um pouco sua tabela, colocando \captiondentro do threeparttableambiente e ampliando a primeira coluna para ter todas as células em uma linha. Proponho também um layout mais bonito (na minha opinião), sem linhas verticais e menos regras horizontais, substituído por um preenchimento vertical.

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

insira a descrição da imagem aqui

Responder2

  • No preâmbulo, exclua \renewcommand \tabularxcolumn [1] { >{ \centering } m{#1} }. Isso é errado. Provavelmente você gosta de ter algo como

    \newcolumntype{M}[1]{>{\Centering} m{#1} }

  • Nas definições de tipo de coluna substitua \begin{tabularx}{1\textwidth}{m{2.5cm}|C|C|m{1.2cm}|m{1.2cm}}por \begin{tabularx}{1\textwidth}{m{2.5cm}|C|C|*{2}{>{\centering\arraybackslash}m{1.2cm}|}}.

Fora do tópico: - geralmente threeparttableestá \caption{...}dentro dele - o uso de \Raggedleft, \RaggedRight, Centeringnas definições de tipo de coluna não é necessário \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}

insira a descrição da imagem aqui

informação relacionada