1

How can I merge two columns in one? I mean, that I make the table with 7 columns, but every two of the columns should be merged at the top of the table. And I use only fixed-width columns.

My code is:

documentclass{article}
\setlength{\arrayrulewidth}{0.5mm}
\setlength{\tabcolsep}{18pt}
\renewcommand{\arraystretch}{1.5}
\begin{document}
\begin{table} [h!]
\centering
\setlength{\tabcolsep}{5pt}
\renewcommand{\arraystretch}{1.5}    
\caption{Table of description}
    \newcolumntype{P}[1]{>{\raggedright\arraybackslash}p{#1}}

\begin{tabular}{|P{3cm}|P{2cm}|P{2cm}|P{2cm}|P{2cm}|P{2cm}|P{2cm}|}
\hline
\ name & par & &par& &par& & \\
\hline
\ & & & & & & & \\
\end{document}
0

2 Answers 2

1

Your question title doesn't reflect question content. You probably mean Multicolumn tables, Column merging as follows from question content.

You can merge adjacent cells with use

    \multicolumn<number of columns>}{<align>}{<content>}

or by tabularray equivalent

    \SetCell[c=<number of columns>]{<align>} ...content...

when you use this novel, powerful package for writing of tables.

Edit: I miss that first column in your table is wider than others. Different column widths of X columns can be simple achieved by "change" factor which change the standard column width (at X[l] or X[1,l]]). This factor is equivalent to >{\hsize=<factor>\hsize}X at tabularx tables.

For your example in your case the width of the first column is for 3cm/2cm = 1.5 wider than other columns, so the specification for the first column is X[1.5,l] or X[1.5.l,m].

Now this is corrected:

\documentclass{article}
\usepackage{tabularray}

\begin{document}
    \begin{table} [ht]
    \caption{Table of description}
\begin{tblr}{hlines = 0.5mm, vlines=0.5mm,
             colspec = {X[1.5,l]*{6}{X[l]}},
             }
name    & \SetCell[c=2]{c} par 
            &   &   \SetCell[c=2]{c} par
                    &   &   \SetCell[c=2]{c} par
                            &   \\
1       & 2 & 3 & 4 & 5 & 6 & 7 \\
        &   &   &   &   &   &   \\
\end{tblr}
    \end{table}
or
    \begin{table} [ht]
    \caption{Table of description}
\begin{tblr}{hlines, vlines,
             colspec = {X[1.5,l,m]*{7}{X[l]}},
             row{1} = {font=\bfseries}
             }
name    & \SetCell[c=2]{c} par
            &   &   \SetCell[c=2]{c} par
                    &   &   \SetCell[c=2]{c} par
                            &   \\
text in two lines
       & 2 & 3 & 4 & 5 & 6 & 7 \\
        &   &   &   &   &   &   \\
\end{tblr}
    \end{table}
\end{document}

Above are for equal width of cells used column type X. By using it, table width become equal to \textwidth. tabularray for this columns type define many options as l for left aligning cell's contents, r for the right, c for centering, m for vertical centering etc. This options cab combined as is done in the second example (which I more liked than the first). For details see package documentation.

enter image description here

3
  • Thanks a lot, I just didn't know about this syntax in tables: \begin{tblr}{hlines = 0.5mm, vlines=0.5mm, colspec = {X[1.5,l]*{6}{X[l]}}, } name & \SetCell[c=2]{c} par & & \SetCell[c=2]{c} par & & \SetCell[c=2]{c} par & \\ Dec 24, 2021 at 7:16
  • @Programmer1988, tabularray is relative new package and not yet consider in wiki or other sources how to write tables. BTW, you receive (so far) two answer. Now is time to select one of them which solve your problem on the best way and accept it by clicking o the check mark at top left side of answer. As I see, so far yo not accept no one of received answer, Does any of them help you?
    – Zarko
    Dec 24, 2021 at 7:29
  • Your answer was very helpful and I accepted it as answer :) Dec 24, 2021 at 7:59
1

It's tough to understand what you want. Do you want something like this?

enter image description here

\documentclass{article}

\usepackage{geometry}
\usepackage{array}

\setlength{\arrayrulewidth}{0.5mm}
\newcolumntype{P}[1]{>{\raggedright\arraybackslash}p{#1}}
% \setlength{\tabcolsep}{18pt}

\begin{document}
    \begin{table} [h!]
    \centering
    \setlength{\tabcolsep}{5pt}
    \renewcommand{\arraystretch}{1.5}    
    \caption{Table of description}
    \begin{tabular}{|P{3cm}|*{6}{P{2cm}|}}
        \hline
        name & \multicolumn{2}{c|}{par}  &\multicolumn{2}{c|}{par} &\multicolumn{2}{c|}{par}  \\
        \hline
         & & & & & & \\
         \hline
    \end{tabular}
    \end{table}
\end{document}

To merge two (or more) columns, you can use the \multicolumn{}{}{} command where the first parameter is the no of columns to be merged, 2nd parameter is the horizontal alignment of the column (l, c, or, r), and the 3rd parameter is the content of the column.

Now, there were some errors in your table. You need (n-1) column separators (&) for n columns (you used 7 &). You need the array package to define new columntype. Another concern is that your table width exceeds the textwidth. So, I recommend to decrease some of the columns' width.

Also, if you have multiple same type of columns, you can define them using the command *{n}{<columnType>} where n is the no of columns.

You must log in to answer this question.

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