TransWikia.com

How to add sub header to a header

TeX - LaTeX Asked on May 23, 2021

I currently have a table with 8 columns and 5 rows that I would like to add a sub column that goes from C4 to C6. For instance, this the code below of my table

usepackage{adjustbox}
    begin{table}[ht]
        centering
        begin{adjustbox}{width=1textwidth,center=textwidth}
        small
        begin{tabular}{rrrrrrrr}
          hline
         c1 & c2 & c3 & c4  & c5 & c6 & c7 & c8  
          hline
        r1 & 8.95e-04 & 7.35e-04 & 4.78e-06 &  10 & 0.52 & 0.67 & 0.30  
           r2 & 7.08e-21 & 2.09e-02 & 1.70e-26 &  54 & 0.55 & 0.64 & 0.25  
          r3 & 4.94e-04 & 5.50e-02 & 3.52e-06 &  17 & 0.38 & 0.61 & 0.13  
          r4 & 1.16e-06 & 6.65e-02 & 5.54e-08 &  10 & 0.52 & 0.62 & 0.28  
           r5 & 1.27e-05 & 8.61e-02 & 5.20e-07 &  10 & 0.53 & 0.66 & 0.27  
           hline
        end{tabular}
        end{adjustbox}
        caption{table example} 
        end{table} 

The output is this:table1

Hwoever, what I need to do is add a header on top of C4, C5, C6 and push C4, C5, C6 downwards. An illustraion of what I am trying to achieve:

table2

Where top-column is aligned with the other columns and the respective columns are under the top-column

2 Answers

Here you go, using multicolumn:

documentclass{article}
usepackage{adjustbox}
usepackage{booktabs}
begin{document}
    begin{table}[ht]
        centering
        begin{adjustbox}{width=1textwidth,center=textwidth}
        small
        begin{tabular}{rrrrrrrr}
        toprule
            c1 &       c2 &       c3 &                     c4 & c5 &   c6 &   c7 &   c8 
        cmidrule(lr){4-6}
               &          &          & multicolumn3r{c4--c6}             &      &      
        midrule
            r1 & 8.95e-04 & 7.35e-04 &               4.78e-06 & 10 & 0.52 & 0.67 & 0.30 
            r2 & 7.08e-21 & 2.09e-02 &               1.70e-26 & 54 & 0.55 & 0.64 & 0.25 
            r3 & 4.94e-04 & 5.50e-02 &               3.52e-06 & 17 & 0.38 & 0.61 & 0.13 
            r4 & 1.16e-06 & 6.65e-02 &               5.54e-08 & 10 & 0.52 & 0.62 & 0.28 
            r5 & 1.27e-05 & 8.61e-02 &               5.20e-07 & 10 & 0.53 & 0.66 & 0.27 
        bottomrule
        end{tabular}
        end{adjustbox}
        caption{table example} 
        end{table} 
end{document}

enter image description here

multicolumn{number of columns}{alignment}{text} is probably self-explanatory. You can leave out the braces if your arguments are only a single token, which is what I tend to do --- hence I wrote multicolumn3r instead of multicolumn{3}{r} here. Functionally, they're identical.

I've also used the rules provided by the booktabs package to improve the overall look of the table.

BTW, for minus signs you may want to use $-$ instead of -; the latter you'll get a dash, which is not the same.

Correct answer by chsk on May 23, 2021

As mentioned before, the multicolumn command can be used for a shared column header. The following MWE also contains some off-topic suggestions with which you can improve your table: For the horizontal lines, I used commands from the booktabs package. In order to improve the alignment of numbers in the table columns, I used the S type column from the siunitx package. Since the use of adjustbox on a table inevitably leads to inconsistent font sizes throughout the document, something you might want to avoid, I removed the adjustbox environment entirely. The table should already be narrow enought to fit into the available textwidth. If you nevertheless insist on making the table exactly as wide as the textwidth, you might want to use tabular*:

enter image description here

documentclass{article}
usepackage{booktabs}
usepackage{siunitx}
begin{document}


begin{table}[ht]
    centering
    caption{table example}
    begin{tabular}{r*{3}{S[table-format=1.2e-2]}S[table-format=2]*{3}{S[table-format=1.2]}}
      toprule
      c1 & {c2}     & {c3}     &multicolumn{3}{c}{common header} & {c7} & {c8}  cmidrule{4-6}
         &          &          & {c4}     & {c5}& {c6} &      &     
      midrule
      r1 & 8.95e-04 & 7.35e-04 & 4.78e-06 &  10 & 0.52 & 0.67 & 0.30  
      r2 & 7.08e-21 & 2.09e-02 & 1.70e-26 &  54 & 0.55 & 0.64 & 0.25  
      r3 & 4.94e-04 & 5.50e-02 & 3.52e-06 &  17 & 0.38 & 0.61 & 0.13  
      r4 & 1.16e-06 & 6.65e-02 & 5.54e-08 &  10 & 0.52 & 0.62 & 0.28  
      r5 & 1.27e-05 & 8.61e-02 & 5.20e-07 &  10 & 0.53 & 0.66 & 0.27  
      bottomrule
    end{tabular} 
end{table} 


begin{table}[ht]
    sisetup{output-exponent-marker=ensuremath{mathrm{E}}}
    centering
    caption{table example}
    begin{tabular}{r*{3}{S[table-format=1.2e-2]}S[table-format=2]*{3}{S[table-format=1.2]}}
      toprule
      c1 & {c2}     & {c3}     &multicolumn{3}{c}{common header} & {c7} & {c8}  cmidrule{4-6}
         &          &          & {c4}     & {c5}& {c6} &      &     
      midrule
      r1 & 8.95e-04 & 7.35e-04 & 4.78e-06 &  10 & 0.52 & 0.67 & 0.30  
      r2 & 7.08e-21 & 2.09e-02 & 1.70e-26 &  54 & 0.55 & 0.64 & 0.25  
      r3 & 4.94e-04 & 5.50e-02 & 3.52e-06 &  17 & 0.38 & 0.61 & 0.13  
      r4 & 1.16e-06 & 6.65e-02 & 5.54e-08 &  10 & 0.52 & 0.62 & 0.28  
      r5 & 1.27e-05 & 8.61e-02 & 5.20e-07 &  10 & 0.53 & 0.66 & 0.27  
      bottomrule
    end{tabular} 
end{table} 

begin{table}[ht]
    centering
    caption{table example}
    begin{tabular*}{linewidth}{r@{extracolsep{fill}}*{3}{S[table-format=1.2e-2]}S[table-format=2]*{3}{S[table-format=1.2]}}
      toprule
      c1 & {c2}     & {c3}     &multicolumn{3}{c}{common header} & {c7} & {c8}  cmidrule{4-6}
         &          &          & {c4}     & {c5}& {c6} &      &     
      midrule
      r1 & 8.95e-04 & 7.35e-04 & 4.78e-06 &  10 & 0.52 & 0.67 & 0.30  
      r2 & 7.08e-21 & 2.09e-02 & 1.70e-26 &  54 & 0.55 & 0.64 & 0.25  
      r3 & 4.94e-04 & 5.50e-02 & 3.52e-06 &  17 & 0.38 & 0.61 & 0.13  
      r4 & 1.16e-06 & 6.65e-02 & 5.54e-08 &  10 & 0.52 & 0.62 & 0.28  
      r5 & 1.27e-05 & 8.61e-02 & 5.20e-07 &  10 & 0.53 & 0.66 & 0.27  
      bottomrule
    end{tabular*} 
end{table} 
        
end{document}

Answered by leandriis on May 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP