Automatically adjust the table width to fit the page

tablestabularray

How can I ensure that the table fits the width of the page automatically? I assumed that the specification would take care of it, but it seems not to be the case. Unfortunately, I cannot include actual images to illustrate the problem, so I have used dummy images with enlarged sizes to demonstrate the issue.

In addition, I tried to ensure that the two images in the "Normalized coefficient" row would be of the same size by setting two columns as X[c]. However, it seems that this approach did not work as expected. Below is the MWE of the issue.

enter image description here

\documentclass[journal]{IEEEtran}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{tabularray}
\usepackage{amsmath}
\usepackage{adjustbox}
\usepackage{subfig}


\begin{document}
\lipsum[]

\adjustboxset{width=\linewidth, valign=M}

\onecolumn
\begin{longtblr}{
  width=\textwidth,
  cells = {c},
  hlines,
  colspec= {|c| X[c,m]}
}
1 & X \\
2 & y \\
3 &\begin{tblr}{
width=\linewidth,
colspec={c X[c] X[c]},
cells={},
hlines,
hline{Z}={3pt,blue5},
row{1-Z}={font=\small},
}
C1 & C2 & C3\\
{Normalized \\Coefficient} & \adjincludegraphics{example-image-a}  &   \adjincludegraphics{example-image-b} \\
 $A$ & \SetCell[c=2]{c} \adjincludegraphics{example-image-a}   \\
 $B$ & \SetCell[c=2]{c} \adjincludegraphics{example-image-a}   \\
\end{tblr} \\
\end{longtblr}

\end{document}

Best Answer

  • Not really an answer, but it is to long for the comment ...
  • Your problem is not trivial.
  • With adding hspan=minimal you can fix width of images, but the same time image in multicolumn cells get spurious vertical space )wjic not arrise if these cells contain a text:
\documentclass[journal]{IEEEtran}
\usepackage{lipsum}
\usepackage[caption=false,
            labelfont=sf,textfont=sf,labelformat=simple]{subfig}
\usepackage[export]{adjustbox}

\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{amsmath, booktabs}
\begin{document}
\lipsum[1]
    \begin{table}[ht]
\adjustboxset{width=\linewidth,
              valign=t}
\begin{tblr}{width=\linewidth,
             hline{1-Y} = solid,
             hline{Z}={3pt,fg=blue5},
             cells={font=\small},
             colspec={Q[c,m] *{2}{Q[c, wd=30mm]} @{}},
             cell{X-Z}{2} = {c=2}{j},
             rowsep=3pt,
             hspan=minimal
             }
C1 & C2 & C3\\
{Normalized \\Coefficient} 
    & \adjincludegraphics[valign=m]{example-image-a} & \adjincludegraphics[valign=m]{example-image-b} \\
$A$ & \adjincludegraphics[valign=m]{example-image-a} &    \\
$B$ & \adjincludegraphics[valign=m]{example-image-b} &    \\
$C$ & \lipsum[66]                                 &    \\
\end{tblr}
    \end{table}
\end{document}

enter image description here

It seems that this is caused with some bug, so we need a help of package author.

Addendum: What to do meanwhile?

Not use \tabularray package and rather stick with good old tabularx package:

\documentclass[journal]{IEEEtran}
\usepackage{lipsum}
\usepackage[caption=false,
            labelfont=sf,textfont=sf,labelformat=simple]{subfig}
\usepackage[export]{adjustbox}

\usepackage{colortbl}
   \usepackage{cellspace,   % for adding vertical space around cells' contents
                tabularx}
    \setlength\cellspacetoplimit{0pt}
    \setlength\cellspacebottomlimit{1pt}
\newcommand\mcxx[1]{\multicolumn{2}{>{\hsize=\dimexpr2\hsize+2\tabcolsep\linewidth=\hsize}X @{}}{#1}}
\newlength\colwidth

\usepackage{booktabs}

\begin{document}
\lipsum[1]
    \begin{table}[ht]
\setkeys{Gin}{width=\linewidth}
\settowidth\colwidth{Coefficient}
\begin{tabularx}{\linewidth}{@{} >{\centering}p{\colwidth} *{2}{SX} @{}}
    \toprule
C1 & C2 & C3\\
    \midrule
Normalized Coefficient 
    & \includegraphics[valign=m]{example-image-a}   & \includegraphics[valign=m]{example-image-b} \\
   \midrule
$A$ & \mcxx{\includegraphics[valign=m]{example-image}}  \\
   \midrule
$B$ & \mcxx{\includegraphics[valign=m]{example-image}}  \\
   \arrayrulecolor{blue}
   \bottomrule[3pt]
\end{tabularx}
    \end{table}
\end{document}

enter image description here

Related Question