1

I have a document with a few tables. For some of these tables, I am currently using the array package to create a cell that spans through several columns and has its contents in the center of the cell. Unfortunately, the output looks very ugly because the fixed space of my multirow cell does not span evenly throughout the four columns over which my multirow cell is defined. To understand what I’m talking about, consider the following MWE:

\documentclass{standalone}
\usepackage{array}

\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\begin{document}

\begin{tabular}{lcccccccc}
\\
 & \multicolumn{4}{C{175pt}}{Very long name of a fancy dependent variable} & \multicolumn{4}{C{175pt}}{Even longer and fancier name of another dependent variable} \\
\\
 & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8)\\
\end{tabular}

\end{document}

See the (ugly) output here:

enter image description here

I would like the fixed length of my multirow cell to span evenly throughout the four columns over which it is defined. What can I do to make sure that the fixed space of my multirow cells span evenly across the columns over which they are defined? In other words, how can I get numbers (1), (2), (3) and (4) (as well as (5), (6), (7) and (8)) to have the same distance between them?

Whatever your suggested solution is, bear in mind that I have many tables, so a systematic and easy-to-apply solution is most welcome.

Thank you all very much for your time.

2
  • 1
    Will the \multicolumn command always span 4 columns or can the number of columns differ from table to table? What about the width of the \multicolumn? Do they always share the same width or is it possible that it differs, as well?
    – leandriis
    Jun 11, 2021 at 17:23
  • 1

2 Answers 2

2

A place to start from:

enter image description here

\documentclass{standalone}
\usepackage{array}

\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\newlength{\mycolwidth}
\newlength{\multicolumnwidth}
\setlength{\multicolumnwidth}{175pt}
\setlength{\mycolwidth}{\dimexpr(\multicolumnwidth-6\tabcolsep)/4}

\begin{document}

\begin{tabular}{l*{4}{wc{\mycolwidth}}*{4}{wc{\mycolwidth}}}
\\
 & \multicolumn{4}{C{\multicolumnwidth}}{Very long name of a fancy dependent variable} & \multicolumn{4}{C{\multicolumnwidth}}{Even longer and fancier name of another dependent variable} \\
\\
 & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8)\\
\end{tabular}

\end{document}
4
  • I am compiling all my tables in standalone documents, so I am trying to adapt your code to my different tables. In doing so, I am having trouble understanding the following piece of your code: \setlength{\mycolwidth}{\dimexpr(\multicolumnwidth-6\tabcolsep)/4} . Could you please explain what it is and what it does? I’m trying to adapt it to a table in which each multicolumn spans two columns (rather than four), and the table has a total of 5 columns (rather than 9).
    – EoDmnFOr3q
    Jun 11, 2021 at 20:31
  • 1
    @Héctor: \setlength{\mycolwidth}{\dimexpr(\multicolumnwidth-2\tabcolsep)/2} would be suitable for a table in which the multicolumns span only 2 other columns.
    – leandriis
    Jun 11, 2021 at 21:02
  • 1
    The required width of one of n columns is calculated by substracting 2n-2 times the value of \tabcolsep and dividing the result by n (number of columns). The value of \tabcolsep equals teh small amount of horizontal white space to the left and right of a text in a table cell. (This space is easily visible when adding vertical lines to a table.)
    – leandriis
    Jun 11, 2021 at 21:04
  • Thank you for your comments. I was able to figure out by myself via trial and error that \setlength{\mycolwidth}{\dimexpr(\multicolumnwidth-2\tabcolsep)/2} would work for the case in which multicolumns span two columns, but couldn’t figure out the math behind it. I appreciate your clarification.
    – EoDmnFOr3q
    Jun 11, 2021 at 21:17
2

Version 2021L of tabularray package added options hspan=even and vspan=even for distributing extra space evenly for multicolumn and multirow cells, respectively.

Remark: it's incorrect to write \\ at the beginning or double \\'s; tabularray package doesn't support these usages.

\documentclass{article}

\usepackage{tabularray}

\begin{document}

\SetTblrDefault{hspan=even}

\begin{tblr}{
  colspec={lcccccccc},
  cell{1}{2,6} = {c=4}{175pt},
}
 & Very long name of a fancy dependent variable
       &     &     &     & Even longer and fancier name of another dependent variable
                               &     &     &     \\
 & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\
\end{tblr}

\end{document}

enter image description here

You must log in to answer this question.

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