2

I have 2 tables that It should look similar even the second table has not as many data as the first table. I want my table look like this.

enter image description here

Here is my code

\documentclass{article}

\usepackage{multirow}
\usepackage{booktabs}
\usepackage{tabularx}

\begin{document}

\newcolumntype{Y}{>{\centering\arraybackslash}X}

\begin{table}[h!]
  \centering
    \caption{A. By Layer}
    \label{tab:table1}
    \begin{tabularx}{\textwidth}{@{}l*{10}{Y}@{}}%{l*{10}{X}}
    \midrule
     & \multicolumn{4}{c}{\textbf{Model}} & & \multicolumn{4}{c}{\textbf{Gaggl and Wright}}\\
     & \multicolumn{4}{c}{Layer} & & \multicolumn{4}{c}{Layer}\\
     & 1 & 2 & 3 & 4 & & 1 & 2 & 3 & 4\\
     \cline{2-5}
    \cline{7-10}
    \bfseries{ Moments}\\
     w  & 5 & 5 & 5 & 5 & & 5 & 5 & 5 & 5\\
     s  & 6 & 6 & 6 & 6 & & 6 & 6 & 6 & 6\\
     k/n  & 7 & 7 & 7 & 7 & & 7 & 7 & 7 & 7\\ 
     wb  & 8 & 8 & 8 & 8 & & 8 & 8 & 8 & 8\\ 
    \end{tabularx}
\end{table}

\begin{table}[h!]
  \centering
    \caption{B. Firm-Level}
    \label{tab:table1}
    \begin{tabularx}{\textwidth}{@{}l*{10}{Y}@{}}%{l*{10}{X}}
    \midrule
     & \multicolumn{4}{c}{\textbf{Model}} & & \multicolumn{4}{c}{\textbf{Gaggl and Wright}}\\
     \cline{2-5}
    \cline{7-10}
    \\
    \bfseries{ Moments}\\
     wage   & \multicolumn{4}{c}{a} & & \multicolumn{4}{c}{f}\\
     house  & \multicolumn{4}{c}{b} & & \multicolumn{4}{c}{j}\\
     something e  & \multicolumn{4}{c}{c} & & \multicolumn{4}{c}{l}\\ 
     wb & \multicolumn{4}{c}{d} & & \multicolumn{4}{c}{m}\\ 
    \end{tabularx}
\end{table}

\end{document}

Here is my table

enter image description here

4
  • Welcome to TeX SX! Do you want two tables (with different numbers)?
    – Bernard
    Nov 20, 2019 at 23:12
  • Hi, thank you for the response. I want my underline of table 2 for word 'Model' as long as underline of table 1 foe word 'Model' so it looks nice. Nov 20, 2019 at 23:18
  • Yes,I thought so, but that does not answer my question: must they be distinct tables?
    – Bernard
    Nov 20, 2019 at 23:25
  • yes the tables must be distinct Nov 21, 2019 at 0:06

1 Answer 1

3

It is mandatory to define the same number of columns that you use. You have defined 11 columns but only use 10. The problem is the columns without any data in table 2. They end up too narrow.

In example 1, I have used a fixed width w-column in table 2 and calculated the width manually to line up the rules with the rules in table 1. A ‘brute force’ solution that works if you have a relatively few tabulars, and do not change margin width too often.

In example 2, I have avoided manual ‘eyeballing’ by defining two fixed width columns (Y and Z), where Z is four times wider than Y. The trick here is fixed the width on all columns except the sixth. Since this is an X-column, tabularx advance calculations secure that the table expands to exactly the line width. The number 11 is chosen because you have ten columns, and the first column is wider than the other. Depending on your document’s margins, 10 may work. With standard margins, using 10 in the formula gave overfull hbox-errors.

Both solutions give tables that (hopefully) look like the tables you request. These examples require an updated array package.

Example 1

enter image description here

\documentclass{article}

\usepackage{multirow}
\usepackage{booktabs}
\usepackage{tabularx}

\begin{document}

\newcolumntype{Y}{>{\centering\arraybackslash}X}

\begin{table}[h!]
  \centering
    \caption{A. By Layer}
    \label{tab:table1}
    \begin{tabularx}{\textwidth}{@{}l*{9}{Y}@{}}%{l*{10}{X}}
    \midrule
     & \multicolumn{4}{c}{\textbf{Model}} && \multicolumn{4}{c}{\textbf{Gaggl and Wright}}\\
     & \multicolumn{4}{c}{Layer} & & \multicolumn{4}{c}{Layer}\\
     & 1 & 2 & 3 & 4 & & 1 & 2 & 3 & 4\\
     \cline{2-5} \cline{7-10}
    \bfseries{Moments}&\multicolumn{9}{l@{}}{}\\
     w  & 5 & 5 & 5 & 5 & & 5 & 5 & 5 & 5\\
     s  & 6 & 6 & 6 & 6 & & 6 & 6 & 6 & 6\\
     k/n  & 7 & 7 & 7 & 7 & & 7 & 7 & 7 & 7\\ 
     wb  & 8 & 8 & 8 & 8 & & 8 & 8 & 8 & 8\\ 
    \end{tabularx}
\end{table}

\begin{table}[h!]
  \centering
    \caption{B. Firm-Level}
    \label{tab:table1}
    \begin{tabularx}{\textwidth}{@{}l*{9}{Y}@{}}%{l*{10}{X}}
    \midrule
     & \multicolumn{4}{wc{3.8cm}}{\textbf{Model}} & & \multicolumn{4}{wc{4cm}@{}}{\textbf{Gaggl and Wright}}\\
     \cline{2-5}\cline{7-10}
    \bfseries{Moments}&\multicolumn{9}{l@{}}{}\\
     wage   & \multicolumn{4}{c}{a} && \multicolumn{4}{c}{f}\\
     house  & \multicolumn{4}{c}{b} & & \multicolumn{4}{c}{j}\\
     something e  & \multicolumn{4}{c}{c} & & \multicolumn{4}{c}{l}\\ 
     wb & \multicolumn{4}{c}{d} & & \multicolumn{4}{c}{m}\\ 
    \end{tabularx}
\end{table}

\end{document}

Example 2

enter image description here

\documentclass{article}

\usepackage{booktabs}
\usepackage{tabularx}

\newcolumntype{Y}{wc{\dimexpr((\linewidth/11)-2\tabcolsep)}}
\newcolumntype{Z}{wc{\dimexpr((4\linewidth/11)-2\tabcolsep)}}
\newcommand{\mc}[1]{\multicolumn{4}{Z}{#1}}

\begin{document}

\begin{table}[th!]
  \centering
    \caption{A. By Layer}
    \label{tab:table1}
    \begin{tabularx}{\textwidth}{@{}wl{2cm}*{4}{Y}X*{4}{Y}}
    \midrule
     & \mc{\textbf{Model}} && \mc{\textbf{Gaggl and Wright}}\\
     & \mc{Layer} & & \mc{Layer}\\
     & 1 & 2 & 3 & 4 & & 1 & 2 & 3 & 4\\
     \cmidrule(lr){2-5} \cmidrule(l){7-10}
    \bfseries{Moments}&\multicolumn{9}{l}{}\\
     w  & 5 & 5 & 5 & 5 & & 5 & 5 & 5 & 5\\
     s  & 6 & 6 & 6 & 6 & & 6 & 6 & 6 & 6\\
     k/n  & 7 & 7 & 7 & 7 & & 7 & 7 & 7 & 7\\ 
     wb  & 8 & 8 & 8 & 8 & & 8 & 8 & 8 & 8\\ 
    \end{tabularx}
\end{table}

\begin{table}[th!]
  \centering
    \caption{B. Firm-Level}
    \label{tab:table2}
    \begin{tabularx}{\textwidth}{@{}wl{2cm}*{4}{Y}X*{4}{Y}}
    \midrule
     & \mc{\textbf{Model}} & & \mc{\textbf{Gaggl and Wright}}\\
     \cmidrule(lr){2-5} \cmidrule(l){7-10}
    \bfseries{Moments}&\multicolumn{9}{l}{}\\
     wage              & \mc{a} && \mc{f}\\
     house            & \mc{b} & & \mc{j}\\
     something e  & \mc{c} & & \mc{l}\\ 
     wb                 & \mc{d} & & \mc{m}\\ 
    \end{tabularx}
\end{table}

\end{document}
4
  • Thanks a lot for the answer. How do you now the width is 3.8cm? Is it manually try and error?. By the way, thanks for the answer especially for the word 'Moments'. I just realize that you make multicolumn for the rest unused column too, so it looks proportional. Nov 21, 2019 at 0:09
  • 1
    @Yasrinayuliasari You are right regarding the width of the column. That is eyeballing. If you have lot of tabulars, this is not ideal, but it is the simplest and fastest solution if you have few and do not change page margins considerable. There are more than one possible and simple solutions do avoid this manual step, but it will depend on what type of information you are entering into the columns.
    – Sveinung
    Nov 21, 2019 at 7:56
  • @Yasrinayuliasari I have updated the answer.
    – Sveinung
    Nov 21, 2019 at 9:27
  • Thank you for the second option that more suit for my case. Nov 22, 2019 at 1:15

You must log in to answer this question.

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