[Tex/LaTex] How to resize a table to fit the page’s width (without changing font size)

tables

How to resize a table to fit the page's width (without changing font size)?

\documentclass[twocolumn,a4paper]{article}
\usepackage{multirow}
\begin{document}

\begin{table}[h]
\begin{tabular}{c|c|c|c|c}
\hline
 \multirow{2}{*}{Types}  & \multicolumn{4}{c}{Days} \\ \cline{2-5}
                     & Day 1   & Day 2 & Day 3 &  Day 4  \\ \hline 
             Type 1   & 12-06-08-07-09-08-18-32-48-101-108& 13-88-08-07-09-08-14-39-36-10 & 56-33-08-07-09-08-18-32-48&26-29-26\\ \hline 
             Type 2   & 01-07-10-22-55-07-100-32-48& 08-18-32-48-66-36-10 &26-29-26& 10-55-21\\ \hline 

\end{tabular}
\end{table} 
\end{document}

The table exceeds the width of the page:
enter image description here

Desired output:

enter image description here

Best Answer

Since you don't want to reduce the font size, it's necessary to introduce line breaks. You may want to use the tabularx package and its tabularx environment, modifying that package's X column type to centerset the contents of columns 2 through 5.

enter image description here

\documentclass[twocolumn,a4paper]{article}
\usepackage{multirow,tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\begin{document}
\begin{table*} 
\begin{tabularx}{\textwidth}{@{} l|C|C|C|C @{}}
 \hline
 \multirow{2}{*}{Types} & \multicolumn{4}{c}{Days} \\ 
 \cline{2-5}
     & Day 1  & Day 2 & Day 3 &  Day 4  \\ \hline
     Type 1   & 12-06-08-07-09-08-18-32-48-101-108& 13-88-08-07-09-08-14-39-36-10 & 56-33-08-07-09-08-18-32-48&26-29-26\\ \hline
     Type 2   & 01-07-10-22-55-07-100-32-48& 08-18-32-48-66-36-10 &26-29-26& 10-55-21\\
 \hline
\end{tabularx}
\end{table*}
\end{document} 

PS If the strings "Type 1" and "Type 2" need to be centered vertically, just encase them in \multirow{2}{*}{...} statements.


Addendum: You may want to consider giving your table a more "open" or "airy" look. One way to do so would be to get rid of all vertical lines and use the macros of the booktabs package to get well-spaced horizontal lines.

enter image description here

\documentclass[twocolumn,a4paper]{article}
\usepackage{tabularx,booktabs}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\begin{document}
\begin{table*}
\begin{tabularx}{\textwidth}{@{} lCCCC @{}}
 \toprule
 Types & \multicolumn{4}{c@{}}{Days} \\
 \cmidrule(l){2-5}
     & Day 1  & Day 2 & Day 3 &  Day 4  \\
 \midrule
     Type 1   & 12-06-08-07-09-08-18-32-48-101-108& 13-88-08-07-09-08-14-39-36-10 & 56-33-08-07-09-08-18-32-48&26-29-26\\
 \addlinespace
     Type 2   & 01-07-10-22-55-07-100-32-48& 08-18-32-48-66-36-10 &26-29-26& 10-55-21\\
 \bottomrule
\end{tabularx}
\end{table*}
\end{document}