宽度相等的表格列

宽度相等的表格列

我尝试使下方列的宽度Very very long name相同(G1 和 G2)。顶部的列使用 创建tabular,而另一个列使用 创建tabularx在此处输入图片描述 我见过类似的问题,但无法将其解决方案应用到我的情况。

\documentclass{article}

\usepackage{graphicx}
\usepackage{multirow}
\usepackage{tabularx}


\begin{document}

\begin{table}[ht]
    \centering
    \resizebox{\textwidth}{!}{
    \renewcommand{\arraystretch}{1.5} % Default value: 1
    \begin{tabular}{lcccccc}
            \multicolumn{7}{c}{\textbf{ABC Metrics}} \\ \hline
            \multicolumn{1}{|l|}{\textbf{Algorithm}} & \multicolumn{2}{c|}{\textit{Very very long name}} & \multicolumn{2}{c|}{\textit{Very very long name}} & \multicolumn{2}{c|}{\textit{Very very long name}} \\ \hline
            \multicolumn{1}{|l|}{\textbf{Graph}} & \multicolumn{1}{c|}{$G_1$} & \multicolumn{1}{c|}{$G_2$} & \multicolumn{1}{c|}{$G_1$} & \multicolumn{1}{c|}{$G_2$} & \multicolumn{1}{c|}{$G_1$} & \multicolumn{1}{c|}{$G_2$} \\ \hline
    \end{tabular}}
\end{table}


\begin{tabularx}{\textwidth}{XXXXXXX}
            \multicolumn{7}{c}{\textbf{ABC Metrics}} \\ \hline
            \multicolumn{1}{|l|}{\textbf{Algorithm}} & \multicolumn{2}{c|}{\textit{Very very long name}} & \multicolumn{2}{c|}{\textit{Very very long name}} & \multicolumn{2}{c|}{\textit{Very very long name}} \\ \hline
            \multicolumn{1}{|l|}{\textbf{Graph}} & \multicolumn{1}{c|}{$G_1$} & \multicolumn{1}{c|}{$G_2$} & \multicolumn{1}{c|}{$G_1$} & \multicolumn{1}{c|}{$G_2$} & \multicolumn{1}{c|}{$G_1$} & \multicolumn{1}{c|}{$G_2$} \\ \hline
\end{tabularx}

\end{document}

答案1

如果您加载geometry,您将获得更合理的边距。默认设计用于边距注释,因此如果您不使用它们,您将在行上获得更多空间。

请注意,您不应将\resizebox其与表格一起使用,因为这会导致字体大小不一致。最后,我不明白\multicolumn{1}{c|}代码中所有这些是干什么用的,因为您可以将X单元格的内容居中?

以下是带有单元格的代码X

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{graphicx}
\usepackage{ragged2e}
\usepackage{tabularx}

\begin{document}

\begin{table}[!ht]
    \centering
    \renewcommand{\arraystretch}{1.5} % 
    \setlength{\tabcolsep}{4pt}
\begin{tabularx}{\textwidth}{|>{\hsize=1.72\hsize\bfseries\RaggedRight}X|*{6}{>{\centering\arraybackslash\hsize=0.88\hsize}X|}}
            \multicolumn{7}{c}{\textbf{ABC Metrics}} \\[1ex] \hline
            \textbf{Algorithm}& \multicolumn{2}{c|}{\textit{Very very long name}} & \multicolumn{2}{c|}{\textit{Very very long name}} & \multicolumn{2}{c|}{\textit{Very very long name}} \\
            \hline%
            \textbf{Graph} & $G₁$ & $G₂$ & $G₁$ & $G₂$ & $G₁$ & $G₂$ \\ 
            \hline%
            \textbf{Weighted Ortho\-gonal Ordering} & $G₁$ & $G₂$ & $G₁$ & $G₂$ & $G₁$ & $G₂$ \\ \hline
\end{tabularx}
\end{table}

\end{document}

在此处输入图片描述

答案2

哪个表格更容易阅读?第一个?第二个?

在此处输入图片描述

\documentclass{article}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcommand\mcxit[1]{\multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep}C|}{\textit{#1}}}

\begin{document}

\begin{table}[ht]
    \centering
    \tiny
    \renewcommand{\arraystretch}{1.2} % Default value: 1
\begin{tabularx}{\linewidth}{|>{\bfseries}l|*{6}{C|}}
\multicolumn{7}{c}{\textbf{ABC Metrics}} \\[1ex] \hline
Algorithm   & \mcxit{Weighted Orthogonal Ordering}
            & \mcxit{Weighted Orthogonal Ordering}
            & \mcxit{Weighted Orthogonal Ordering}       \\
    \hline%
Graph       & $G_1$ & $G_2$ & $G_1$ & $G_2$ & $G_1$ & $G_2$ \\ \hline
\end{tabularx}
\end{table}

\begin{table}[ht]
    \centering
    \renewcommand{\arraystretch}{1.2} % Default value: 1
\begin{tabularx}{\linewidth}{|>{\bfseries}l|*{6}{C|}}
\multicolumn{7}{c}{\textbf{ABC Metrics}} \\[1ex] \hline
Algorithm   & \mcxit{Weighted Orthogonal Ordering}
            & \mcxit{Weighted Orthogonal Ordering}
            & \mcxit{Weighted Orthogonal Ordering}       \\
    \hline%
Graph       & $G_1$ & $G_2$ & $G_1$ & $G_2$ & $G_1$ & $G_2$ \\ \hline
\end{tabularx}
\end{table}

\end{document}

在这两种情况下,正如您从 mwe 中看到的,都使用了您的页面布局(默认article)。但是,如果您增加\textwidth使用包geometry,请按照建议伯纳德在他的回答中:

\documentclass{article}
\usepackage[margin=20mm]{geometry}
\usepackage{tabularx}
% follows the same code as in mwe above

该表看起来好多了:

在此处输入图片描述

从上表图像可以看出,第二个是合理的折衷方案。

编辑: 一个可能的解决方案是允许表格从右文本边框溢出。例如使用包changepage

\documentclass{article}
\usepackage{changepage}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcommand\mcxit[1]{\multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep}C|}{\textit{#1}}}

%-------------------------------------- only for show page layout
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.25pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}

\begin{table}[ht]
\begin{adjustwidth*}{0pt}{-\dimexpr\marginparsep+\marginparwidth}
    \small
    \renewcommand{\arraystretch}{1.2} % Default value: 1
\begin{tabularx}{\linewidth}{|>{\bfseries}l|*{6}{C|}}
\multicolumn{7}{c}{\textbf{ABC Metrics}} \\[1ex] \hline
Algorithm   & \mcxit{Weighted Orthogonal Ordering}
            & \mcxit{Weighted Orthogonal Ordering}
            & \mcxit{Weighted Orthogonal Ordering}       \\
    \hline%
Graph       & $G_1$ & $G_2$ & $G_1$ & $G_2$ & $G_1$ & $G_2$ \\ \hline
\end{tabularx}
\end{adjustwidth*}
\end{table}
\end{document}

结果列标题文本仍然只有两行:

在此处输入图片描述

(红线表示页面布局)。

答案3

一种可能的解决方案如下tabularx

\documentclass{article}

\usepackage{ragged2e}
\usepackage{tabularx}
\newcolumntype{Y}{>{\Centering\arraybackslash}X}
\newcolumntype{Z}[1]{>{\Centering\arraybackslash\hsize=#1\hsize}X}

\begin{document}

\begin{tabularx}{\textwidth}{|l|Y|Y|Y|Y|Y|Y|}
  \multicolumn{7}{c}{\textbf{ABC Metrics}} \\ 
  \hline
  \textbf{Algorithm} & \multicolumn{2}{Z{2.3}|}{\textit{Very very long name}} & \multicolumn{2}{Z{2.3}|}{\textit{Very very long name}} & \multicolumn{2}{Z{2.3}|}{\textit{Very very long name}} \\ 
  \hline
  \textbf{Graph} & $G_1$  & $G_2$ & $G_1$ & $G_2$ & $G_1$ & $G_2$ \\ \hline
\end{tabularx}

\end{document}

在此处输入图片描述

在上面的例子中,我删除了多余的\multicolumn命令。为了使列内容水平居中X,我定义了新的列类型。对于多列,我用额外定义的列类型Y替换了列类型,这些列基于列,因此允许自动换行以使表格适合文本宽度。cZX

相关内容