Search code examples
latextabular

How to improve latex table


In this table, how can I have the upper line that matches between the two tables? I would like to have two tables with the same height. Here is the code that I used to create the table

\documentclass{article}
\usepackage{geometry}
\usepackage{textcomp}
\usepackage{adjustbox}
\usepackage{mathtools}
\usepackage{booktabs} % 
\usepackage[group-separator={,}]{siunitx}
\usepackage{changepage}
\newcommand{\undepth}[1]{%
    \smash[b]{%
        \begin{varwidth}[t]{\linewidth}#1\end{varwidth}
    }%
}
\usepackage{makecell}%To keep spacing of text in tables

\begin{document}

\begin{table}[htbp!]
    \centering
    \footnotesize
    \caption{caption}
    \begin{tabular}{lSSSS}
        \toprule
        \makecell[cc]{column1 \\ second line} & \multicolumn{1}{l}{column2} & \multicolumn{1}{l}{column3} & \multicolumn{1}{l}{column4} & \multicolumn{1}{l}{column5} \\
        \midrule
        A     & 4     & 0     & 0.00 & 4     \\
        B     & 30    & 0     & 0.00 & 30   \\

    \bottomrule
    \end{tabular}
    \quad   
    \footnotesize
    \begin{tabular}{lSS}
        \toprule
        & {Column1.1 } & {Column2.1}\\
        \midrule
        A     & 0.02 & 0.00 \\
        B     & 0.04 & 0.00  \\


        \bottomrule
    \end{tabular}%
\end{table}%
\end{document}

Solution

  • Quick hack:

    Put some invisible dummy header of the same height as in the other table

    \documentclass{article}
    \usepackage{geometry}
    \usepackage{textcomp}
    \usepackage{adjustbox}
    \usepackage{mathtools}
    \usepackage{booktabs} % 
    \usepackage[group-separator={,}]{siunitx}
    \usepackage{changepage}
    \newcommand{\undepth}[1]{%
        \smash[b]{%
            \begin{varwidth}[t]{\linewidth}#1\end{varwidth}
        }%
    }
    \usepackage{makecell}%To keep spacing of text in tables
    
    \begin{document}
    
    \begin{table}[htbp!]
        \centering
        \footnotesize
        \caption{caption}
        \begin{tabular}{lSSSS}
            \toprule
            \makecell[cc]{column1 \\ second line} & \multicolumn{1}{l}{column2} & \multicolumn{1}{l}{column3} & \multicolumn{1}{l}{column4} & \multicolumn{1}{l}{column5} \\
            \midrule
            A     & 4     & 0     & 0.00 & 4     \\
            B     & 30    & 0     & 0.00 & 30   \\
    
        \bottomrule
        \end{tabular}
        \quad   
        \footnotesize
        \begin{tabular}{lSS}
            \toprule
            \makecell[cc]{\mbox{} \\ \mbox{}} & {Column1.1 } & {Column2.1}\\
            \midrule
            A     & 0.02 & 0.00 \\
            B     & 0.04 & 0.00  \\
    
    
            \bottomrule
        \end{tabular}%
    \end{table}%
    \end{document}
    

    enter image description here


    Cleaner solution:

    Use only one tabular

    \documentclass{article}
    \usepackage{geometry}
    \usepackage{textcomp}
    \usepackage{adjustbox}
    \usepackage{mathtools}
    \usepackage{booktabs} % 
    \usepackage[group-separator={,}]{siunitx}
    \usepackage{changepage}
    \newcommand{\undepth}[1]{%
        \smash[b]{%
            \begin{varwidth}[t]{\linewidth}#1\end{varwidth}
        }%
    }
    \usepackage{makecell}%To keep spacing of text in tables
    
    \begin{document}
    
    \begin{table}[htbp!]
        \centering
        \footnotesize
        \caption{caption}
        \begin{tabular}{lSSSSSS}
            \cmidrule[\heavyrulewidth](r){1-5}\cmidrule[\heavyrulewidth](l){6-7}
            \makecell[cc]{column1 \\ second line} & \multicolumn{1}{l}{column2} & \multicolumn{1}{l}{column3} & \multicolumn{1}{l}{column4} & \multicolumn{1}{l}{column5} & {Column1.1 } & {Column2.1} \\
            \cmidrule(r){1-5}\cmidrule(l){6-7}
            A     & 4     & 0     & 0.00 & 4   & 0.02 & 0.00 \\ 
            B     & 30    & 0     & 0.00 & 30   & 0.04 & 0.00  \\ 
            \cmidrule[\heavyrulewidth](r){1-5}\cmidrule[\heavyrulewidth](l){6-7}
        \end{tabular}%
    \end{table}%
    \end{document}
    

    enter image description here