如何自動均衡內容相似的列的寬度?

如何自動均衡內容相似的列的寬度?

假設我們有一個表格,並且希望 LaTeX 自動確定其列寬,前提是列寬相等。這樣的問題可以透過定義一個新的列類型來解決,如下所示\newcolumntype{C}{ >{ \arraybackslash \Centering } X }:然而,當使用這樣的命令時,LaTeX 將使列寬相等,但這可能會導致輸出不理想,如下所示:

在此輸入影像描述

它是使用以下程式碼產生的:

\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}
\newcolumntype{A}[1]{ >{ \arraybackslash \RaggedRight } m{#1} }
\newcolumntype{S}[1]{ >{ \arraybackslash \Centering } m{#1} }
\newcolumntype{D}[1]{ >{ \arraybackslash \RaggedLeft } m{#1} }
\newcolumntype{F}[1]{ >{ \arraybackslash \justifying } m{#1} }
\usepackage{multirow}



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


        \begin{tabularx}{1\textwidth}{ D{2.6cm}@{\quad} |C|C|C|C }



            \toprule


            \multirow{2}{*}{
                                \textbf{Customer type}
                            } & 
            \multicolumn{2}{c}{
                                    \multirow{2}{*}{\textbf{Set 1}}
                                }
            &
            \multicolumn{2}{|c}{
                                \multirow{2}{*}{\textbf{Set 2}}
                            }
            \\



            \multicolumn{1}{c|}{} &
            \multicolumn{2}{c}{} &
            \multicolumn{2}{|c}{}
            \\
            \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}



\end{table*}

\end{document}

優化後的程式碼應該如下所示:

在此輸入影像描述

它是透過將最後兩列替換為每列 1 公分的恆定寬度而產生的。但是,我希望 LaTeX 會自動使最後兩列變小(因為它們的內容很小),同時強制兩列相等。換句話說,我想定義一個新的列類型,稱其名稱為NewCol,並在表格命令中使用它\begin{tabularx}{1\textwidth}{ D{2.6cm}@{\quad} |C|C| NewCol|NewCol }NewCol這裡應該產生寬度相等但大小與列類型不同的列CNewCol和的寬度C應根據列的內容和語法中定義的允許長度自動決定。

答案1

此解決方案測量組合列,然後將這些寬度用於表格。缺點是您必須輸入兩次資料。請注意,第 2 組的標題比資料寬。

我做了一些無端的更改,主要是為了簡化程式碼。

\documentclass{report}

\usepackage{amsmath}
\usepackage{amssymb, amsfonts}
\usepackage[flushleft]{threeparttable}
\usepackage{makecell}
\usepackage{ragged2e} 
\usepackage{multirow}
% none of the above are used here

\usepackage{booktabs}
%\usepackage{array}% already loaded
\newcolumntype{C}[1]{ >{\hfil} m{#1} }
\newcolumntype{D}[1]{ >{\hfill} m{#1} }
\newcommand{\bigstrut}{\rule[\dimexpr -\arraystretch\dp\strutbox-1ex]{0pt}{0pt}%
  \rule{0pt}{\dimexpr \arraystretch\ht\strutbox+1ex}}

\begin{document}

\begin{table*}[]
\sbox0{\begin{tabular}{@{}l@{}}% measure set 1
  $-0.09$ to $-0.5$\\
  $-0.12$ to $-1.56$\\
  $-0.019$ to $-0.25$\\
  $-0.44$ to $-0.97$\\
  $-0.005$ to $-0.14$\\
  $-0.34$ to $-5.48$
\end{tabular}}
\sbox1{\begin{tabular}{@{}l@{}}% measure set 2
  1\\
  2\\
  3\\
  4\\
  5\\
  6
\end{tabular}}
    % Table options
    \caption{Elasticity Values}
    \label{table:PV_mod_STC_comp2}
    % Center the table
    \centering
    % Stretch the table (vertically)
    \renewcommand{\arraystretch}{1.3}
        \begin{tabular}{ D{2.7cm}@{\quad} |C{\wd0}|C{\wd0}|C{\wd1}|C{\wd1}}
            \toprule
            \bigstrut\textbf{Customer type} &
            \multicolumn{2}{c}{\bigstrut\textbf{Set 1}} &
            \multicolumn{2}{|c}{\bigstrut\textbf{Set 2}} \\
            \midrule
            Residential & $-0.09$ to $-0.5$   & $-0.12$ to $-1.56$  & 1 & 2 \\
            \midrule
            Commercial  & $-0.019$ to $-0.25$ & $-0.44$ to $-0.97$  & 3 & 4 \\
            \midrule
            Industrial  & $-0.005$ to $-0.14$ & $-0.34$ to $-5.48$  & 5 & 6 \\
            \bottomrule
        \end{tabular}
\end{table*}
\end{document}

示範

答案2

像這樣?我曾經>{\hsize=xₖ\hsize}X調整列的寬度X,係數的條件xₖ是它們的總和等於 X 列的數量(這作為重心座標系統):

\documentclass{report}
\usepackage{amsmath}
\usepackage{amssymb, amsfonts}
\usepackage[flushleft]{threeparttable}
\usepackage{makecell, caption}
\captionsetup{skip=6pt}
\usepackage{ragged2e}
\usepackage{tabularx}
\newcolumntype{L}{ >{ \arraybackslash \RaggedRight } X } % left justified
\newcolumntype{C}{ >{ \arraybackslash \Centering\hsize=1.6\hsize} X } % centered
\newcolumntype{y}{ >{ \arraybackslash \Centering\hsize=0.4\hsize} X } % centered
\newcolumntype{R}{ >{ \arraybackslash \RaggedLeft } X } % right justified
\newcolumntype{J}{ >{ \arraybackslash \justifying } X } % justified
\renewcommand \tabularxcolumn [1] { >{ \centering } m{#1} }
\usepackage{booktabs}
\newcolumntype{A}[1]{ >{ \arraybackslash \RaggedRight } m{#1} }
\newcolumntype{S}[1]{ >{ \arraybackslash \Centering } m{#1} }
\newcolumntype{D}[1]{ >{ \arraybackslash \RaggedLeft } m{#1} }
\newcolumntype{F}[1]{ >{ \arraybackslash \justifying } m{#1} }
\usepackage{multirow}

\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}
        \begin{tabularx}{1\textwidth}{ D{2.6cm}@{\quad} |C|C|y|y }
            \toprule
            \multirow{2}{*}{
                                \textbf{Customer type}
                            } &
            \multicolumn{2}{c}{
                                    \multirow{2}{*}{\textbf{Set 1}}
                                }
            &
            \multicolumn{2}{|c}{
                                \multirow{2}{*}{\textbf{Set 2}}
                            }
            \\
            \multicolumn{1}{c|}{} &
            \multicolumn{2}{c}{} &
            \multicolumn{2}{|c}{}
            \\
            \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}
\end{table*}

\end{document} 

在此輸入影像描述

答案3

tabularxX 列(tabulary這更接近您的要求),就像標準表格p列一樣,旨在允許斷線在列中達到指定或計算的寬度,但您幾乎不希望在數字資料表中自動換行。標準tabularl、r、c 列會自動調整為列內容的寬度。

在此輸入影像描述

\documentclass{report}

\usepackage{amsmath}
\usepackage{amssymb, amsfonts}
\usepackage[flushleft]{threeparttable}
\usepackage{makecell}


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


\begin{tabular}{>\bfseries rcccc }
           \toprule
Customer type&
\multicolumn{2}{c}{\textbf{Set 1}}&
\multicolumn{2}{c}{\textbf{Set 2}}\\
\midrule
Residential&
            $-0.09$ to $-0.5$ &
            $-0.12$ to $-1.56$ & $1$ & $2$
            \\
\midrule
 Commercial & $-0.019$ to $-0.25$ & $-0.44$ to $-0.97$ & $3$ & $4$
            \\
\midrule
Industrial & $-0.005$ to $-0.14$ & $-0.34$ to $-5.48$ & $5$ & 
            $6$
            \\
            \bottomrule
\end{tabular}

\end{table*}

\end{document}

答案4

你需要的是一個tabulary.列的寬度tabulary與每列中最長條目的自然寬度成正比。在這裡,您不需要手動計算任何東西。允許的標頭是LCJR,這三個標L,C,R頭正如其名稱所示,與J普通標頭一樣p,全部大寫。

筆記:

  • 使用 時\multicolumns,組合列的寬度不應超過其單獨對應列的自然寬度。觀察\makebox[.7em]{$1$}我曾經稍微增加了寬度。此外,普通字母c,l,r,p也用於組合列。
  • 要增加第一行的高度, \parbox{0pt}{\rule{0pt}{1cm}}可以簡單地做到這一點。
  • \toprule最後,垂直線不應該與或一起使用\bottomrule,它們會在水平線下方/上方產生一個小間隙。

-

\documentclass{report}

\usepackage{amsmath}
\usepackage{amssymb, amsfonts}
\usepackage[flushleft]{threeparttable}
\usepackage{makecell,tabulary}

\usepackage{ragged2e} 
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{multirow}    
\begin{document}

\begin{table*}    % [ ] Table options
  \caption{Elasticity Values}
  \label{table:PV_mod_STC_comp2}    
  \centering                                                % Center the table    
  \renewcommand{\arraystretch}{1.3} % Stretch the table (vertically)
  \begin{tabulary}{\textwidth}{ R|C|C|C|C}
    \Xhline{1pt}
    \textbf{Customer type}\parbox{0pt}{\rule{0pt}{1cm}}&\multicolumn{2}{c|}{\textbf{Set 1}} & \multicolumn{2}{c}{\textbf{Set 2}}\\
    \hline\textbf{Residential} & $-0.09$  to $-0.5$  & $-0.12$ to $-1.56$ & \makebox[.7em]{$1$} & $2$\\
    \hline\textbf{Commercial}  & $-0.019$ to $-0.25$ & $-0.44$ to $-0.97$ & $3$ & $4$   \\
    \hline\textbf{Industrial}  & $-0.005$ to $-0.14$ & $-0.34$ to $-5.48$ & $5$ & $6$   \\
    \Xhline{1pt}
  \end{tabulary}
\end{table*}

\end{document}

在此輸入影像描述

相關內容