1

I use the tabularx package and use \newcolumntype command to specify the column width.

However, the text width of the table does not fit the text width of the document after I use the specified column type, i.e., s in the example.

And if I don't use the specified column type, the text with of the table is correct.

The source code and the output PDF is showed as follows,

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

illustration of output

How to make TABLE 1 display as TABLE 2, i.e., the text in the table 1 fit the whole text width of the document?

9
  • 2
    \newcolumntype{s}{>{\hsize=.23\hsize}X} will completely break the tabularx algorithm to determine column widths. Sep 4, 2019 at 16:02
  • Thanks for your comment. So, how to specify the width of a column correctly?
    – Spark
    Sep 4, 2019 at 16:09
  • 2
    The sum of the coefficients of \hsize in the X columns has to be the total number of columns. It seems you try to obtain a certain ratio between the s column type and the X column type, if I'm not mistaken, but I'm not sure which it is. Could you give details?
    – Bernard
    Sep 4, 2019 at 16:32
  • @Bernard Yes, exactly. And I think I have got your idea. For example, if s is set to be 0.23\hsize as above, then the last column should be 1.77\hsize to ensure the sum to be 2X. The code should be like \newcolumntype{s}{>{\hsize=.4\hsize}X} \newcolumntype{L}{>{\hsize=1.6\hsize}X} then \begin{tabularx}{\textwidth}{lslL} will work correctly.
    – Spark
    Sep 4, 2019 at 17:15
  • Yes. The last code says the s column is ¼ of the L column. It's a sort of barycentric coordinates which is used.
    – Bernard
    Sep 4, 2019 at 17:24

1 Answer 1

2

I have got the solution.

As indicated by @Bernard

The sum of the coefficients of \hsize in the X columns has to be the total number of columns.

So the code should be like:

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

Then the following output is obtained: enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .