在 tabularx 环境中使用 \newcolumntype 后表格宽度不适合文本宽度!

在 tabularx 环境中使用 \newcolumntype 后表格宽度不适合文本宽度!

我使用 tabularx 包并使用 \newcolumntype 命令来指定列宽。

但是,当我使用指定的列类型后,表格的文本宽度并不适合文档的文本宽度,即s在示例中。

如果我不使用指定的列类型,则表格的文本是正确的。

源代码和输出的PDF如下所示,

\documentclass[10pt,journal,compsoc]{IEEEtran}
\usepackage{tabularx}
\newcolumntype{s}{>{\hsize=.23\hsize}X}

\begin{document}    
\begin{table*}[!t]
    \renewcommand{\arraystretch}{1.5}
    \caption{Table example.}
    \centering
    \begin{tabularx}{\textwidth}{lslX}
        \hline
        column1 & column2   &column3    &column4\\
        \hline
        abc & some text & some text in column3 &some text some text some text some text some text some text some text some text some text some text \\
        aaa &bbbbb  &ccccc  &dddddd\\
        bcd & some text in column2  & some text in column3 & some text some text some text some text some text some text some text some text some text \\
        \hline
    \end{tabularx}
\end{table*}

\begin{table*}[!t]
    \renewcommand{\arraystretch}{1.5}
    \caption{Table example.}
    \centering
    \begin{tabularx}{\textwidth}{lllX}
        \hline
        column1 & column2   &column3    &column4\\
        \hline
        abc & some text & some text in column3 &some text some text some text some text some text some text some text some text some text some text \\
        aaa &bbbbb  &ccccc  &dddddd\\
        bcd & some text in column2  & some text in column3 & some text some text some text some text some text some text some text some text some text \\
        \hline
    \end{tabularx}
\end{table*}
\end{document}

输出说明

如何让表 1 显示为表 2,即表 1 中的文本适合文档的整个文本宽度?

答案1

我已找到解决办法。

正如@Bernard 所指出的

X 列中 \hsize 系数的总和必须是总列数。

所以代码应该是这样的:

\begin{table*}[!t]
    \renewcommand{\arraystretch}{1.5}
    \caption{Table example.}
    \centering
    \begin{tabularx}{\textwidth}{l >{\hsize=.23\hsize}X l >{\hsize=1.77\hsize}X}
        \hline
        column1 & column2   &column3    &column4\\
        \hline
        abc & some text & some text in column3 &some text some text some text some text some text some text some text some text some text some text \\
        aaa &bbbbb  &ccccc  &dddddd\\
        bcd & some text in column2  & some text in column3 & some text some text some text some text some text some text some text some text some text \\
        \hline
    \end{tabularx}
\end{table*}
\begin{table*}[!t]
    \renewcommand{\arraystretch}{1.5}
    \caption{Table example.}
    \centering
    \begin{tabularx}{\textwidth}{lllX}
        \hline
        column1 & column2   &column3    &column4\\
        \hline
        abc & some text & some text in column3 &some text some text some text some text some text some text some text some text some text some text \\
        aaa &bbbbb  &ccccc  &dddddd\\
        bcd & some text in column2  & some text in column3 & some text some text some text some text some text some text some text some text some text \\
        \hline
    \end{tabularx}
\end{table*}

然后得到如下输出: 在此处输入图片描述

相关内容