如何在 Latex 中将表格的宽度更改为文本宽度?

如何在 Latex 中将表格的宽度更改为文本宽度?

我制作了一个表格,但该表格不适合页面。表格的代码是:

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

如何更改表格的宽度以使其适合页面?

答案1

您的tabular环境目前不允许在标题单元格中换行,原因有两个。首先,五个数据列的类型为c,不允许换行。其次,标题单元格实际上是使用 列l类型排版的,这也不允许换行。

我建议您(a)摆脱\multicolumn{1}{l}“包装器”,(b)使用tabularx环境而不是环境,以及(c)对五个数据列tabular使用居中版本的列类型。X

我还会删除所有垂直线。这样桌子会立刻看起来更加“开放”和吸引人。

在此处输入图片描述

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

相关内容