Assume that we have a table and we want LaTeX to automatically determine its columns' width provided that column widths are equal. Such a problem can be solved by defining a new column type as follows: \newcolumntype{C}{ >{ \arraybackslash \Centering } X }
. However, when using such command, LaTeX will make column widths equal, but this may result in suboptimal output like this:
Which is generated using the following code:
\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}
An optimized code should result in something like this:
Which is generated by replacing the last two columns with a constant width of 1cm each. However, I want LaTeX to automatically make the last two columns small (because their contents are small) while forcing both columns to be equal. In other words, I want to define a new column type, say its name is NewCol
, and use it in the table command \begin{tabularx}{1\textwidth}{ D{2.6cm}@{\quad} |C|C| NewCol|NewCol }
. NewCol
here should result in columns of equal widths but different in size from the column type C
. The widths of both NewCol
and C
should be aumatically determined according to the contents of columns and the allowable length defined in the syntax.