[Tex/LaTex] How to change the width of the table to the text width in Latex

tableswidth

I made a table, but the table does not fit on the page. The code for the table is:

\begin{table}[H]
        \centering
        \label{my-label}
        \begin{tabular}{|l|c|c|c|c|c|}
            \hline
            & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{} & \multicolumn{1}{l|}{} \\ \hline
                         & x                                             & 0                                                & 0                                                         & 0                                              & 1                                               \\ \hline
                      & 1                                             & x                                                & 0                                                         & 0                                              & 1                                               \\ \hline
             & 1                                             & 1                                                & x                                                         & 1                                              & 1                                               \\ \hline
                       & 1                                             & 1                                                & 0                                                         & x                                              & 1                                               \\ \hline
                      & 0                                             & 0                                                & 0                                                         & 0                                              & x                                               \\ \hline
            & 3                                             & 2                                                & 0                                                         & 1                                              & 4                                               \\ \hline
                                    & 4                                             & 3                                                & 1                                                         & 2                                              & 5                                               \\ \hline
        \end{tabular}
    \end{table}

How do I change the width of the table so that it fits on the page?

Best Answer

Your tabular environment is currently not allowing line breaks in the header cells, for two reasons. First, the five data columns are of type c, which doesn't allow line breaks. Second, the header cells are actually typeset using the l column type, which doesn't allow line breaks either.

I suggest you (a) get rid of the \multicolumn{1}{l} "wrappers", (b) use a tabularx environment instead of a tabular environment, and (c) use a centered version of the X column type for the five data columns.

I would also get rid of all vertical rules. That'll immediately give the table a more "open" and inviting look.

enter image description here

\documentclass{article}
\usepackage[letterpaper,margin=1in]{geometry} % choose page size parameters suitable
\usepackage{float,tabularx} 
\newcolumntype{C}{>{\centering\arraybackslash}X} % centered version of "X" column type
\begin{document}
\begin{table}[H]
\setlength\extrarowheight{2pt} % create a more open look
\setlength\tabcolsep{3pt}      % default value: 6pt
\caption{Being cool} \label{my-label}
\begin{tabularx}{\textwidth}{@{} l *{5}{C} @{}}
\hline
& Cool as much as possible 
& The COP as high as possible 
& Have as much input power as possible 
& Cool as quick as possible 
& System as safe as possible 
\\ \hline
Cool as much as possible             & x & 0 & 0 & 0 & 1 \\ \hline
The COP as high as possible          & 1 & x & 0 & 0 & 1 \\ \hline
Have as much input power as possible & 1 & 1 & x & 1 & 1 \\ \hline
Cool as quick as possible            & 1 & 1 & 0 & x & 1 \\ \hline
System as safe as possible           & 0 & 0 & 0 & 0 & x \\ \hline
                                     & 3 & 2 & 0 & 1 & 4 \\ \hline
Weight factor                        & 4 & 3 & 1 & 2 & 5 \\ \hline
\end{tabularx}
\end{table}
\end{document}