0

I have the following table, and I can't seem to get it to distribute evenly to columns. What would be the easiest way to do this?

Other problems seem to suggest p{( 1/# of cols )\linewidth} , but this doesn't seem to work for me

\begin{table}[t]
\begin{tabular}{p{0.14\linewidth}|p{0.14\linewidth}p{0.14\linewidth}|p{0.14\linewidth}p{0.14\linewidth}|p{0.14\linewidth}p{0.14\linewidth}}
% some data that's very long                   
\end{tabular}
\end{table}

For some reason, .14, which is approximately 1/7, is far too short of line width and .15 is also too short. Is there any automatic way to scale this?

This is in the CVPR 2022 Paper Template, for reference.

3
  • Most other answers seem to revolve around p{a \linewidth}, but this was not working for me, so I thought I would post in case I'm doing something systemically wrong.
    – envyul
    May 11, 2023 at 9:19
  • 2
    You can use tabularray or tabularx package. X column type is what you need.
    – Clara
    May 11, 2023 at 9:19
  • 2
    If you provided an example that is not working someone would debug, but as it is, it is impossible to say. As long as there is at least one non empty entry a .14\linewidth column will be that wide so 7 of them will be 0.98\linewidth+14\tabcolsep+6\arrayrulewidth which is not far too short, and may be too wide depending on details you have not shown May 11, 2023 at 10:11

2 Answers 2

3

With tabularx:

\documentclass{article}
\usepackage{tabularx}

\begin{document}
\begin{table}[ht]
\begin{tabularx}{\linewidth}{X|
                            XX|
                            XX|
                            XX}
\hline
1   & 2 & 3 & 4 & 5 & 6 & 7 \\
\hline 
\end{tabularx}
\end{table}
\end{document}

enter image description here

(red lines indicate page layout)

or with tblr of tabularray package:

\documentclass{article}
\usepackage{tabularray}

\begin{document}
\begin{table}[ht]
\begin{tblr}{hlines,
             colspec = {X|XX|XX|XX}
             }
1   & 2 & 3 & 4 & 5 & 6 & 7 \\
\end{tblr}
\end{table}
\end{document}

enter image description here

Result are almost the same. Differences are in the vertical space around cells contents.

0

You need to take into account also the padding between columns.

\documentclass{article}
\usepackage{array}% recommended

\usepackage{tabularx}% for the second realization

\usepackage{showframe}% for showing the page boundaries

\newlength{\mycolwd}

\begin{document}

\begin{table}[htp]

% we want 7 columns, so we subtract twelve \tabcolsep 
% and 3 rule widths from the column width
% and divide by seven
\setlength{\mycolwd}{%
  \dimexpr(\columnwidth-12\tabcolsep-3\arrayrulewidth)/7\relax
}

\begin{tabular}{
  @{}% no padding at the left
  p{\mycolwd}|p{\mycolwd}
  p{\mycolwd}|p{\mycolwd}
  p{\mycolwd}|p{\mycolwd}
  p{\mycolwd}
  @{}% no padding at the right
}
aa b c dd eee f &
aa b c dd eee f &
aa b c dd eee f &
aa b c dd eee f &
aa b c dd eee f &
aa b c dd eee f &
aa b c dd eee f \\
\end{tabular}

\caption{First way}

\end{table}

\begin{table}[htp]

\begin{tabularx}{\columnwidth}{
  @{}% no padding at the left
  X|X
  X|X
  X|X
  X
  @{}% no padding at the right
}
aa b c dd eee f &
aa b c dd eee f &
aa b c dd eee f &
aa b c dd eee f &
aa b c dd eee f &
aa b c dd eee f &
aa b c dd eee f \\
\end{tabularx}

\caption{Second way}

\end{table}

\end{document}

You see that the second way needs no computation.

enter image description here

You must log in to answer this question.

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