TransWikia.com

Nested column-wise cell merging using multicolumn in a long table environment

TeX - LaTeX Asked by aimbotter21 on May 15, 2021

The following table is set up as can be seen below. I would like to combine duplicated values in column 1-3 by merging the table cells column-wise. However, as you can see the merging of cells is on a different level or spans a different number of cells in each considered column.

    begin{longtable}[!htbp]{ |m{2cm} m{3.7cm} m{2cm} | m{1.8cm} m{1.8cm} m{1.8cm}| }
 hline
 textbf{Rating-level} & textbf{Rating category} & textbf{Credit risk} & textbf{Fitch newline (original)} & textbf{S&P newline (original)} & textbf{Moody's newline (original)} 
 hline
    rowcolor{YellowGreen}IG & Highest-grade & Lowest & AAA & AAA & Aaa 
    rowcolor{YellowGreen}IG & High grade & Very low & AA+ & AA+ & Aa1 
    rowcolor{YellowGreen}IG & High grade & Very low & AA & AA & Aa2 
    rowcolor{YellowGreen}IG & High grade & Very low & AA- & AA- & Aa3 
    rowcolor{YellowGreen}IG & Upper medium grade & Low & A+ & A+ & A1 
    rowcolor{YellowGreen}IG & Upper medium grade & Low & A & A & A2 
    rowcolor{YellowGreen}IG & Upper medium grade & Low & A- & A- & A3 
    rowcolor{YellowGreen}IG & Medium grade & Moderate & BBB+ & BBB+ & Baa1 
    rowcolor{YellowGreen}IG & Medium grade & Moderate & BBB & BBB & Baa2 
    rowcolor{YellowGreen}IG & Medium grade & Moderate & BBB- & BBB- & Baa3  
    hline
    rowcolor{Apricot}Non-IG & Speculative grade & Substantial & BB+ & BB+ & Ba1 
    rowcolor{Apricot}Non-IG & Speculative grade & Substantial & BB & BB & Ba2 
    rowcolor{Apricot}Non-IG & Speculative grade & Substantial & BB- & BB- & Ba3 
    rowcolor{Apricot}Non-IG & Speculative grade & High & B+ & B+ & B1 
    rowcolor{Apricot}Non-IG & Speculative grade & High & B & B & B2 
    rowcolor{Apricot}Non-IG & Speculative grade & High & B- & B- & B3 
    rowcolor{Apricot}Non-IG & Highly speculative grade & Very high & CCC+ & CCC+ & Caa1 
    rowcolor{Apricot}Non-IG & Highly speculative grade & Very high & CCC & CCC & Caa2 
    rowcolor{Apricot}Non-IG & Highly speculative grade & Very high & CCC- & CCC- & Caa3 
    % rowcolor{Apricot}Non-IG & & & & & & 
    rowcolor{Apricot}Non-IG & Highly speculative grade & Likely in or newline near default & CC & CC &  
    % rowcolor{Apricot}Non-IG & & & & & & 
    rowcolor{Apricot}Non-IG & Highly speculative grade & Likely in or newline near default & & & Ca 
    % rowcolor{Apricot}Non-IG & & & & & & 
    rowcolor{Apricot}Non-IG & Highly speculative grade & Likely in or newline near default & C & C &    
    hline
    rowcolor{Red}Default & Default & In default & D & D & C 
 hline
caption{Numeric ratings mapping table} 
% needs to go inside longtable environment
label{tab:4thlongtable}
end{longtable}

I tried hours with stacking the multicolumn command on various levels but none gave me the desired output. To clarify, in column 1, I would like to have 3 stacked cells (IG, Non-IG, Def), in column 2 accordingly 7 stacked cells and in column 3 accordingly 9 cells. Somehow every method I tried, completely screwed up the table layout, merging row-wise and column-wise.

Would highly appreciate any pointers!

2 Answers

I guess, the following is closer to the expected output:

enter image description here

documentclass{article}
usepackage{geometry}
usepackage[dvipsnames]{xcolor}
usepackage{nicematrix}
NiceMatrixOptions{cell-space-limits = 0.5tabcolsep}

begin{document}
begin{table}
centering
caption{Numeric ratings mapping table} 
label{tab:4thlongtable}
    begin{NiceTabular}{ llllll }[hvlines, %
                                  code-before={%
                                     rowcolor{YellowGreen}{2-11}
                                     rowcolor{Apricot}{12-23}
                                     rowcolor{Red}{24}}]
 textbf{Block{1-1}{Rating- level}} & textbf{Block{1-1}{Rating category}} & textbf{Block{1-1}{Credit risk}} & textbf{Block{1-1}{Fitch  (original)}} & textbf{Block{1-1}{S&P  (original)}} & textbf{Block{1-1}{Moody's  (original)}} 
    Block[l]{10-1}{IG}     & Highest-grade                                & Lowest                                        & AAA  & AAA  & Aaa 
                            & Block[l]{3-1}{High grade}                   & Block[l]{3-1}{Very low}                      & AA+  & AA+  & Aa1 
                            &                                              &                                               & AA   & AA   & Aa2 
                            &                                              &                                               & AA-  & AA-  & Aa3 
                            & Block[l]{3-1}{Upper medium grade}         & Block[l]{3-1}{Low}                           & A+   & A+   & A1 
                            &                                              &                                               & A    & A    & A2 
                            &                                              &                                               & A-   & A-   & A3 
                            & Block[l]{3-1}{Medium grade}                 & Block[l]{3-1}{Moderate}                      & BBB+ & BBB+ & Baa1 
                            &                                              &                                               & BBB  & BBB  & Baa2 
                            &                                              &                                               & BBB- & BBB- & Baa3  
    Block[l]{12-1}{Non-IG} & Block[l]{6-1}{Speculative grade}          & Block[l]{3-1}{Substantial}                   & BB+  & BB+  & Ba1 
                            &                                              &                                               & BB   & BB   & Ba2 
                            &                                              &                                               & BB-  & BB-  & Ba3 
                            &                                              & Block[l]{3-1}{High}                          & B+   & B+   & B1 
                            &                                              &                                               & B    & B    & B2 
                            &                                              &                                               & B-   & B-   & B3 
                            & Block[l]{6-1}{Highly speculative grade} & Block[l]{3-1}{Very high}                     & CCC+ & CCC+ & Caa1 
                            &                                              &                                               & CCC  & CCC  & Caa2 
                            &                                              &                                               & CCC- & CCC- & Caa3 
                            &                                              & Block[l]{3-1}{Likely in or near default} & CC   & CC   &  
                            &                                              &                                               &      &      & Ca 
                            &                                              &                                               & C    & C    &    
    Default                 & Default                                      & In default                                    & D    & D    & C 
end{NiceTabular}
end{table}
end{document}

Correct answer by leandriis on May 15, 2021

Didn't know how to include a picture otherwise, so pls see below what I get with your solution so far. I managed to compile the table in a way that the merging of the cells column-wise looks like in your example, but somehow the column width is out of bounds. I tried manually adjusting them using the initial column types that allow specification of the column width in the table environment (p{2cm} and m{2cm}, but that didn't work.

After looking hours in the pkg documentation, I am not any further at a working solution. I would love your feedback on this follow-up question, if you have an idea. Thanks!

enter image description here

Answered by aimbotter21 on May 15, 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