Best way to merge columns and rows in tables with column separators

  • Topic: LaTeX 
  • Thread starter Thread starter Wrichik Basu
  • Start date Start date
  • Tags Tags
    Column Columns Table
Join the discussion
Registration is free. Start your own thread to ask a follow-up.
0 replies · 4K views
Science Advisor
Insights Author
Gold Member
Messages
2,188
Reaction score
2,694
For merging columns, I generally use the multicolumn command, while for merging rows, I use the multirow command from the multirow package.

The above combination works great if there is no separator in any of the merged columns. If, however, one or more columns have separators, the code breaks.

Consider the following code snippet:
[CODE title="LaTeX"]\begin{table}[h]
\centering
\begin{tabular}{|l|c c c|c c c c|}
\hline
col1 & col2 & col3 & col4 & col5 & col6 & col7 & col8\\
\hline
row1 & j & k & l & x & y & z & aa\\
row2 & \multicolumn{7}{c|}{\multirow{3}{*}{a very long sentence that I want to type}}\\
row3 & & & & & & & \\
row4 & & & & & & & \\
row5 & q & r & s & t & u & v & bb \\
\hline
\end{tabular}
\end{table}[/CODE]
And I get the following output:

1601031345063.png

What I wanted is that the long text will be placed in the middle of rows 2 to 4, and the separator will be removed by at that point.

What ##\LaTeX## is doing instead is that multicolumn is merging all the columns in row2, and then multirow is positioning the text somewhere in the middle. So, the point where the separator is removed is not the place where the text is placed.

I can do this "manually" by positioning the text at the correct position myself instead of using multirow to do that:
[CODE title="LaTeX"]\begin{table}[h]
\centering
\begin{tabular}{|l|c c c|c c c c|}
\hline
col1 & col2 & col3 & col4 & col5 & col6 & col7 & col8\\
\hline
row1 & j & k & l & x & y & z & aa \\
row2 & & & & & & & \\
row3 & \multicolumn{7}{c|}{a very long sentence that I want to type} \\
row4 & & & & & & & \\
row5 & q & r & s & t & u & v & bb \\
\hline
\end{tabular}
\end{table}[/CODE]
This gives the following table:
1601032078511.png
which is what I wanted.

Is there any better way of merging rows and columns in these cases?